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

Removing field / annotation does not work

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Cannot Reproduce
    • 2.0.18
    • None
    • AcroForm
    • None
    • macOS Mojave 10.14.6
    • Patch

    Description

      After upgrading to `2.0.18`, the following code doesn't work for me anymore:

      private void removeField() throws IOException {
          PDAcroForm form = this.field.getAcroForm();
          List<PDField> allFields = form.getFields();
      
          PDAnnotationWidget widget = field.getWidgets().get(0);
          List<PDAnnotation> allAnnotations = widget.getPage().getAnnotations();
      
          System.out.println("BEFORE: Fields size: " + allFields.size() + "; Field to array: " + COSArrayList.converterToCOSArray(allFields));
          for(PDField field : allFields) {
              if (field.getFullyQualifiedName().equals(this.field.getFullyQualifiedName())) {
                  allFields.remove(field);
                  break;
              }
          }
          System.out.println("AFTER: Fields size: " + allFields.size() + "; Field to array: " + COSArrayList.converterToCOSArray(allFields));
      
          System.out.println("BEFORE: Annots size: " + allAnnotations.size() + "; Annots to array: " + COSArrayList.converterToCOSArray(allAnnotations));
          for(PDAnnotation annotation : allAnnotations) {
              if(annotation.getCOSObject().equals(widget.getCOSObject())) {
                  allAnnotations.remove(annotation);
                  break;
              }
          }
          System.out.println("AFTER: Annots size: " + allAnnotations.size() + "; Annots to array: " + COSArrayList.converterToCOSArray(allAnnotations));
      }
      

      For whatever reason, the array in `COSArrayList` is not updating. The internal `ArrayList` is updating, but not the internal `COSArray`. Here is some output from above that might helpful after trying to remove a field from a pdf with a single field.

      // 2.0.17
      BEFORE: Fields size: 1; Field to array: COSArray{[COSObject{10, 0}]}
      AFTER: Fields size: 0; Field to array: COSArray{[]}
      BEFORE: Annots size: 1; Annots to array: COSArray{[COSObject{10, 0}]}
      AFTER: Annots size: 0; Annots to array: COSArray{[]}
      
      // 2.0.18
      BEFORE: Fields size: 1; Field to array: COSArray{[COSObject{10, 0}]}
      AFTER: Fields size: 0; Field to array: COSArray{[COSObject{10, 0}]}
      BEFORE: Annots size: 1; Annots to array: COSArray{[COSObject{10, 0}]}
      AFTER: Annots size: 0; Annots to array: COSArray{[COSObject{10, 0}]}
      
      

      I can definitely attach one of the PDFs I was working with if needed... But this was happening with every PDF I tried it on, so I don't think it's something special about the pdfs I was working with.

      Attachments

        Activity

          People

            msahyoun Maruan Sahyoun
            jezhou Jesse
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: