Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#
Article

Digital Signatures

Rate me:
Please Sign up or sign in to vote.
3.67/5 (7 votes)
11 Sep 2008CPOL 82.9K   9.3K   46   16
Custom digital signatures using iTextSharp

itextsharp

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 the iText library: iTextSharp, that can be downloaded here. You'll need Visual Studio 2008 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 the digital signature functions. I will also use some functions 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.

Background

I prefer e-signing of an iTextSharp document first, and then doing any change/modification as per requirements.

Using the code

C#
string signerName = 
       PdfPKCS7.GetSubjectFields(this.myCert.Chain[0]).GetField("CN");
PdfTemplate template = st.SignatureAppearance.GetLayer(2);
template.MoveTo(0, 200);
template.LineTo(500, 0);
template.Stroke();
template.BeginText();
BaseFont bf1 = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, 
                                   BaseFont.NOT_EMBEDDED);
template.SetFontAndSize(bf1, 10);
template.SetTextMatrix(1, 1);
template.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "" + 
                         signerName + "", 0, 40, 0);

BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, 
              BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
template.SetFontAndSize(bf, 7);
template.SetTextMatrix(1, 1);
template.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "" + 
         System.DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss zzz") + "", 
         50, 30, 0);

template.SetFontAndSize(bf, 7);
template.SetTextMatrix(1, 1);
template.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Signer:", 0, 25, 0);

template.SetFontAndSize(bf, 7);
template.SetTextMatrix(1, 1);
template.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "" + "CN=" + 
         PdfPKCS7.GetSubjectFields(myCert.Chain[0]).GetField("CN") + "", 
         10, 17, 0);



template.SetFontAndSize(bf, 7);
template.SetTextMatrix(1, 1);
template.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "" + "C=" + 
                         PdfPKCS7.GetSubjectFields(myCert.Chain[0]).GetField("C") + "", 
                         10, 10, 0);

template.EndText();

License

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


Written By
Software Developer (Senior) Business Solution
India India
i m Software Developer

Comments and Discussions

 
Questionwork with asp.net? Pin
davidberlin14-May-10 2:58
davidberlin14-May-10 2:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.