简体   繁体   中英

java PDFBox : remove Signature field and widget

I am trying to remove an existing signature field (PDSignatureField) from a PDF document and its associated widget (PDAnnotationWidget)

I am using the code provided by the following answer:

Does PDFBox allow to remove one field from AcroForm?

I am actually able to retrieve both of the field and the associated widget, and I remove them from the PDDocument (remove the field from AcroForm, remove the widget from page annotations)

As my process needs to use the incremental save, I update the acroFrom and the page COSObjects as follow:

acroForm.getCOSObject().setNeedToBeUpdated(true);

page.getCOSObject().setNeedToBeUpdated(true);

But after saving it, the document still seem to contain the deleted widget... Even though the field seems to be deleted. Can anybody help me out on this? Is it a good way to do it?

EDIT:

This is my problem:

try (PDDocument document = PDDocument.load(file)) {
        for (int i = 0; i < document.getNumberOfPages(); i++) {
            List<PDAnnotation> annotations = document.getPage(i).getAnnotations();
   }
}

Here, the "annotations" list returns a not empty set, although I did remove the annotations before the incremental save.

Thank you

Thank you for the replies. I managed to solve my problem, as mkl said, I needed to create a path of changed objects.

This is what I did:

        COSDictionary dictionary = document.getDocumentCatalog().getCOSObject();
        dictionary.setNeedToBeUpdated(true);
        dictionary = (COSDictionary) dictionary.getDictionaryObject(COSName.ACRO_FORM);
        dictionary.setNeedToBeUpdated(true);
        COSArray array = (COSArray) dictionary.getDictionaryObject(COSName.FIELDS);
        array.setNeedToBeUpdated(true);

        // for each changed page
        COSDictionary item = page.getCOSObject();
                
        while (item.containsKey(COSName.PARENT)) {
            COSBase parent = item.getDictionaryObject(COSName.PARENT);
            if (parent instanceof COSDictionary) {
                item = (COSDictionary) parent;
                item.setNeedToBeUpdated(true);
            }
        }
                
        page.getCOSObject().setNeedToBeUpdated(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM