Click here to Skip to main content
11,479,096 members (63,462 online)
Click here to Skip to main content

Digital Signatures

, 11 Sep 2008 CPOL 44.9K 5.7K 44
Rate this:
Please Sign up or sign in to vote.
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 Smile | :) .

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

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)

Share

About the Author

nimeshmca
Software Developer (Senior) Business Solution
India India
i m Software Developer

Comments and Discussions

 
QuestionSet revision label and Add picture as signature Pin
Rui Frazao26-Feb-14 1:22
memberRui Frazao26-Feb-14 1:22 
Questionmeta tags Pin
Kalpana Volety12-Jan-13 3:43
memberKalpana Volety12-Jan-13 3:43 
Generalhelp!!! Pin
ROHIT KUMAR GAUTAM13-Jul-11 0:38
memberROHIT KUMAR GAUTAM13-Jul-11 0:38 
General.CER instead of .PFX certificates Pin
Miroslav Karaffa9-May-11 3:48
memberMiroslav Karaffa9-May-11 3:48 
GeneralADD IMAGE And languages Pin
shin99926-Dec-10 14:05
membershin99926-Dec-10 14:05 
Questionwork with asp.net? Pin
davidberlin14-May-10 3:58
memberdavidberlin14-May-10 3:58 
Questionit is not working Pin
Daulat Kadam2-Sep-09 2:59
memberDaulat Kadam2-Sep-09 2:59 
AnswerRe: it is not working Pin
nimeshmca15-Feb-10 3:29
membernimeshmca15-Feb-10 3:29 
AnswerRe: it is not working Pin
nimeshmca15-Feb-10 3:29
membernimeshmca15-Feb-10 3:29 
QuestionHow will it works? Pin
Daulat Kadam2-Sep-09 2:18
memberDaulat Kadam2-Sep-09 2:18 
GeneralLocation of the signaure Pin
ju168181-Jul-09 17:41
memberju168181-Jul-09 17:41 
GeneralRe: Location of the signaure Pin
nimeshmca15-Feb-10 3:30
membernimeshmca15-Feb-10 3:30 
Generalgetting fields..... Pin
J .R. Martins4-May-09 6:21
memberJ .R. Martins4-May-09 6:21 
Questionhow to sign with Datetime sign (TSA server) ? Pin
Rani10928-Oct-08 13:09
memberRani10928-Oct-08 13:09 
AnswerRe: how to sign with Datetime sign (TSA server) ? Pin
nimeshmca16-Mar-09 23:09
membernimeshmca16-Mar-09 23:09 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 12 Sep 2008
Article Copyright 2008 by nimeshmca
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid