繁体   English   中英

如何使用 Java 对具有可见签名和文本的 PDF 文档进行数字签名

[英]How to digitally sign a PDF document with visible signature and text using Java

我正在尝试使用PDFBox Library对 PDF 文件进行数字签名,该文件使用 Java 和可见文本,以显示在签名字段(如附加图像)上。 我尝试了以下代码,并收到以下警告,

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的工作解决方案。 您可以从他们的示例中查看更多信息。

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, null, null, 0, 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", certificateChain, privateKey,
            DigestAlgorithms.SHA256, bouncyCastleProvider.getName(), PdfSigner.CryptoStandard.CMS,
            "Reason", "Location");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM