PDFBox:签名时引用现有签名(字段)

PDFBox: reference existing signature (field) when signing

我想弄清楚 PDFBox 是否支持对现有(emtpy)签名表单域进行签名。我检查了 OP 声明的 examples provided however all only seem to add new fields. There was another post

"Pre-existing signature fields are not affected by pdfbox as pdfbox appears not to be able to reference them."

不过这是一年前写的,签名功能上似乎有一些 effort。 那么谁能告诉我是否可以(如果可以的话)引用现有的签名字段?或者也许它是计划好的?

更新 我按照您的建议实现了以下功能:

PDDocumentCatalog docCatalog = doc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField("exampleSignature");
PDSignature signature = ((PDSignatureField)field).getSignature();

但是签名总是null。检查 PDF 规范后,它完全有意义,因为空签名字段永远不会设置签名字典。添加签名字典时,例如必须填写过滤器、内容、字节范围等的值,但只能填写对签名时有意义的值...

从 2.0.4 开始,但已经在 snapshot builds, it is possible to sign existing (empty) signature form fields. (It will not work with 2.0.3, even if you use the updated code example from upcoming 2.0.4, because the library code had several bugs that have been fixed). The example code can be found here 中。示例代码中有两件事是新的:

  • visibleSignatureProperties.buildSignature(); 已移动
  • 已添加调用 signature = findExistingSignature(doc, "Signature1");

它所做的是搜索名为 "Signature1" 的签名字段,如果找到,它会创建一个签名字典(/V 组件)。因为这个签名对象被传递给 doc.addSignature() 调用,PDFBox 将能够检测到父字段已经存在并且不会创建新的。

可以在 PDFBOX-3525 中找到更多详细信息。