Error reading font file when using Icepdf

211 views Asked by At

I am writing a program where I need to display PDFs inside a JPanel. To do so I am using the ICEPDF library. The problem is that with some PDFs I got the following exception:

WARNING: Error reading font file with
java.io.IOException: 'head' table is mandatory
    at [email protected]/org.apache.fontbox.ttf.TTFParser.parseTables(TTFParser.java:198)
    at [email protected]/org.apache.fontbox.ttf.TTFParser.parse(TTFParser.java:165)
    at [email protected]/org.apache.fontbox.ttf.TTFParser.parse(TTFParser.java:110)
    at [email protected]/org.icepdf.core.pobjects.fonts.zfont.fontFiles.ZFontTrueType.<init>(ZFontTrueType.java:60)

The file opens anyway but it takes much longer than if exception doesn't occur (4 seconds versus 1 second). Code for the displayPdf is:

void displayPdf(File selectedFile, JPanel homeContainer, CardLayout c1) {
        String filePath = selectedFile.getPath();
        SwingController controller = new SwingController();
        SwingViewBuilder factory = new SwingViewBuilder(controller);
        JPanel viewerComponentPanel = factory.buildViewerPanel();
        ComponentKeyBinding.install(controller, viewerComponentPanel);
        controller.getDocumentViewController().setAnnotationCallback(
                new org.icepdf.ri.common.MyAnnotationCallback(
                        controller.getDocumentViewController()));
        JFrame window = new JFrame("PDF file");
        window.getContentPane().add(viewerComponentPanel);
        window.pack();
        window.setVisible(true);
        controller.openDocument(filePath);
}

I think the problem is connected with the lack of fonts, but I do not know what to do. I have set org.icepdf.core.awtFontLoading to true but this didn't fix the issue.

Thank you very much for any kind of help!

1

There are 1 answers

3
pcorless On BEST ANSWER

Looks like you are using 7.x so the most up-to-date docs can be found at https://github.com/pcorless/icepdf/wiki. The system property org.icepdf.core.awtFontLoading is no longer applicable as the library is now using FontBox to render embedded fonts and ready system fonts. The older < 7.x property was used when the Open source library could only read some embedded fonts with AWT.

When the file loads a font that isn't embedded or fails to load an embedded font it fall back to font substitution. Font substitution will will kick off a processes to read all the available system fonts and try to find an appropriate font to render. This processes is expensive if the cache isn't used. This will enable the cache:

// read/store the font cache.
FontPropertiesManager.getInstance().loadOrReadSystemFonts();

I haven't looked into suppressing the warning as it is technically valid.