简体   繁体   English

java PDFBox:删除签名字段和小部件

[英]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)我正在尝试从 PDF 文档及其关联的小部件 (PDAnnotationWidget) 中删除现有的签名字段 (PDSignatureField)

I am using the code provided by the following answer:我正在使用以下答案提供的代码:

Does PDFBox allow to remove one field from AcroForm? PDFBox 是否允许从 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)我实际上能够检索字段和关联的小部件,并将它们从 PDDocument 中删除(从 AcroForm 中删除字段,从页面注释中删除小部件)

As my process needs to use the incremental save, I update the acroFrom and the page COSObjects as follow:由于我的过程需要使用增量保存,我更新了 acroFrom 和页面 COSObjects 如下:

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.正如 mkl 所说,我设法解决了我的问题,我需要创建一个更改对象的路径。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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