PDFOne .NET为开发人员提供了字体嵌入和替换的方法,为PDF嵌入字体首先得创建一个PDF字体对象,然后使用PDFDocument.EmbedFont(PDFFont fontToBeEmbedded)方法把字体嵌入到PDF里,具体代码如下:
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();
当替换一种字体时,需要创建一个PDF字体对象,并设置 PDFFontEmbedType.Full,然后使用 PDFDocument.EmbedFont(PDFFont
fontToBeEmbedded, string originalFont) 进行替换,具体代码如下:
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();