iTextSharp embed subset fonts in existing PDF

£可爱£侵袭症+ 提交于 2020-01-13 17:04:10

问题


We use an old reporting software to create PDFs, but it is unable to embed the used fonts in the file.

Now I am trying to use iTextSharp to embed all (non-embedded) fonts in an existing PDF based on these examples EmbedFontPostFacto, ListUsedFonts and other information from other similar questions.

Like in the first example I create a fontfile for each font and then add it to the font object (PdfDictionary) or rather to the font descriptor.

// the font file
byte[] fontfile = null;
using (FileStream fs = new FileStream(
    fontFileName, FileMode.Open, FileAccess.Read))
{
    fontfile = new byte[fs.Length];
    fs.Read(fontfile, 0, (int)fs.Length);
}
// create a new stream for the font file
PdfStream stream = new PdfStream(fontfile);
stream.FlateCompress();
stream.Put(PdfName.LENGTH1, new PdfNumber(fontfile.Length));


PdfDictionary desc = font.GetAsDict(PdfName.FONTDESCRIPTOR);
PdfIndirectObject objref = stamper.Writer.AddToBody(stream);
desc.Put(PdfName.FONTFILE2, objref.IndirectReference);

This (mostly) works, but since this embeds the entire font and not a subset, the files can get very big.

So I am wondering if there is a way to embed the fonts only as a subset retroactively, either with iTextSharp or any other way.

Any help is appreciated.

来源:https://stackoverflow.com/questions/39747620/itextsharp-embed-subset-fonts-in-existing-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!