将文本字段添加到数字签名的 PDF 会破坏应用的签名
我正在尝试使用 PDFBOX-3.0.0 将带有值的文本字段添加到数字签名文档中,该文档允许填写表单,但会使应用的签名无效。
代码
try ( InputStream resource = getClass().getResourceAsStream("SignatureVlidationTest.pdf");
PDDocument document = Loader.loadPDF(resource);
) {
PDPage page = document.getPage(0);
page.getCOSObject().setNeedToBeUpdated(true);
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
acroForm.getCOSObject().setNeedToBeUpdated(true);
PDTextField textBox = new PDTextField(acroForm);
textBox.setPartialName("SampleField");
textBox.setMultiline(true);
textBox.setDefaultAppearance("/Helv 14 Tf 1 0 0 rg");
textBox.setReadOnly(true);
textBox.setValue("label");
float pw = page.getMediaBox().getUpperRightX();
float ph = page.getMediaBox().getUpperRightY();
// Specify the widget annotation associated with the field
PDAnnotationWidget widget = textBox.getWidgets().get(0);
PDRectangle rect = new PDRectangle(pw - 110, ph - 25, 110, 25);
widget.setRectangle(rect);
widget.getCOSObject().setNeedToBeUpdated(true);
widget.setPage(page);
widget.setPrinted(true);
widget.setReadOnly(true);
page.getAnnotations().add(widget);
page.getCOSObject().setNeedToBeUpdated(true);
acroForm.getFields().add(textBox);
document.getDocumentCatalog().getCOSObject().setNeedToBeUpdated(true);
Set<COSDictionary> objectsToWrite = new HashSet<>();
objectsToWrite.add(page.getCOSObject());
document.saveIncremental(new FileOutputStream(new File(RESULT_FOLDER, "SignatureVlidationTest-ImageAnnotationAdded.pdf")),objectsToWrite );
}
I am trying to add text field with value using PDFBOX-3.0.0 to digitally signed document which allows form filling but it makes applied signature invalid.
Code
try ( InputStream resource = getClass().getResourceAsStream("SignatureVlidationTest.pdf");
PDDocument document = Loader.loadPDF(resource);
) {
PDPage page = document.getPage(0);
page.getCOSObject().setNeedToBeUpdated(true);
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
acroForm.getCOSObject().setNeedToBeUpdated(true);
PDTextField textBox = new PDTextField(acroForm);
textBox.setPartialName("SampleField");
textBox.setMultiline(true);
textBox.setDefaultAppearance("/Helv 14 Tf 1 0 0 rg");
textBox.setReadOnly(true);
textBox.setValue("label");
float pw = page.getMediaBox().getUpperRightX();
float ph = page.getMediaBox().getUpperRightY();
// Specify the widget annotation associated with the field
PDAnnotationWidget widget = textBox.getWidgets().get(0);
PDRectangle rect = new PDRectangle(pw - 110, ph - 25, 110, 25);
widget.setRectangle(rect);
widget.getCOSObject().setNeedToBeUpdated(true);
widget.setPage(page);
widget.setPrinted(true);
widget.setReadOnly(true);
page.getAnnotations().add(widget);
page.getCOSObject().setNeedToBeUpdated(true);
acroForm.getFields().add(textBox);
document.getDocumentCatalog().getCOSObject().setNeedToBeUpdated(true);
Set<COSDictionary> objectsToWrite = new HashSet<>();
objectsToWrite.add(page.getCOSObject());
document.saveIncremental(new FileOutputStream(new File(RESULT_FOLDER, "SignatureVlidationTest-ImageAnnotationAdded.pdf")),objectsToWrite );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论