PDFbox Could not find font: /Helv

2019-08-01 10:10发布

问题:

I try to add form fields to existing PDF file but the following error appears PDFbox Could not find font: /Helv

My code in Java has the following view:

        PDDocument pdf = PDDocument.load(inputStream);
        PDDocumentCatalog docCatalog = pdf.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();
        PDPage page = pdf.getPage(0);

        PDTextField textBox = new PDTextField(acroForm);
        textBox.setPartialName("SampleField");
        acroForm.getFields().add(textBox);
        PDAnnotationWidget widget = textBox.getWidgets().get(0);
        PDRectangle rect = new PDRectangle(0, 0, 0, 0);
        widget.setRectangle(rect);
        widget.setPage(page);
        widget.setAppearance(acroForm.getFields().get(0).getWidgets().get(0).getAppearance());

        widget.setPrinted(false);

        page.getAnnotations().add(widget);

        acroForm.refreshAppearances();
        acroForm.flatten();
        pdf.save(outputStream);
        pdf.close();

Do you have any ideas why the exception appears?

There is top of stack trace

java.io.IOException: Could not find font: /Helv
at org.apache.pdfbox.pdmodel.interactive.form.PDDefaultAppearanceString.processSetFont(PDDefaultAppearanceString.java:179)
at org.apache.pdfbox.pdmodel.interactive.form.PDDefaultAppearanceString.processOperator(PDDefaultAppearanceString.java:132)
at org.apache.pdfbox.pdmodel.interactive.form.PDDefaultAppearanceString.processAppearanceStringOperators(PDDefaultAppearanceString.java:108)
at org.apache.pdfbox.pdmodel.interactive.form.PDDefaultAppearanceString.<init>(PDDefaultAppearanceString.java:86)
at org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.getDefaultAppearanceString(PDVariableText.java:93)
at org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.<init>(AppearanceGeneratorHelper.java:100)
at org.apache.pdfbox.pdmodel.interactive.form.PDTextField.constructAppearances(PDTextField.java:262)
at org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm.refreshAppearances(PDAcroForm.java:368)
at com.workjam.service.impl.PDFService.fillForm(PDFService.java:85)

Here is the link for PDF: https://drive.google.com/file/d/0B2--NSDOiujoR3hOZFYteUl2UE0/view?usp=sharing

回答1:

Your new text field doesn't have a default appearance, so PDFBox makes one for you (/Helv 0 Tf 0 g).

Solution 1: get it from the field you're using (this will not work with every PDF because you're making several assumptions, i.e. that there is a field and that it is a text field)

textBox.setDefaultAppearance(((PDTextField)acroForm.getFields().get(0)).getDefaultAppearance());

Solution 2: initialize the default resources:

PDResources resources = new PDResources();
resources.put(COSName.getPDFName("Helv"), PDType1Font.HELVETICA);
acroForm.setDefaultResources(resources);

See also the CreateSimpleForm.java example from the source code download.

Update: this has been fixed in 2.0.8, see issue PDFBOX-3943.



回答2:

The cause is a combination of you and the source PDF not providing a default appearance for the text field and PDFBox providing defaults inconsequentially.

The default appearance

According to the specification, each field containing variable text (e.g. your text field) must have a DA default appearance value:

DA string (Required; inheritable) The default appearance string containing a sequence of valid page-content graphics or text state operators that define such properties as the field’s text size and colour.

(ISO 32000-1, Table 222 – Additional entries common to all fields containing variable text)

In addition to parent fields in the field tree, the DA value can also be inherited from the AcroForm dictionary:

DA string (Optional) A document-wide default value for the DA attribute of variable text fields (see 12.7.3.3, “Variable Text”).

(ISO 32000-1, Table 218 – Entries in the interactive form dictionary)

In your PDF

You don't provide a default appearance, and your PDF does not have a default in the AcroForm dictionary.

Thus, strictly speaking, it is not valid at the moment you call acroForm.refreshAppearances(). So PDFBox could reject that call due to missing information.

It works differently, though, as PDFBox provides defaults for certain AcroForm dictionary entries if they are not present, in particular

final String adobeDefaultAppearanceString = "/Helv 0 Tf 0 g ";

// DA entry is required
if (getDefaultAppearance().length() == 0)
{
    setDefaultAppearance(adobeDefaultAppearanceString);
}

Unfortunately, though, PDFBox does not ensure that the font Helv used here is in the default resources unless they are also missing completely.

Solutions

I just saw Tilman wrote an answer here, too. You can find solutions to your issue there.



标签: java pdfbox