我在这里阅读了关于 saveIncremental 是如何工作的,我的最终结果需要类似于这里,因为我已经设法根据签名字段本身创建具有多个可视化的可见签名(不像在响应中,但是回应对我帮助很大)。为了详细说明标题,我现在的基本任务是在已经签名的文档上创建一个空的签名字段,而不破坏现有的签名。但是,此处的示例不适用于 saveIncremental。我已将以下代码段(已改编)添加到 main 函数的末尾,但没有结果:
acroForm.setSignaturesExist(true);
acroForm.setAppendOnly(true);
acroForm.getCOSObject().setDirect(true);
// ...
COSDictionary pageTreeObject = pdPage.getCOSObject();
while (pageTreeObject != null) {
pageTreeObject.setNeedToBeUpdated(true);
pageTreeObject = (COSDictionary) pageTreeObject.getDictionaryObject(COSName.PARENT);
}
结果文件不包含任何签名字段。我试图将 COSObject.needToBeUpdated(true) 更新为 acroform、signatureField、pddocument、page、widget,但没有结果。签名字段仅在我通常保存时出现。
编辑:我设法添加了一个空的签名字段(在 COSObject.needToBeUpdated 链上编辑的代码),但它破坏了现有的签名。
我想念什么?谢谢!
我的实际代码:
public class CreateEmptySignatureForm {
public static void main(String[] args) throws IOException
{
InputStream resource = new FileInputStream("test-semnat.pdf");
PDDocument document = PDDocument.load(resource);
PDPage page = document.getPage(0);
// Add a new AcroForm and add that to the document
PDAcroForm acroForm = new PDAcroForm(document);
document.getDocumentCatalog().setAcroForm(acroForm);
acroForm.setSignaturesExist(true);
acroForm.setAppendOnly(true);
acroForm.getCOSObject().setDirect(true);
// Create empty signature field, it will get the name "Signature1"
PDSignatureField signatureField = new PDSignatureField(acroForm);
PDAnnotationWidget widget = signatureField.getWidgets().get(0);
PDRectangle rect = new PDRectangle(50, 250, 200, 50);
widget.setRectangle(rect);
widget.getCOSObject().setNeedToBeUpdated(true);
widget.setPage(page);
page.getAnnotations().add(widget);
page.getCOSObject().setNeedToBeUpdated(true);
acroForm.getFields().add(signatureField);
// general updates
document.getDocumentCatalog().getCOSObject().setNeedToBeUpdated(true);
OutputStream os = new FileOutputStream("fooo.pdf");
document.saveIncremental(os);
System.out.println("done");
}
}