Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Digital Signature to a PDF using IText 7

Tags:

itext

itext7

For IText 5, adding digital signature was fairly easy. The link for its documentation is: http://developers.itextpdf.com/examples/security/digital-signatures-white-paper/digital-signatures-chapter-2

Can someone share the link to documentation for doing so in ITEXT 7? I have tried various ways to no avail. Could not find any links online. I can unsign and check signature, but can't add it.

like image 967
kz2014 Avatar asked Dec 15 '16 08:12

kz2014


People also ask

Can I add a digital signature to a PDF?

Steps to sign a PDF. Open the PDF document or form that you want to sign. Click the Sign icon in the toolbar. Alternatively, you can choose Tools > Fill & Sign or choose Fill & Sign from the right pane.

How do I add a DSC to all pages of a PDF?

After you having selected a signature, click on the position where you would like to place the signature, then right-click on the newly added signature, choose Place on Multiple Pages…. Option in pop-up context menu and set page range, click OK to apply.

How do I add a signature to a PDF in Java?

Add Digital Signature to a PDF using Java PDF for Java. Create an instance of the Document class and initialize it with the PDF document's path. Initialize the PdfFileSignature class and pass to it the Document object. Create an instance of PKCS7 class and initialize it with a certificate's path and the password.


1 Answers

Ports of the Digital Signatures Whitepaper code examples to iText 7 can be found in the iText 7 Java signature samples github repository test sources package com.itextpdf.samples.signatures, e.g. an excerpt from the simple C2_01_SignHelloWorld example:

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);
}
like image 84
mkl Avatar answered Sep 23 '22 15:09

mkl