如何解决如何使用Java对带有可见签名和文本的PDF文档进行数字签名
我正在尝试使用Java使用PDFBox Library
对PDF文件进行数字签名,并在其中将可见文本显示在签名字段(如附件图像)上。我尝试了以下代码,并收到以下警告,
WARN org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField- 尚未实现的签名字段的外观生成-您 需要手动生成/更新
以下是代码
PDDocument pDDocument = PDDocument.load(new File("input.pdf"));
PDDocumentCatalog pDDocumentCatalog = pDDocument.getDocumentCatalog();
PDAcroForm pDAcroForm = pDDocumentCatalog.getAcroForm();
PDSignatureField pDSignatureField = (PDSignatureField) pDAcroForm.getField("signatureField");
PDSignature pDSignature = new PDSignature();
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(new FileInputStream("pfxfile.pfx"),"password".toCharArray());
byte[] bytes = IOUtils.toByteArray(new FileInputStream("pfxfile.pfx"));
pDSignature.setContents(bytes);
pDSignatureField.setValue(pDSignature);
FileOutputStream fileOutputStream = new FileOutputStream("output.pdf");
pDDocument.saveIncremental(fileOutputStream);
那么我在做什么错了?还是除了PDFBox之外还有其他解决方案吗?
解决方法
您可以使用iText轻松做到这一点。这是使用iText 7
的有效解决方案。您可以从他们的examples中查看更多信息。
public static void digitalSignature(String sourceFile,String signatureFieldName,String outputFile,Certificate[] certificateChain,PrivateKey privateKey,String digestAlgorithm,String bouncyCastleProvider,PdfSigner.CryptoStandard cryptoStandardSubFilter,String reason,String location)
throws GeneralSecurityException,IOException {
PdfReader pdfReader = new PdfReader(sourceFile);
PdfSigner pdfSigner = new PdfSigner(pdfReader,new FileOutputStream(outputFile),new StampingProperties());
// Create the signature appearance
PdfSignatureAppearance pdfSignatureAppearance = pdfSigner.getSignatureAppearance()
.setReason(reason)
.setLocation(location);
// This name corresponds to the name of the field that already exists in the document.
pdfSigner.setFieldName(signatureFieldName);
pdfSignatureAppearance.setRenderingMode(PdfSignatureAppearance.RenderingMode.NAME_AND_DESCRIPTION);
IExternalSignature iExternalSignature = new PrivateKeySignature(privateKey,digestAlgorithm,bouncyCastleProvider);
IExternalDigest iExternalDigest = new BouncyCastleDigest();
// Sign the document using the detached mode,CMS,or CAdES equivalent.
pdfSigner.signDetached(iExternalDigest,iExternalSignature,certificateChain,null,cryptoStandardSubFilter);
}
public static void main(String[] args) throws IOException,GeneralSecurityException {
BouncyCastleProvider bouncyCastleProvider = new BouncyCastleProvider();
Security.addProvider(bouncyCastleProvider);
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream("path/to/keystore/file"),"password".toCharArray());
String alias = keyStore.aliases().nextElement();
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias,"password".toCharArray());
Certificate[] certificateChain = keyStore.getCertificateChain(alias);
digitalSignature("path/to/input.pdf","Signature Field Name","path/to/output.pdf",privateKey,DigestAlgorithms.SHA256,bouncyCastleProvider.getName(),PdfSigner.CryptoStandard.CMS,"Reason","Location");
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。