Usage of CJK font in PDFsharp / Telerik RadPdfViewer - c#

Some background:
I’m developing a WPF application used for measuring and comparing data that’s delivered of a balance attached to it. It will be installed on a Windows 10 system delivered together with the balance. The application currently has to support eleven languages including Japanese and Chinese. One feature of the application is, that the values measured can be shown as a PDF report. For the creation of the PDF we use the PDFsharp library, for displaying the Telerik RadPdfViewer.
As mentioned before the application supports Japanese and Chinese language, therefore the PDF also needs the ability to show Japanese and Chinese letters. Earlier versions of the application were delivered on older versions of Windows, which meant we could use either the Microsoft YaHei or Arial Unicode MS font for this case. Unfortunately PDFsharp does not support TrueType font collections, which means I can’t use the version of Microsoft Yahei installed on the System and Arial Unicode MS is not available anymore.
Problem:
Since these two aren’t an option anymore I searched the internet for an alternative. After a quick search I noticed that Google’s Noto Sans might be what I need, so I tried to use it. Unfortunately it resulted in a ton of IndexOutOfRange Exceptions from the Telerik.Windows.Documents.Fixed.dll (all internally caught by the library) and does not show any letters on the generated PDF. It shows the PDF and the lines of the generated table, so I assume it’s some problem with the font. I used “Noto Sans CJK SC Regular” for Chinese and “Noto Sans CJK JP Regular” for Japanese.
After a long search session I still did not find another suitable font for Chinese. For Japanese I could use “Gen Shin Gothic”, but I’d prefer to use a font with the same style for both languages. There is the additional problem that the font should be usable without an additional license needed.
Unfortunately I can’t add code, since it’s a running project of my employer.
Questions:
Is there something I need to set/adjust for the Noto fonts to work properly?
Is there another usable sans-serif font for Chinese, where no additional licensing is needed? (It has to be capable of showing Latin letters too, since some stuff, like the product name is written that way)
Alternatively:
Is there a tweak to the PDFsharp library, so it can use TTC fonts?

A user posted their changes for TTC support on the PDFsharp forum:
https://forum.pdfsharp.net/viewtopic.php?p=9039#p9039
Another user found a suitable TTF file that works with PDFsharp:
https://forum.pdfsharp.net/viewtopic.php?p=11874#p11874
There is one TTF and its font name is "標楷體".

Related

ActiveReports interface to RightFax

I'm trying send ActiveReports formatted reports to my company's RightFax server, and pre-set some of the fax fields, like FAX number, sender, and recipient. The RightFax documentation says that the document must include Embedded Codes to set these values, e.g. <TOFAXNUM:12345556789><TONAME:Recipient><FROMNAME:Sender>. I create a TextBox or Label in ActiveReports that contains this text. But the values are not set when RightFax receives the document and brings up the RightFax client UI. The Embedded Codes remain in the fax image. I have the RightFax printer driver set to HP LaserJet 4. I'm developing in C#, using Visual Studio 2010 Professional.
One suggestion on the web is to make sure the Embedded Codes are set in Courier or another "printer font". However, Visual Studio does not have "Courier" or "Times Roman", only MS true type versions of these standard fonts, "Courier New" and "Times New Roman".
This method of sending faxes is working with older software, that doesn't use ActiveReports, on another machine using the same RightFax server.
Any experience you can share interfacing ActiveReports to RightFax would be most appreciated.
Thanks,
Gregg Lobdell
You could accomplish your task by controlling the whole printing process and sending the escape sequence using Windows API. I assume you using ActiveReports 6 or section-based reports of ActiveReports 7.
In more detail:
create your own PrintDocument and define PrintPage handler
in the PrintPage handler send escape sequence to printer using Escape winapi call (see the example on CodeProject)
render the page itself by calling the Page.Draw()
As long as you use a true type font, the printer should recognize that font and be able to "read" the text in it. Only old bitmap fonts are fonts that might be not readable by the printer. Usually commonly used TT fonts on windows like "Courier New" or "Times New Roman", are already in the printer so they won't even be downloaded.
However, RightFax does have some documentation on escape codes here, so you might want to try using escape codes with ActiveReports. Also, here is an example of using the SystemPrinter object in ActiveReports6 to send escape codes directly to the printer without using any special API. You might try using that code and replacing the escape code there with ones that RightFax understands.
Finally, ActiveReports essentially prints by getting a graphics from the printer and drawing on it. Textboxes are real text drawn with appropriate text commands (i.e. text is not rendered as bitmaps). This is a normal way of printing in modern windows so any printer should see the text as normal text. You should be abel to see the same exact results by writing your own simple printing code in .NET and sending it to the RightFax driver. If it works there it will work in ActiveReports.
If it isn't working, and the escape code trick above won't work, I think contacting RightFax and asking them for insight into how to print to their driver from a .NET application would be the next logical step.
Hope this helps!

Japanese characters are not rendered correctly on image on ASP.NET page

I create an image in C# which should contain some text in Japanese. Then I put this image into the whole page which is also in Japanese. The whole page is displayed correctly (Encoding: UTF-8), but the image is rendered incorrectly. Instead of the correct text I get wrong symbols (not '?' but something similar to square).
I need to write this text on image in Arial. Does anyone know what might be wrong? Why text is not rendered correctly.
And one more think... when I test it on my local machine everything looks correctly, but when I deploy app with page on external server this strange error occurs.
In order to create image with text I use:
Font f = Font("Arial", 10f, FontStyle.Bold);
g.DrawString(text, f, b, rect);
The external server likely has a version of Arial installed, which doesn't include the Japanese character set (as far as I recall, the one that includes Japanese is called "Arial Unicode MS"). Remember that when you're generating an image in ASP.NET, it's the servers fonts that are used.
Note, however, that legally, you're not allowed to install "Arial Unicode MS", except when it's part of Office - or if you've licensed it ("Arial Unicode") from Monotype/Ascender. It may be a more viable option to replace Arial with some other typeface, depending on your funds (I'll keep my subjective opinions on Arial out of this).
When installing a new font on the server, make sure you restart IIS. .NET won't recognize installed fonts until a restart (it's not enough to restart the application - may be enough to recycle the app pool, but I never tried that).
Update
If it still doesn't work, it's likely font fallback isn't in place. I.e., you're specifying "Arial", but GDI+ (DrawString) doesn't know to fall back to "Arial Unicode MS" for characters that aren't in Arial (Office sets this up on install, I think).
Two possibilities:
Change your code to actually use the font (i.e. "Arial Unicode MS") rather than "Arial" (which never has Japanese characters in any other versions). This has the disadvantage that if you're using other characters than Japanese, they may look (even) less good than in the standard "Arial" typeface, because "Arial Unicode MS" includes no kerning or other such features.
Or check if there's a link between Arial and other fonts in your (local) registry: HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontLink\SystemLink - one of those fonts will be the one actually displaying your Japanese characters as a fallback font - it may not even be "Arial Unicode". You may add the same link manually in registry on the server (and probably restart IIS again).
Another likely candidate that may be used for fallback is "MS Gothic". As far as I recall, GDI+ uses the above "FontLink" system for font fallback, while WPF uses its own system. The easiest way to be sure (when you're using fonts that you control anyway) is to directly use a font that has Japanese glyphs. Arial Unicode is merely intended as a last resort for Windows when glyphs aren't found in other fonts - not as something that looks nice in itself.

How to change font in word document

I'm wondering how can I change the font (+ color and some font style) for the whole word document (excluding the page headers, if it's possible) by using C# ?
If You know any libraries (also commercial) feel free to let me know.
(After changing the font, I'll convert that word document to PDF using ABCPdf)
Depending on the library you would loop through the content (Pages/Paragraphs etc.) and change whatever font attribute you want...
Since you don't say which type of application you build - in case it is a pure desktop application and Word is guranteed to be installed locally you can use Office Interop. BEWARE that Office Interop is NOT supported by MS in server-scenarios (like IIS, ASP.NET. Windows Service, WCF etc.).
IF Word is not guaranteed to be installed and/or your application is not a desktop application and your Word documents are always DOCX files you could use OpenXML SDK V 2 from MS (free).
Otherwise I can recommend Aspose.Words (commercial) - works with old DOC and current DOCX and is usable in any type of application (desktop, ASP.NET, Windows Service etc.).
Handling Office documents in .NET is a quite straight forward task. Have a look at Microsoft.Office.Interop.Word, it is pretty well documented. Here is another link on to how you can change a word document font style using .NET (the example is in VB but is easily convertible to C#)
Start by reading on Microsoft.Office.Interop.Word
(You can google some user-friendly examples, there are tons)
Later on, read about Text Formatting
Easy, isn't it?

How does font fallback work on a non-Windows OS?

I have been reading a lot of documentation on how to create an internationalised ASP.NET application, using C# 3.5. They all seem to give the general impression that font fallback and font linking will make font choice a non-issue.
However, the documentation I have seems to imply that it will be fine on Windows, but doesn't mention other OSs, like for the Mac and Linux.
There is no font that supports all characters for all languages, but if your clients are using Windows 2000 or later, it is sufficient to use a single font throughout an application due to font linking and font fallback. This means that your system will determine a font appropriate for the machine's culture. (source)
Will this magically work on a Mac/Linux, or is there some extra legwork involved in supporting these platforms?
If your application isn't producing any sort of documents like PDFs, Excel files or whatnot ASP.Net isn't really responsible for font handling. CSS will be.
For web applications, you have to specify that font fallback yourself, in your CSS files using the font-family directive - documentation for which is available at W3

Which font should I use for Latin and East Asian characters

My application needs to be able to display text in English, German, Chinese, and Korean. I would like to use a single font throughout the application. I know I could use Arial Unicode MS or Lucida Sans Unicode. But they are both very large and need to be licensed.
Is the a good font that I could use?
edit:
This is a windows forms application.
First of all, Arial Unicode MS is a fallback font, it shouldn't be used explicitly. And the idea of having a single font is of much less use than most people think. Keep in mind that the scripts are already very different. You'll need Latin, sinographs and Hangul. While all those probably can be found in a single font I have found the Latin characters to be pretty ugly in comparison.
Furthermore, the operating system already knows which font it can use for which script. And mostly it does a pretty good job at choosing the right one.
It would help if you can say what kind of application we are talking about.
There are big differences (for instance) between ASP.NET (accessed with random browsers from random operating systems) and forms/WPF/Win32 applications.
You cannot use the same font between Korean, Traditional Chinese and Simplified Chinese.
If you do, it is immediately obvious to a native if the font is not the one for his language.

Categories