PDFBox是否允许从AcroForm中删除一个字段?

时间:2017-11-08 21:42:37

标签: java pdfbox

我正在使用Apache PDFBox 2.0.8并尝试删除一个字段。但无法找到方法,就像我可以用iText做的那样:PdfStamper.getAcroFields().removeField("signature3")

我想要做什么。最初我有3个数字签名的模板PDF。在某些情况下,我只需要2个签名,所以在这种情况下我需要从模板中删除第3个签名。而且我似乎无法用PDFBox做到这一点,我发现接近的事情是扁平化这个领域,但问题是如果一个特定的PDField(不是整个形式,而只是一个字段) - 所有其他签名都失去了它们的功能,看起来就像他们也变得扁平化了。 这是执行它的代码:

PDDocument document = PDDocument.load(file);
PDDocumentCatalog documentCatalog = document.getDocumentCatalog();
PDAcroForm acroForm = documentCatalog.getAcroForm();

List<PDField> flattenList = new ArrayList<>();
for (PDField field : acroForm.getFieldTree()) {
    if (field instanceof PDSignatureField && "signature3".equals(field.getFullyQualifiedName())) {
        flattenList.add(field);
    }
}

acroForm.flatten(flattenList, true);
document.save(dest);        
document.close();

1 个答案:

答案 0 :(得分:2)

正如Tilman在评论中已经提到的那样, PDFBox没有从字段树中删除字段的方法。尽管如此,它还有方法来操作底层的PDF结构,所以可以编写这样的一种方法,例如像这样:

PDField removeField(PDDocument document, String fullFieldName) throws IOException {
    PDDocumentCatalog documentCatalog = document.getDocumentCatalog();
    PDAcroForm acroForm = documentCatalog.getAcroForm();

    if (acroForm == null) {
        System.out.println("No form defined.");
        return null;
    }

    PDField targetField = null;

    for (PDField field : acroForm.getFieldTree()) {
        if (fullFieldName.equals(field.getFullyQualifiedName())) {
            targetField = field;
            break;
        }
    }
    if (targetField == null) {
        System.out.println("Form does not contain field with given name.");
        return null;
    }

    PDNonTerminalField parentField = targetField.getParent();
    if (parentField != null) {
        List<PDField> childFields = parentField.getChildren();
        boolean removed = false;
        for (PDField field : childFields)
        {
            if (field.getCOSObject().equals(targetField.getCOSObject())) {
                removed = childFields.remove(field);
                parentField.setChildren(childFields);
                break;
            }
        }
        if (!removed)
            System.out.println("Inconsistent form definition: Parent field does not reference the target field.");
    } else {
        List<PDField> rootFields = acroForm.getFields();
        boolean removed = false;
        for (PDField field : rootFields)
        {
            if (field.getCOSObject().equals(targetField.getCOSObject())) {
                removed = rootFields.remove(field);
                break;
            }
        }
        if (!removed)
            System.out.println("Inconsistent form definition: Root fields do not include the target field.");
    }

    removeWidgets(targetField);

    return targetField;
}

void removeWidgets(PDField targetField) throws IOException {
    if (targetField instanceof PDTerminalField) {
        List<PDAnnotationWidget> widgets = ((PDTerminalField)targetField).getWidgets();
        for (PDAnnotationWidget widget : widgets) {
            PDPage page = widget.getPage();
            if (page != null) {
                List<PDAnnotation> annotations = page.getAnnotations();
                boolean removed = false;
                for (PDAnnotation annotation : annotations) {
                    if (annotation.getCOSObject().equals(widget.getCOSObject()))
                    {
                        removed = annotations.remove(annotation);
                        break;
                    }
                }
                if (!removed)
                    System.out.println("Inconsistent annotation definition: Page annotations do not include the target widget.");
            } else {
                System.out.println("Widget annotation does not have an associated page; cannot remove widget.");
                // TODO: In this case iterate all pages and try to find and remove widget in all of them
            }
        }
    } else if (targetField instanceof PDNonTerminalField) {
        List<PDField> childFields = ((PDNonTerminalField)targetField).getChildren();
        for (PDField field : childFields)
            removeWidgets(field);
    } else {
        System.out.println("Target field is neither terminal nor non-terminal; cannot remove widgets.");
    }
}

RemoveField辅助方法removeFieldremoveWidgets

可以将此应用于这样的文档和字段:

PDDocument document = PDDocument.load(SOURCE_PDF);

PDField field = removeField(document, "Signature1");
Assert.assertNotNull("Field not found", field);

document.save(TARGET_PDF);        
document.close();

RemoveField test testRemoveInvisibleSignature

PS:我不确定PDFBox实际缓存了多少表单相关信息。因此,我建议不要在同一个文档操作会话中进一步操作表单信息,至少不是没有测试。

PPS:您在removeWidgets辅助方法中找到了TODO。如果方法输出“窗口小部件注释没有关联的页面;无法移除窗口小部件”,则必须添加丢失的代码。