Font embedding
Version 6.1.0
In this article
- Why fonts should be embedded
- Adding additional glyphs to a subset
- Limitations of font sub-setting
- Embedding a whole font
This article describes why and how PDFsharp embeds font files in a PDF document.
Why fonts should be embedded?
To ensure that a PDF viewer application like Acrobat can display all characters correctly, the files of the used fonts can be embedded in the PDF document. PDFsharp always embeds font files that are used into the PDF document. You cannot turn that off anymore as you could in earlier versions of PDFsharp. So a viewer app always finds the right glyph for every character. But because modern font files may contain thousands of glyphs for thousands of Unicode characters, PDF files may be very large even if only one or two fonts are used and embedded completely. Therefore, PDFsharp usually compiles each original font file to a new font subset. The subset contains only the glyphs of the characters actually used in the document. This ensures that no superfluous glyphs are in the produced PDF file and the PDF document is typically significantly smaller.
Adding additional glyphs to a subset
Sometimes a PDF file is created for being modified with Acrobat or contains text fields for typing. In this case, more characters are required than those actually used in the document. With the function AddCharacters of the PdfDocument class you can add the glyphs of characters of a string to the subset of a particular font.
// Create a font.
var font = new XFont(…);
// Ensures that the glyphs of the specified characters are added to the font subset
// even if they are currently not used in the document.
document.AddCharacters(font, "ABCDEFG… abcdefg… 12345…");
Note
The font must be used for drawing text at least one time on a page. Otherwise it will not be embedded at all. You can force this by drawing a blank on a page using this font.
For PDFsharp versions earlier than 6.1 you can draw a dummy text with the required characters outside the visible area of a page. This hack has the same effect at the cost of a slightly larger PDF file.
Limitations of font sub-setting
PDFsharp cannot compute subsets of OpenType fonts with PostScript outlines. Subsets can only be computed from OpenType fonts with TrueType outlines. If PDFsharp cannot compute a subset, the whole font is embedded.
Furthermore, OpenType font collections cannot be used by PDFsharp and therefore not be embedded.
Embedding a whole font
By default PDFsharp tries to compute a font subset that contains only the glyphs of the characters actually used in the document. However, if you want to embed the whole font file as it is, you can enforce this by specifying this when you create the font.
var font = new XFont("Arial", 10, XFontStyleEx.Regular,
new XPdfFontOptions(PdfFontEmbedding.EmbedCompleteFontFile));
The code above tags the underlying Arial font face for embedding without creating a subset. It is not necessary to specify the XPdfFontOptions each time a new instance of XFont is created. One time is enough.
If you create one instance of XFont for the same font face with PdfFontEmbedding.TryComputeSubset and another with PdfFontEmbedding.EmbedCompleteFontFile, embedding the complete font wins.
Note
Fonts may contain embedding rights in their so called OS/2 table. These rights specify whether or not it is allowed to embed a font. PDFsharp does not evaluate these entries and embeds a font anyway when used for drawing text, because PDFsharp cannot determine whether you have the right to use that font. Therefore you must only use fonts with PDFsharp where you have the right to embed them.