3

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.

Community
  • 1
  • 1
  • 3
    That task requires an in-depth analysis of every usage of that font in your PDF to determine the required glyphs. While iText(Sharp) does not contain a single method for that task, you can use it as framework to implement such an analysis in. I reckon, though, that a generic implementation might well go beyond the format of a stackoverflow answer. – mkl Sep 28 '16 at 15:46
  • @mkl Thats what I feared. But thank you for your answer. If anyone has other suggestions how to achieve this it would be appreciated. – user6892929 Sep 29 '16 at 12:03

0 Answers0