Search code examples
javaitextitext7

iText 7 - font not recognized


I'm using a ttf file to implement a fixed font for czech letters.

As long as I run the code in my debugger (IntelliJ 2020.3) it works fine. But if I try to run the built jar file in my test project I get the following error:

Exception in thread "main" com.itextpdf.io.IOException: font.is.not.recognized
    at com.itextpdf.io.font.FontProgramFactory.createFont(FontProgramFactory.java:291)
    at com.itextpdf.io.font.FontProgramFactory.createFont(FontProgramFactory.java:214)

The code:

    InputStream in = Template_Dokument.class.getClassLoader().getResourceAsStream("font.ttf");
    byte[] targetArray = null;
    try {
        targetArray = new byte[in.available()];
        in.read(targetArray);
    } catch (IOException e) {
        e.printStackTrace();
    }
    FontProgram fontProgram = FontProgramFactory.createFont(targetArray);
    PdfFont font = PdfFontFactory.createFont(fontProgram, PdfEncodings.IDENTITY_H, false);

The error happens in the method: FontProgramFactory.createFont


Solution

  • Apparently you have to convert the Inputstream (to byte array) in small units of 4Kb.

    https://stackoverflow.com/questions/1264709/convert-inputstream-to-byte-array-in-java
    

    This one solved it for me.