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.
Related
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);
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 "標楷體".
Hi anyone know how to get the Glyph Name from a TTF or OFT font file from C#, I'm willing to parse the file directly to get them if necessary? I found this post here Access opentype glyph names from WPF but it got no answer.
P.S. I have created the font myself and am creating an program to create a CSS (LESS or SASS) file to use the Glyphs I have made easily in web pages like Bootstrap or FontAwesome :)
In TrueType-based fonts (.TTF files), you can try parsing the 'post' table. It's fairly easy to figure out. But, only format 2.0 explicitly stores glyph names. If the post table is format 3.0, there are no glyph names stored (there are a couple of other formats defined, but fonts using them are very, very rare). In that case, your only option is to back-track using Unicode values from the 'cmap'...there are some standard references for Unicode-to-glyph names that may be useful.
For CFF-based fonts (.OTF files), glyph names are stored inside of the 'CFF ' table. That's a bit trickier to parse, but if you're only looking for the glyph name references it shouldn't be too difficult to figure out.
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.
Background:
I have PDF's I am programmatically generating. I need to be able to send the PDF directly to a printer from the server (not through an intermediate application). At the moment I can do all of the above (generate PDF, send to printer), but because the fonts aren't embedded in the PDF the printer is doing font substitution.
Why the fonts aren't embedded when generated:
I am creating PDF's using SQL Reporting Services 2008. There is a known issue with SQL Reporting Services in that it will not embed fonts (unless a series of requirements are met - http://technet.microsoft.com/en-us/library/ms159713%28SQL.100%29.aspx). Don't ask me why, the PDF meets all of MS's listed requirements and the fonts still show up as not embedded - there is no real control over whether the fonts are embedded, so I have accepted that this isn't working and moved on. The suggested workaround from Microsoft (Link under 'When will Reporting Services do font embedding') is to post process the PDF to manually embed the fonts.
Goal
Take an already generated PDF document, programmatically 'open' it and embed the fonts, resave the PDF.
Approach
I was pointed towards iTextSharp, but most of the examples are for the Java version and I'm having trouble translating to the iTextSharp version (I can't find any documentation for iTextSharp).
I am working on this post for what I need to do: Itext embed font in a PDF.
However for the life of me, I cannot seem to use the ByteArrayOutputStream object. It can't seem to find it. I've researched and researched but nobody seems to say what class it's in or where I find it so I can include it in the using statements. I've even cracked open Reflector and can't seem to find it anywhere.
This is what I have so far and it compiles etc. etc.
(result is my byte[] of the generated PDF).
PdfReader pdf = new PdfReader(result);
BaseFont unicode = BaseFont.CreateFont("Georgia", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// the next line doesn't work as I need a ByteArrayOutputStream variable to pass in
PdfStamper stamper = new PdfStamper(pdf, MISSINGBYTEARRAYOUTPUTSTREAMVARIABLE);
stamper.AcroFields.SetFieldProperty("test", "textfont", unicode, null);
stamper.Close();
pdf.Close();
So can anybody either help me with using iTextSharp to embed fonts into a PDF or point me in the right direction?
I'm more than happy to use any other solutions other than iTextSharp to complete this goal, but it needs to be free and able to be used by a business for an internal application (i.e. Affero GPL).
This may not be the answer you are looking for (since you want to get your problems solved programmatically, not by an external tool).
But you can use Ghostscript commandline to embed missing fonts in retrospect to PDFs which have not embedded them:
gs \
-sFONTPATH=/path/to/fonts:/another/dir/with/more/fonts \
-o output-pdf-with-embedded-fonts.pdf \
-sDEVICE=pdfwrite \
-dPDFSETTINGS=/prepress \
input-pdf-where-some-fonts-are-not-embedded.pdf
One important thing is that the missing fonts are all available in one of the directories pointed to by the -sFontPath=... switch.
Besides Ghostscript, it is also possible to use Poppler and Cairo. There is a command pdftocairo from Poppler that converts PDF to PDF via pdftocairo -pdf input.pdf output.pdf. It also considers font substitutions set in a Fontconfig configuration file. This is very helpful if you do not have all fonts on your system that are referenced in a PDF file, but know which other font you have installed is a good-looking replacement. After processing, the substitution font is embedded.
I had this problem on a Mac with a PDF I was submitting to IEEE. Using Adobe Reader and Preview, I was able to get around this. I think any pdf printer might work in place of Preview if you are on a PC.
Here are the steps I took. You can individually fix each figure, or fix the whole document.
Open at pdf file using Adobe Reader.
Right click on image, and click “Document Properties.”
Click “Fonts.” Check to see if the font isn’t embedded. Should say “Courier” or other font name.
If your pdf isn’t a standard page size, click on “Description” and look at the page size. Write this down. Ex. 19.4 x 5.22 in.
Open the pdf up in Preview. Go to File->Print. If using a pdf that isn’t a standard page size, click on Paper Size and choose custom. You will need to create a custom page size that is equal to the one you wrote down in step 4. Don’t forget to zero the margins to 0 for all sides. After doing that, you’ll need to set the scale of the print in the print dialog to 100%.
In the lower left of the print dialog (in Preview on a Mac), click “PDF” to print the PDF to a new PDF. Select the destination and print.
Open the new pdf up in Adobe Reader and verify that the fonts are now embedded.
I hope this helps.
I had this problem today with an existing PDF I uploaded to lulu.com to make a printed copy. It was rejected for not having all fonts embedded.
I found that if I opened it in Acrobat X and Saved out as postscript .ps file, then when I double clicked this .ps file in File Explorer, it opened in Acrobat X Distiller, and this automatically created a new PDF file with all fonts embedded!
Naturally this would mean you must have all the fonts needed on your computer. Otherwise a program like InFix can make font substitutions.