|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Introduction
In this article I will present a simple source code allowing you to digitally sign a PDF document and modify its Meta data.
I will use the excellent and free port of iText library : iTextSharp that can be downloaded here. If you don’t know what are digital signatures or how does 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.
Getting started
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 demo certificate from here.
You are now ready to use the code provided in this article.
Now how all this work?
In the source code provided with this article, I wrote library called PDFSigner, it’s a helper package that use iTextSharp and do everything you need for digital signatures.
processCet method
private void processCert()
{
string alias = null;
PKCS12Store pk12;
//First we'll read the certificate file
pk12 = new PKCS12Store(new FileStream(this.Path, FileMode.Open, FileAccess.Read), this.password.ToCharArray());
//then Iterate throught certificate entries to find the private key entry
IEnumerator i = pk12.aliases();
while (i.MoveNext())
{
alias = ((string)i.Current);
if (pk12.isKeyEntry(alias))
break;
}
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 methode reads the certificate and iterate throught its entries to find the private key entry then extract it.it also construct the certificate's chain if available sign method
public void Sign(string SigReason, string SigContact, string SigLocation, bool visible)
{
PdfReader reader = new PdfReader(this.inputPDF);
//Activate MultiSignatures
PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(this.outputPDF, FileMode.Create, FileAccess.Write), '\0', null, true);
//To disable Multi signatures uncomment this line : every new signature will invalidate older ones !
//PdfStamper st = PdfStamper.CreateSignature(reader, 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, PdfSignatureAppearance.WINCER_SIGNED);
sap.Reason = SigReason;
sap.Contact = SigContact;
sap.Location = SigLocation;
if (visible)
sap.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 250, 150), 1, null);
st.Close();
}
this function reads the content of a given pdf , then it use the read data to create a new PDF using PDFStamper.PDFStamper is a PDF Writer that can sign PDF documents, the signature appearence can be configured so you can add a reason, a contact and a location attributes to the signature. SetCrypto methode allows us to sign the document using the private key and chain certificate we extracted from the certificate file. And finally, the SetVisibleSignature is used if you need to add a visible signature to the document PDFReader, PDFStamper and PdfSignatureAppearance are provided by iTextSharp library Well, that’s all for now :) I hope that you found my first article useful … if you have any question or have any problem to build/run this example don’t hesitate to post a comment.
|
||||||||||||||||||||||