我使用 iReport 生成了一个包含 3 页的报告。现在签名只出现在一页中。但我需要使用 iText 签署每个页面。
PdfReader reader = new PdfReader(fullFilePath);
String outputPath = reportPath + randomUUID + fileExtension;
FileOutputStream fout = new FileOutputStream(outputPath);
stp = PdfStamper.createSignature(reader, fout, '\0', null, true);
PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setCrypto(key, chain, null,PdfSignatureAppearance.SELF_SIGNED);
sap.setReason("test");
sap.setLocation("test");
String imagePath = servletContextPath + "/resources/img/signature.png";
File file = new File(imagePath);
byte[] imageByte = new byte[(int) file.length()];
try (FileInputStream fileInputStream = new FileInputStream(file)) {
fileInputStream.read(imageByte);
Image image = Image.getInstance(imageByte);
sap.setImage(image);
}
// comment next line to have an invisible signature
for (int page = 1; page <= reader.getNumberOfPages(); page++){
sap.setVisibleSignature(signatureRectangle, page, null);
}
最佳答案
sap.setVisibleSignature(signatureRectangle, page, null);
设置视觉表示,它不会添加另一个。因此,除了最后一个之外,您在循环中对该方法的所有调用都是徒劳的。
IText 签名创建代码只能为每个签名创建一个视觉表示,虽然根据规范可以有例如多个小部件可视化相同的签名,PDF 查看器可能会拒绝它,因为具有多个可视化的签名的法律值(value)值得怀疑。
编辑:
Adobe,例如,在其 Digital Signature Appearances 中v9白皮书写道:
The location of a signature within a document can have a bearing on its legal meaning. For this reason, signature fields never refer to more than one annotation. If more than one location is associated with a signature, the meaning may become ambiguous.
因此,他们将来可能会完全停止接受带有多个注释(即可视化)的签名。
关于java - 使用 iText 在每个页面中进行数字签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15673274/