Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-3751

Flattening a pdf with refreshAppearances set to true causes a NullPointerException

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.0.5
    • Fix Version/s: 2.0.6, 3.0.0 PDFBox
    • Component/s: AcroForm
    • Labels:
      None

      Description

      Flattening a pdf with refreshAppearances set to true causes a NullPointerException. This is because some fields do not have a NormalAppearance set.

      For example:

              try(PDDocument pdfDocument = PDDocument.load(new File("/Users/travis/Desktop/fdpAttachment2.pdf"))) {
      
                  pdfDocument.getDocumentCatalog().getAcroForm().flatten(pdfDocument.getDocumentCatalog().getAcroForm().getFields(), true);
              }
      

      causes:

      Exception in thread "main" java.lang.NullPointerException
      at org.apache.pdfbox.pdmodel.interactive.form.PDButton.updateByValue(PDButton.java:356)
      at org.apache.pdfbox.pdmodel.interactive.form.PDButton.constructAppearances(PDButton.java:285)
      at org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm.refreshAppearances(PDAcroForm.java:348)
      at org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm.flatten(PDAcroForm.java:223)
      at org.kuali.coeus.sys.pdf.FlattenException.main(FlattenException.java:13)

      The workaround is pretty simple. Just make sure every widget without a NormalAppearance has one before calling flatten. I would think pdfbox could do a null check internally when trying to refresh appearance.

              pdfDocument.getDocumentCatalog().getAcroForm().getFields()
                      .stream()
                      .flatMap(f -> f.getWidgets().stream())
                      .filter(w -> w.getAppearance() == null)
                      .forEach(w -> {
                          final PDAppearanceDictionary appearance = new PDAppearanceDictionary(new COSDictionary());
                          appearance.setNormalAppearance(new PDAppearanceEntry(new COSDictionary()));
                          w.setAppearance(appearance);
                      });
      

        Attachments

        1. fdpAttachment2.pdf
          4.03 MB
          Travis Schneeberger

          Activity

            People

            • Assignee:
              msahyoun Maruan Sahyoun
              Reporter:
              leo.herbie Travis Schneeberger
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: