0

我的 PDFBox 抛出以下错误:字体 AAAAAD+FreeSerifBold 中没有 U+0054 的字形。我在 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。

有谁知道我该如何解决这个问题?

堆栈跟踪:

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)
4

1 回答 1

2

此代码是为您的文件量身定制的。它将默认外观字符串更改为使用不同的字体。另请参阅这个有点相关但更普遍的答案。

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

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();
于 2018-09-25T18:37:34.380 回答