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

NullPointerException when signing a PDF with AcroForm and no fields

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.0.4, 3.0.0 PDFBox
    • 2.0.5, 3.0.0 PDFBox
    • AcroForm
    • None

    Description

      This is related to PDFBOX-1719. The workaround made there calls PDAcroForm.setFields with an empty list when the acro form has no fields:

      PDDocument.java (Line 286)
      List<PDField> fields = acroForm.getFields();
      if (fields == null)
      {
          fields = new ArrayList<PDField>();
          acroForm.setFields(fields);
      }
      else
      {
          COSArray fieldArray = (COSArray) acroForm.getCOSObject().getDictionaryObject(COSName.FIELDS);
          fieldArray.setNeedToBeUpdated(true);
      }
      

      This doesn't work anymore because PDAcroFrom.getFields never returns null in the current version, instead it returns an empty list and a NPE raises in line 295.

      The source of the problem seems to be that the PDAcroForm's dictionary's COSName.FIELDS field contains null instead of an empty array. An empty array is implicitly created when calling PDAcroForm.setFields with an empty list. The PDAcroForm(PDDocument) constructor also sets an empty COSArray for the field. You could also do this in the second constrcutor, that would fix the error:

      PDAcroForm.java
      public PDAcroForm(PDDocument doc, COSDictionary form)
      {
          document = doc;
          dictionary = form;
          if (dictionary.getItem(COSName.FIELDS) == null)
          {
            dictionary.setItem(COSName.FIELDS, new COSArray());
          }
      }
      

      Attachments

        Issue Links

          Activity

            People

              tilman Tilman Hausherr
              morgov Moritz Orth
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: