Click here to Skip to main content
15,844,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get the task to add a digital signature on any PDF file. After hours of googling, I found that there are few libraries that help me to achieve this task easily. (We have our application on dot net 2.0) Below I am listing all the libraries I have found

What I have tried:

  • iTextSharp -- This is too costly to purchase.
  • PDFSharp -- finds only articles but no code to achieve it.
  • SelectPDF -- this adds its own watermark in the pdf.
  • AsposePDF -- this adds its own watermark in the pdf.
  • SpirePDF -- it is working on dot net 4.0 or above

My question is, is there a free library are there to help me with dot net 2.0?
If not free then it should not cost more than $100 a year.
Is there any way to achieve this without the help of libraries?
Posted
Updated 21-Jan-23 5:52am
v2

1 solution

In order to add a digital signature to a PDF, you can use a library such as iText or PDFBox.

iText has some open source capabilities, albeit limited. I think you will probably end up paying some kind of fee.

Here is an example of how you can add a digital signature to a PDF using iText:

import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfSigner;
import com.itextpdf.signatures.BouncyCastleDigest;
import com.itextpdf.signatures.DigestAlgorithms;
import com.itextpdf.signatures.ICrlClient;
import com.itextpdf.signatures.IExternalDigest;
import com.itextpdf.signatures.IExternalSignature;
import com.itextpdf.signatures.PdfSignatureAppearance;
import com.itextpdf.signatures.PrivateKeySignature;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.Certificate;

public class Sign {
    public static final String KEYSTORE = "path/to/keystore.p12";
    public static final char[] PASSWORD = "password".toCharArray();
    public static final String SRC = "path/to/original.pdf";
    public static final String DEST = "path/to/signed.pdf";

    public static void main(String[] args) throws Exception {
        BouncyCastleProvider provider = new BouncyCastleProvider();
        Security.addProvider(provider);

        KeyStore ks = KeyStore.getInstance("PKCS12");
        ks.load(new FileInputStream(KEYSTORE), PASSWORD);
        String alias = (String) ks.aliases().nextElement();
        PrivateKey pk = (PrivateKey) ks.getKey(alias, PASSWORD);
        Certificate[] chain = ks.getCertificateChain(alias);

        PdfReader reader = new PdfReader(SRC);
        PdfSigner signer = new PdfSigner(reader, new FileOutputStream(DEST), new StampingProperties());
        IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256, provider.getName());
        IExternalDigest digest = new BouncyCastleDigest();
        ICrlClient crlClient = null;

        PdfSignatureAppearance appearance = signer.getSignatureAppearance();
        appearance.setReason("Digital signature example");
        appearance.setLocation("Location");
        appearance.setContact("Contact");
        appearance.setSignatureCreator("Creator");
        signer.setFieldName("Signature");
        signer.signDetached(digest, pks, chain, null, null, crlClient, 0, PdfSigner.CryptoStandard.CMS);
    }
}


The keystore, password, source file, and destination file are specified as constants at the top of the class. The keystore is loaded, and the first alias is used to get the private key and certificate chain. The PdfReader class is used to read
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900