PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2008 VS 2005 CLR 2.0

Embed and Replace Fonts In PDF Documents

PDFOne .NET now supports embedding fonts both in creation mode as well as reading mode.
By Raju Sinha

Until recently, PDFOne .NET supported font embedding only in creation mode. In Version 1.43, methods to enable font embedding in reading mode as well were introduced. One of the methods provides the ability to replace a font existing in the document with another font that will be embedded in its place. In this article, we will see how to use these methods.

Embedding Fonts In PDF Documents

To embed a font, we need to first create a PDF font object with a font file present on the system. The font can then be embedded with the new PDFDocument.EmbedFont(PDFFont fontToBeEmbedded) method.

PDFDocument doc = new PDFDocument();
doc.Load("DocWithoutEmbeddedFont.pdf");
Font fnt = new Font("Garamond", 24);
PDFFont pfnt = new PDFFont(fnt, PDFFontEmbedType.Full);
doc.EmbedFont(pfnt);
doc.OpenAfterCreate = true;
doc.Save("DocWithEmbeddedFont.pdf");
doc.Close();

Replacing an Existing Font

Again, when you try to replace a font, you need to create the PDF font object with the PDFFontEmbedType.Full setting. Next, you embed the font using another overloaded method PDFDocument.EmbedFont(PDFFont fontToBeEmbedded, string originalFont) where you can specify the font that needs to be replaced.

PDFDocument doc = new PDFDocument();
doc.Load("DocWithoutEmbeddedFont.pdf");
Font wf = new Font("Segoe UI", 24);
PDFFont pf = new PDFFont(wf, PDFFontEmbedType.Full);
doc.EmbedFont(pf, "Garamond"); 
doc.OpenAfterCreate = true;
doc.Save("DocWithReplacedFont.pdf");
doc.Close();
Privacy | Legal | Feedback | Newsletter © 2002-2010 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows users are advised to use Microsoft ClearType Tuning for optimal experience. Linux and other users can enable font smoothing, as supported by their OS. Also, please use the latest version of a standards-compliant browser such as Opera, FireFox, Chrome or Safari.