java - 使用 iText 在每个页面中进行数字签名

标签 java pdf itext

我使用 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/

相关文章:

java - 如何使用 Java 将 HTML 内容转换为 PDF 而不会丢失格式?

java - 使用 iText 在 PDF 上无法正确打印数据

java - LatencyAwarePolicy datastax cassandra 版本 4.6 并在没有本地数据中心的情况下进行连接

java - 在 Web 服务中访问 sqlite 数据库文件的最佳方法

java - 无法 Autowiring Spring 环境

ios - 将 PDF 转换为一页 PDF 原因是 PDF 页面没有相同的页面高度

PDF 谷歌文档查看器

java - 在 iText5 中,如果可能的话,如何将 PdfTemplate 添加到 PdfStamper 中?

java - iText 版本 4.2.1 在 Maven 中央存储库中重定向

java - 使用 Javascript 将 doc/docx 批量转换为 pdf