Can I embed a font only partially?

Category: 
Tags: fontsfont subsetembedded fontsiText 5

I am asked to include partial font into a PDF. I think I will use iText and I found how to embed the font, but I found no clue about partial embedding. Does anybody know if partial embedding is automatic? Or maybe iText does not have this feature?

Posted on StackOverflow on Sep 8, 2015 by Romain Smordowski

When does iText embed the full font, a subset, or no font?

In this answer, it is assumed that you use the BaseFont class and the Font class like this:

BaseFont bf = BaseFont.createFont(pathToFont, encoding, embedded);
Font font = new Font(bf, 12);

In this snippet:

  • pathToFont is the path to a font file (.ttf, .ttc, otf, .afm),

  • encoding is an encoding such as "winansi", BaseFont.IDENTITY_H,...

  • embedded is a boolean: true or false.

Will iText embed the font or not?

That's determined by the embedded parameter:

  • If it is false, the font isn't embedded.

  • If it is true, the font is embedded, except in the case of the Standard Type 1 fonts or Type 1 fonts for which the .pfb file is missing or CJK fonts.

Regarding the exceptions:

  • The Standard Type 1 fonts are 4 flavors of Helvetica (regular, bold, italic, bold-italic), 4 flavors of Times Roman (...), 4 flavors of Courier (...), Symbol and Zapfdingbats. iText ships with 14 Adobe Font Metrics (AFM) files. These files contain the metrics that are needed to calculate widths of glyphs and words. iText doesn't have the necessary Printer Font Binary (PFB) files that are required to embed the font.

  • Type 1 fonts are stored in two files: an AFM file and a PFB file. If you provide an AFM file, iText will look for the PFB file in the same directory. If iText doesn't find any PFB file, the font can't be embedded.

  • CJK stands for a series of Chinese, Japanese and Korean fonts that are available in downloadable font packs. It's a special type of Asian fonts; Asian fonts in .ttf, .otf or .ttc files can be embedded.

Will iText subset the font or not?

iText will always try to embed a subset of the font, not the whole font, except in case you provide a Type 1 font (AFM and PFB file). In case a Type 1 font is provided, the full font is embedded.

Can iText embed the full font?

Yes, you can force iText to embed the full font by adding this line:

bf.setSubset(false);

However, this value will be ignored in case you use the encoding Identity-H because that's how it's described in ISO-32000-1. iText will only embed full fonts that are stored inside the PDF as a simple font (256 characters); iText will never embed fonts that are stored as a composite font (up to 65,535 characters).