我正在处理一个用于通过 Adobe AATL 证书签署 PDF 文件的项目,我正在使用 PDFBOX 库。我下面的代码适用于小于 4MB 的文件,并适用于更大的文件。
注意:下面的代码不会引发异常。但是,由于完整性问题,它签名的文件没有打开。
public void signDetached(PDDocument document, File inFile, SignProperties sigProps)
throws IOException {
int accessPermissions = SigUtils.getMDPPermission(document);
if (accessPermissions == 1) {
throw new IllegalStateException("No changes to the document are permitted due to DocMDP transform parameters dictionary");
}
// create signature dictionary
PDSignature signature = new PDSignature();
signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
signature.setName(sigProps.getName());
signature.setLocation(sigProps.getLocation());
signature.setReason(sigProps.getReason());
// the signing date, needed for valid signature
signature.setSignDate(Calendar.getInstance());
// Optional: certify
if (accessPermissions == 0)
SigUtils.setMDPPermission(document, signature, 2);
SignatureOptions signatureOptions = new SignatureOptions();
// Size can vary, but should be enough for purpose.
signatureOptions.setPreferredSignatureSize(SignatureOptions.DEFAULT_SIGNATURE_SIZE * 3);
// register signature dictionary and sign interface
document.addSignature(signature, this, signatureOptions);
FileOutputStream stream = new FileOutputStream(inFile);
// write incremental (only for signing purpose)
document.saveIncremental(stream);
document.close();
stream.close();
}
更新: PDFBOX 版本是 2.0.8 输入文件大于 4 MB
德玛西亚99
相关分类