6

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.

kz2014
  • 324
  • 1
  • 4
  • 12
  • Have you had a look at the [iText 7 Java samples github repository](https://github.com/itext/i7js-samples)? In the subfolder [/publications/signatures/](https://github.com/itext/i7js-samples/tree/develop/publications/signatures) it contains the whitepaper samples ported to iText 7. – mkl Dec 15 '16 at 10:58
  • Thank you so much. If you can write the answer below, ill mark it :) – kz2014 Dec 15 '16 at 12:26
  • @mkl the link does not exists anymore.... any idea where is it now? – Enrique Molinari Aug 27 '19 at 18:44
  • Found it... https://github.com/itext/i7js-signatures/tree/develop/src/test/java/com/itextpdf/samples/signatures/chapter02 – Enrique Molinari Aug 27 '19 at 19:45
  • @EnriqueMolinari Thanks, I updated the links in my answer below. – mkl Aug 28 '19 at 10:26
  • Related: https://stackoverflow.com/q/37647359/1729265 – mkl Aug 28 '19 at 10:40

1 Answers1

16

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);
}
mkl
  • 90,588
  • 15
  • 125
  • 265
  • Is it possible to add signature to pdf using public key(not private key)? – Banzragch. B Feb 13 '20 at 07:57
  • 2
    @Banzragch.B *"Is it possible to add signature to pdf using public key"* - That would make no sense, would it? Such a signature is meant to ensure that a specific person applied it, no one else. Thus, it must be generated using some *private* data, not merely a public key. – mkl Feb 13 '20 at 09:28
  • 1
    In principle, though, often key pairs can be used inversely. I.e. one can often technically create a "signature" using a public key, but to verify that signature one needs the private key. This merely makes no sense, see my previous comment. – mkl Feb 13 '20 at 09:31
  • 1
    i mean the signature has been signed by private key(other service does it) and i have certificate chain. I just need to add signature to existing pdf and do validition. Do you have any advise? – Banzragch. B Feb 19 '20 at 09:57
  • So you want to add a second signature? Or something different altogether? You probably should make this an actual stack overflow question instead of a series of some comments here. In that question you could explain in more detail what your use case is. – mkl Feb 19 '20 at 10:05
  • thank you for quick reply, i will add question then put the link here. – Banzragch. B Feb 19 '20 at 10:09
  • Can you please see my case here https://stackoverflow.com/questions/60312185/verify-digital-signature-using-certificate-chain-in-java – Banzragch. B Feb 20 '20 at 02:46