使用IText 7将数字签名添加到PDF

时间:2016-12-15 08:05:21

标签: itext itext7

对于IText 5,添加数字签名相当容易。 其文档的链接是: http://developers.itextpdf.com/examples/security/digital-signatures-white-paper/digital-signatures-chapter-2

有人可以在ITEXT 7中共享指向文档的链接吗? 我尝试了各种方法无济于事。无法在线找到任何链接。我可以取消签名并检查签名,但无法添加。

1 个答案:

答案 0 :(得分:5)

数字签名的端口白皮书7的代码示例可以在iText 7 Java samples github repository子文件夹/publications/signatures/中找到,例如摘自简单的C2_01_SignHelloWorld示例:

public void sign(String src, String dest,
                 Certificate[] chain,
                 PrivateKey pk, String digestAlgorithm, String provider,
                 PdfSigner.CryptoStandard subfilter,
                 String reason, String location)
        throws GeneralSecurityException, IOException {
    // Creating the reader and the signer
    PdfReader reader = new PdfReader(src);
    PdfSigner signer = new PdfSigner(reader, new FileOutputStream(dest), false);
    // Creating the appearance
    PdfSignatureAppearance appearance = signer.getSignatureAppearance()
            .setReason(reason)
            .setLocation(location)
            .setReuseAppearance(false);
    Rectangle rect = new Rectangle(36, 648, 200, 100);
    appearance
            .setPageRect(rect)
            .setPageNumber(1);
    signer.setFieldName("sig");
    // Creating the signature
    IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider);
    IExternalDigest digest = new BouncyCastleDigest();
    signer.signDetached(digest, pks, chain, null, null, null, 0, subfilter);
}