How to CHECK if the Font is embedded

I use itext to create PDf documents. Some fonts may not be used due to licensing restrictions.

... ExceptionConverter: com.lowagie.text.DocumentException: C:\WINDOWS\Fonts\LucidaSansRegular.ttf cannot be embedded due to licensing restrictions. at com.lowagie.text.pdf.TrueTypeFontUnicode.<init>(Unknown Source) at com.lowagie.text.pdf.BaseFont.createFont(Unknown Source) at com.lowagie.text.pdf.BaseFont.createFont(Unknown Source) at com.lowagie.text.pdf.BaseFont.createFont(Unknown Source) at com.lowagie.text.pdf.DefaultFontMapper.awtToPdf(Unknown Source) at com.lowagie.text.pdf.PdfGraphics2D.getCachedBaseFont(Unknown Source) at com.lowagie.text.pdf.PdfGraphics2D.setFont(Unknown Source) ... 

I am considering checking font or PDF content to verify this case. How to check if Font is embedded using java or itext?

+4
source share
1 answer

As far as I can tell, there is no direct way to determine if a font can be embedded. I did a quick search, and I don’t think it’s possible except using the catch catch method mentioned in Eric's comments.

  // 1) have a list of all fonts ArrayList allAvailableFonts; // 2) second list of fonts that that can be embedded ArrayList embedableFonts; //Iterate through every available font in allAvailableFonts for( .... allAvailableFonts ..... ) { boolean isFontEmbeddable = true; try { // try to embed the font } catch( DocumentException de) { //this font cannot be embedded isEmbeddable = false; } if( isEmbeddable ) { // add to list of embeddable fonts embedableFonts.add ( font ); } } 

Perhaps you may actually be doing hardcore and making your own calls on Windows Apis to get the same result, but I think it works too much for a simple task like this.

Some research has been done to find out how this exception is thrown by Java.

The code that throws the above exception can be found here. http://kickjava.com/src/com/lowagie/text/pdf/TrueTypeFont.java.htm Line number 367 368

 if (!justNames && embedded && os_2.fsType == 2) throw new DocumentException(fileName + style + " cannot be embedded due to licensing restrictions."); 

The interesting part that should be noted is the condition os_2.fsType == 2

os_2 is an instance of WindowsMetrics , see line 174 here http://kickjava.com/src/com/lowagie/text/pdf/TrueTypeFont.java.htm

Search WindowsMetrics on Google, and that was what I got.

This explains that the fsType parameter contains information about whether the font can be embedded. http://www.microsoft.com/typography/otspec/os2ver3.htm#fst

The explicit equivalent of WindowsMetrics used in itext http://www.docjar.org/docs/api/com/lowagie/text/pdf/TrueTypeFont.WindowsMetrics.html

+5
source

All Articles