Click here to Skip to main content
Click here to Skip to main content

Digital Signatures

By , 11 Sep 2008
 

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

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)

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionmeta tagsmemberKalpana Volety12-Jan-13 2:43 
Is it possible to explicitly define and change the
application meta tag using itextsharp.
 
Kalpana Volety

Generalhelp!!!memberROHIT KUMAR GAUTAM12-Jul-11 23:38 
can u pls tell me can i run this programme in java,if yes pls tell me the details
thnx
regards
rohit
General.CER instead of .PFX certificatesmemberMiroslav Karaffa9-May-11 2:48 
i need to load another type of certificates to be used in source code...can you help me?
GeneralADD IMAGE And languagesmembershin99926-Dec-10 13:05 
Hi, good example!
 
1. Can I add a picture, for example a logo?
 
2. Why can not I write in other languages such as Hebrew?
 
Thanks
Simon
Questionwork with asp.net?memberdavidberlin14-May-10 2:58 
i want use it on asp.net project, does it work?
Questionit is not workingmemberDaulat Kadam2-Sep-09 1:59 
Your sample download zip file is not working.
AnswerRe: it is not workingmembernimeshmca15-Feb-10 2:29 
its working download again
AnswerRe: it is not workingmembernimeshmca15-Feb-10 2:29 
its working download again Laugh | :laugh:
QuestionHow will it works?memberDaulat Kadam2-Sep-09 1:18 
Dear sir,
I am student of mca.
Can u suggest me some solution for building mini project on digital signature?
 
regards,
Daulat
GeneralLocation of the signaurememberju168181-Jul-09 16:41 
Like your example. How do I put the signature on different page of pdf file. Thx
GeneralRe: Location of the signauremembernimeshmca15-Feb-10 2:30 
different page means?
Generalgetting fields.....memberJ .R. Martins4-May-09 5:21 
How can i get all cert fields ????
Questionhow to sign with Datetime sign (TSA server) ?memberRani10928-Oct-08 12:09 
Hi
thank for your sample
 
do you know how to sin with tsa server ?
 
10q
 
Rani
AnswerRe: how to sign with Datetime sign (TSA server) ?membernimeshmca16-Mar-09 22:09 
hi
for adding datetime
you have to add some code
 
like
 

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);

 
in PDFSigner.cs Class
 
thanx
Rose | [Rose] Rose | [Rose] Rose | [Rose]
Laugh | :laugh: Laugh | :laugh: Laugh | :laugh: Laugh | :laugh:

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 12 Sep 2008
Article Copyright 2008 by nimeshmca
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid