添加另一个签名或将初始签名从 PAdES B-T 升级到 PAdES B-LT 时:
byte[] addLTV(byte[] in)
throws IOException, GeneralSecurityException, java.io.IOException {
IOcspClient ocsp = new OcspClientBouncyCastle(new com.itextpdf.signatures.OCSPVerifier(null, null));
ICrlClient crl = new CrlClientOnline();
PdfReader reader = new PdfReader(new ByteArrayInputStream(in));
ByteArrayOutputStream out = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(out);
PdfDocument pdfDoc = new PdfDocument(reader, writer, new StampingProperties().useAppendMode());
LtvVerification v = new LtvVerification(pdfDoc);
SignatureUtil signatureUtil = new SignatureUtil(pdfDoc);
List<String> names = signatureUtil.getSignatureNames();
String sigName = names.get(names.size() - 1);
PdfPKCS7 pkcs7 = signatureUtil.readSignatureData(sigName);
if (pkcs7.isTsp()) {
v.addVerification(sigName, ocsp, crl, LtvVerification.CertificateOption.WHOLE_CHAIN,
LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.NO);
} else {
for (String name : names) {
v.addVerification(name, ocsp, crl, LtvVerification.CertificateOption.WHOLE_CHAIN,
LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.YES);
}
}
v.merge();
pdfDoc.close();
return out.toByteArray();
}
初始签名变得无效,并出现错误“自应用签名以来文档已被更改或损坏。
经检查,发现初始签名一旦被PdfDocument或PdfSigner读取后就失效了。
String srcFileName = "./BT.pdf";
String outFileName = "./B-LT01.pdf";
PdfDocument document = new PdfDocument(new PdfReader(srcFileName), new PdfWriter(outFileName), new StampingProperties().useAppendMode());
document.close();
该代码适用于其他 PDF 文件。我该如何解决这个问题? 我的pdf文件
正如@KJ 在问题的a comment 中已经暗示的那样,这里的问题是由文档的初始修订中的错误引起的,甚至在第一次签名之前的修订。当验证 PDF 中的签名以及签名后应用的更改时,Adobe Acrobat 会尝试检查是否允许或禁止这些更改;此检查非常敏感,如果初始文档修订中出现某些错误,它会报告无效签名(而不是报告错误的初始文档修订)。
这已经在其他地方讨论过,例如此答案中的堆栈溢出或此答案中的 Adobe 社区论坛。
就像提到的堆栈溢出问题和答案的情况一样,原始版本是由 Aspose 产品生成的(根据“Aspose.PDF for .NET 24.5.1”的文档元数据),所以显然 Aspose尚未有时间修复其软件中产生这些问题的错误。这个 bug 已经为人所知至少 8 年了;也许他们已经爱上它并且不想再放弃了。