1

my PDFBox throws following error: No glyph for U+0054 in font AAAAAD+FreeSerifBold. I found several similar threads on stackoverflow but I couldn't fix my problem by them.

My code is similar to code example:

    public QuoteWorkerPdf() throws IOException {
    // Create PDF with one blank page
    document = PDDocument.load(
            getClass().getResourceAsStream("data/quote_template.pdf"));
    page = (PDPage) document.getDocumentCatalog().getPages().get(0);
    printable = new PDFPrintable(document);

    // get the document catalog
    PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();

    // as there might not be an AcroForm entry a null check is necessary
    if (acroForm != null)
    {
        // Retrieve an individual field and set its value.
        PDTextField field = (PDTextField) acroForm.getField( "q2_quotationPrepared" );
        field.setValue("TextEntry");

        // If a field is nested within the form tree a fully qualified name
        // might be provided to access the field.
        //field = (PDTextField) acroForm.getField( "fieldsContainer.nestedSampleField" );
        //field.setValue("Text Entry");
    }

    // Save and close the filled out form.
    document.save("target/FillFormField.pdf");
}

U+0054 is "T" which is the first letter of the string. For pdf form creation I use www.jotform.com.

Does anybody know how can I solve this?

Stacktrace:

Exception in thread "main" java.lang.IllegalArgumentException: No glyph for U+0054 in font AAAAAD+FreeSerifBold
at org.apache.pdfbox.pdmodel.font.PDCIDFontType2.encode(PDCIDFontType2.java:363)
at org.apache.pdfbox.pdmodel.font.PDType0Font.encode(PDType0Font.java:398)
at org.apache.pdfbox.pdmodel.font.PDFont.encode(PDFont.java:324)
at org.apache.pdfbox.pdmodel.font.PDFont.getStringWidth(PDFont.java:353)
at org.apache.pdfbox.pdmodel.interactive.form.PlainText$Paragraph.getLines(PlainText.java:174)
at org.apache.pdfbox.pdmodel.interactive.form.PlainTextFormatter.format(PlainTextFormatter.java:182)
at org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.insertGeneratedAppearance(AppearanceGeneratorHelper.java:508)
at org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.setAppearanceContent(AppearanceGeneratorHelper.java:364)
at org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.setAppearanceValue(AppearanceGeneratorHelper.java:237)
at org.apache.pdfbox.pdmodel.interactive.form.PDTextField.constructAppearances(PDTextField.java:263)
at org.apache.pdfbox.pdmodel.interactive.form.PDTerminalField.applyChange(PDTerminalField.java:228)
at org.apache.pdfbox.pdmodel.interactive.form.PDTextField.setValue(PDTextField.java:218)
at aaalabel.diefinder.QuoteWorkerPdf.<init>(QuoteWorkerPdf.java:69)
at aaalabel.diefinder.QuoteWorkerPdf.main(QuoteWorkerPdf.java:114)
xMichal
  • 624
  • 2
  • 7
  • 19
  • Can you share the PDF? The problem here is that a font subset is used. The solution IMHO would be to replace the font with a full one, before setting the new value. This answer may help: https://stackoverflow.com/questions/47995062/ – Tilman Hausherr Sep 25 '18 at 17:55
  • pdf is here: https://drive.google.com/open?id=1tLKVdiGuFCKYn7RawqJmfWkP33QwOFMS – xMichal Sep 25 '18 at 18:23

1 Answers1

3

This code is tailored to your file. It changes the default appearance string to use a different font. See also this answer that is somewhat related but more general.

The problem with your input file is that the font used in the field is subsetted, so it doesn't have all glyphs you would expect.

PDDocument doc = PDDocument.load(new File("82667884384374 (1).pdf"));
PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
PDTextField field = (PDTextField) acroForm.getField("q2_quotationPrepared");
COSName helvName = acroForm.getDefaultResources().add(PDType1Font.HELVETICA); // use different font if you want. Do not subset!
field.setDefaultAppearance("/" + helvName.getName() + " 10 Tf 0 g"); // modifies your existing DA string
field.setValue("TextEntry");        
doc.save(new File("82667884384374 (1)-new.pdf"));
doc.close();
Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
  • 2
    To stress the topic of the second paragraph: this problem is **not** a pdfbox issue, it's a shortcoming of the original pdf. – mkl Sep 25 '18 at 20:39