IText font not subsetting or embedding

2019-09-09 22:36发布

问题:

I am on the mainframe platform and uploaded the arial.ttf from Windows.
I used the following code for the font, but the font does not show SUBSETTED or EMBEDDED in Adobe.
I even tried to add font.getBaseFont to force it to embed.

Any reason why it would not embed or subset?

String font1 = "arial.ttf";                                                
FontFactory.register(font1,"myfont");                                      
BaseFont bf = BaseFont.createFont(font1, BaseFont.IDENTITY_H, true);       
Font font =  FontFactory.getFont("arial");   

font.getBaseFont().setSubset(true);    

Adobe doc show the following font information:

Type truetype
Encoding Ansi
Actual Font: ArialMT
Actual Font type: TrueType

回答1:

You create a BaseFont object bf, but you aren't doing anything with it. One would expect that you do this:

BaseFont bf = BaseFont.createFont(pathToFont, BaseFont.IDENTITY_H, true);
Font font = new Font(bf, 12);

In this case, font would make sure that a subset of the font is embedded because the encoding is Identity-H and iText always embeds a subset of a font with that encoding.

As you aren't doing anything with bf, it is as if the line isn't present. In that case, we are left with:

String font1 = "arial.ttf";                                                
FontFactory.register(font1,"myfont");    
Font font =  FontFactory.getFont("arial"); 

Assuming that the path to arial.ttf is correct, and that the alias of that font is "arial", you are now creating a font with the default encoding (Ansi), the default font size (12) and the default embedding (false).

That is in line with what is shown in Adobe Reader. If you want a subset of the font to be embedded, you need at least:

Font font =  FontFactory.getFont("arial", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

In answer to your question: the reason why the font is not embedded by iText is the fact that you are not telling iText to embed the font.



标签: itext