In this article, I will present a simple source code allowing you to digitally sign a PDF document and modify its metadata. I will use the excellent and free port of iText library: iTextSharp that can be downloaded here. You'll need Visual Studio 2005 to be able to open and build the project.
If you don’t know what digital signatures are or how they work, you can go here or here, or simply ask Google
iTextSharp provides a lot of interesting features to create and manipulate PDF documents, but in this article, we will only use digital signature functions. I will also use some function to manipulate PKCS#12 certificates; the only thing you need to know here is that our digital signature will use a private key extracted from a PKCS#12 certificate.
So the first thing you have to do is to install a certificate on your browser. If you don’t have one, you can install the demo certificate from here. Then, extract the PKCS#12 certificate as described below:
- Open Internet Explorer and click on Tools, then Internet Options.
- Go to the 'Content' tab and click 'Certificates'.
- Choose a certificate from the list and click Export.
- Follow the wizard, and when asked, choose to include private key with the extracted certificate.
- Enter a password when prompted (don't give an empty one!!!)
You are now ready to use the code provided in this article. Using the signature example:
- Compile and run the example.
- Browse to the PDF source file you want to sign.
- Browse and choose a destination PDF file.
- Add/modify the PDF meta data if you want.
- Browse to the certificate (the .pfx file) you just extracted and choose it.
- Give the password you used to extract the certificate.
- Add signature information if needed (reason, contact, and location).
- Click the Sign button.
In the debug box, you’ll see the operation’s progress. If everything goes well, open your explorer and browse to the location you entered for the target file, open this file with Adobe Acrobat reader, and your document is signed! =).
Now, how will all this work?
In the source code provided with this article, I wrote a library called PDFSigner. It’s a helper package that uses iTextSharp and doed everything you need for digital signatures. It contains three classes:
Cert: this class is used to hold a certificate and extract the needed information for the signature; the most important method in this class is
processCert (will be explained below).
MetaData: holds the PDF metadata.
PDFSigner: the construction of this class takes a
Cert object and, if needed, a
MetaData object. The most important method here is the
Sign method (will be explained below).
private void processCert()
string alias = null;
pk12 = new PKCS12Store(new FileStream(this.Path, FileMode.Open,
IEnumerator i = pk12.aliases();
alias = ((string)i.Current);
this.akp = pk12.getKey(alias).getKey();
X509CertificateEntry ce = pk12.getCertificateChain(alias);
this.chain = new org.bouncycastle.x509.X509Certificate[ce.Length];
for (int k = 0; k < ce.Length; ++k)
chain[k] = ce[k].getCertificate();
This method reads the certificate and iterates through its entries to find the private key entry, then extracts it. It also construct the certificate's chain if available.
public void Sign(string SigReason, string SigContact, string SigLocation, bool visible)
PdfReader reader = new PdfReader(this.inputPDF);
PdfStamper st = PdfStamper.CreateSignature(reader,
new FileStream(this.outputPDF, FileMode.Create, FileAccess.Write), '\0', null, true);
new FileStream(this.outputPDF, FileMode.Create, FileAccess.Write), '\0');
st.MoreInfo = this.metadata.getMetaData();
st.XmpMetadata = this.metadata.getStreamedMetaData();
PdfSignatureAppearance sap = st.SignatureAppearance;
sap.SetCrypto(this.myCert.Akp, this.myCert.Chain, null,
sap.Reason = SigReason;
sap.Contact = SigContact;
sap.Location = SigLocation;
sap.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 250, 150), 1, null);
This function reads the content of a given PDF, then it uses the read data to create a new PDF using PDFStamper. PDFStamper is a PDF Writer that can sign PDF documents. The signature appearance can be configured so you can add a reason, a contact, and a location attribute to the signature. The
SetCrypto method allows us to sign the document using the private key and chain certificate we extracted from the certificate file. And finally,
SetVisibleSignature is used if you need to add a visible signature to the document. PDFReader, PDFStamper and PdfSignatureAppearance are provided by the iTextSharp library.
Well, that’s all for now I hope that you found my first article useful … If you have any questions or have any problems to build/run this example, don’t hesitate to post a comment.