PDFBox:U + 0054的AAAAAD + FreeSerifBold字体没有字形

时间:2018-09-25 17:25:53

标签: java pdfbox jotform

我的PDFBox引发以下错误:U + 0054的AAAAAD + FreeSerifBold字体没有字形。 我在stackoverflow上发现了几个类似的线程,但是我无法通过它们解决问题。

我的代码类似于代码示例:

    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是“ T”,它是字符串的第一个字母。 对于pdf表单创建,我使用www.jotform.com。

有人知道我该怎么解决吗?

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)

1 个答案:

答案 0 :(得分:2)

此代码是针对您的文件量身定制的。它将默认外观字符串更改为使用其他字体。另请参阅this answer,虽然有些相关,但比较笼统。

输入文件的问题是该字段中使用的字体是子集,因此它没有您期望的所有字形。

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();