Using iText7 to create Font "Courier_new" - c#

I am trying to create a font Courier new in C# using iText7 package. I can only find Courier among the standard fonts, but i would like to use Courier new. My code looks like this:
PdfFont courier = PdfFontFactory.CreateFont(StandardFonts.COURIER, false);
How can I create custom fonts with iText7?

The set of standard fonts is fixed in the PDF specification and consists of 14 fonts in total. Moreover, the visual appearance of those fonts in PDF viewers may be implementation dependent and thus different from viewer to viewer (although in practice you pretty much can't see the difference).
To use your custom font you will need a font file (usually a .ttf or .otf file). E.g. the regular (not bold, not italic) Courier New font shipped with Windows is usually located at C:/Windows/Fonts/cour.ttf (please check with the Windows Fonts licenses whether you are allowed to use it).
You should also provide encoding to the method creating the font. If you don't provide the encoding your Latin characters will be converted just fine, but for a broader range of Unicode characters you should provide IDENTITY_H encoding.
Example:
PdfFont font = PdfFontFactory.createFont("C:/Windows/Fonts/cour.ttf", PdfEncodings.IDENTITY_H);

Related

Usage of CJK font in PDFsharp / Telerik RadPdfViewer

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 "標楷體".

iTextSharp Helvetica using ArialMT as actual font

I am creating an export from HTML using the Helvetica font family:
But when the PDF is created I get this:
And the font being used is definitely Arial rather than Helvetica because the top of the 't' is not flat:
Our project is loading fonts from the '\content\fonts' dir in the web solution, but I've cleared this out completely and the issues still persists.
All that is left after clearing it out is 6 fonts that I presume iText is picking up from my system or that come bundled.

how does font work in windows OS /browser?

I know there are .ttf files.
I'm guessing this is the file that OS uses to display fonts?
Does the browser uses the same font files to display fonts?
If not, where does it stores it's own font files?
what's the difference between the .ttf font and Unicode font?
I mean how/where does Unicode fit into .ttf file format?
Starting with the last question first, asking about the difference between TTF and Unicode Fonts is a bit like asking about the difference between "JPG files" and "Images". One is kind of a version of the other:
A modern .ttf file is not so much a "truetype font" as it is an OpenType font that uses the TrueType format for the part that needs to encode shapes and specify where inside the fonts those shapes can be found. Perhaps surprisingly, that's only a small part of what makes a modern font, and the important part here is that it's an OpenType font, not really that it uses the TrueType format.
The vast majority of modern fonts are OpenType fonts, with two common formats: .ttf for OpenType fonts that use TrueType format, and .otf for OpenType fonts that use the CFF format. There are a few more formats that OpenType can use (bitmaps and SVG, for instance), but you won't run into those a lot.
OpenType fonts also specify which character mappings they support, and they can support multiple mappings: it's entirely possible for a font to support a Unicode mapping as well as Windows-specific and Mac-specific non-unicode mappings, for several different languages, or not support Unicode's mapping, but be able to typeset text in tens of scripts that are also found in Unicode, just using different, unicode-incompatible mappings.
So, on to the first question:
Browsers get their default fonts from the OS. There is a set of "safe fonts" that all Operating Systems come with, and Browsers rely on that set for the default text rendering. Browsers, much like any big application, are compiled separately for each target platform, so they simply know which fonts are available on those platforms, and how to ask the OS for them.
In addition to this, CSS can be used to specify that there is content that needs to be styled by a non-default system font, or even by a font that is explicitly pointed to by a URL (using #font-face rule). For system fonts, the browser will simply ask the OS for the font reference, for fonts-by-URL the browser downloads the font, and then uses that. But, in both cases, only provided that the font passes a few security checks to make sure it's not (sometimes subtly) corrupt or internally inconsistent.

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 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

Categories