在iText中,您正在寻找
LtvTimestamp.timestamp(appearance, tsa, signatureName);
也参考JavaDoc文档:
/** * Signs a document with a PAdES-LTV Timestamp. The document is closed at the end. * @param sap the signature appearance * @param tsa the timestamp generator * @param signatureName the signature name or null to have a name generated * automatically * @throws DocumentException * @throws IOException * @throws GeneralSecurityException */
您可能需要阅读第5.4.1节 添加文档安全性存储(DSS)和文档级时间戳 在 PDF文档的数字签名 用于上下文中。
请注意,旧的PDF查看器无法正确识别文档级时间戳,因为它们最近才进入PDF世界,即使用 的PAdES-4 。
要使用PDFBox,您需要一些简单的SignatureInterface实现,如下所示:
public class TimestampSignatureImpl implements SignatureInterface { private TSAClient tsaClient; public TimestampSignatureImpl(TSAClient tsaClient) { super(); this.tsaClient = tsaClient; } @Override public byte[] sign(InputStream paramInputStream) throws IOException { return tsaClient.getTimeStampToken(IOUtils.toByteArray(paramInputStream)); } }
和一些像这样的PDSignature:
PDSignature signature = new PDSignature(); signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); signature.setSubFilter(COSName.getPDFName("ETSI.RFC3161")); signature.setSignDate(Calendar.getInstance());
然后像这样签署你的pdf:
PDDocument pdf = PDDocument.load(inputFile); MessageDigest digest = MessageDigest.getInstance("SHA-256"); TSAClient tsaClient = new TSAClient(new URL("your time stamp authority"), null, null, digest); pdf.addSignature(signature, new TimestampSignatureImpl(tsaClient)); pdf.saveIncremental(new FileOutputStream(outputFile)); pdf.close();
P.S:TSAClient取自PDFBox示例。