Create a new digitally signed PDF document

Digital signatures, Manipulate PDF
11/2/2011

Downloads

Instead of taking an existing PDF document that already has a signature field and signing it, this code sample creates a new PDF document, adds a signature fields, and then digitally signs it.

Create PDF with signature field

The following code creates a new document and adds a page with a signature field.

C# code sample

1 Document document = new Document(); 2 Page page = new Page(PageSize.A4); 3 document.Pages.Add(page); 4 5 //create a new SignatureField 6 SignatureField signField = new SignatureField("SignatureField"); 7 8 //create widget, which specifies the location and size. 9 SignatureWidget widget = new SignatureWidget(45, page.Height - 200, 150, 40); 10 signField.Widgets.Add(widget); 11 page.Widgets.Add(widget); 12 document.Fields.Add(signField);

VB.NET code sample

1 Dim document As New Document() 2 Dim page As New Page(PageSize.A4) 3 document.Pages.Add(page) 4 5 'create a new SignatureField 6 Dim signField As New SignatureField("SignatureField") 7 8 'create widget, which specifies the location and size. 9 Dim widget As New SignatureWidget(45, page.Height - 200, 150, 40) 10 signField.Widgets.Add(widget) 11 page.Widgets.Add(widget) 12 document.Fields.Add(signField)

Here is the result:

Create-digitally-signed-pdf.png

Sign PDF using signature handler

The following code signs the new signature field using a signature handler and a certificate store.

C# code sample

1 //open certificate store. 2 Pkcs12Store ks = null; 3 using (FileStream file = new FileStream(@"..\..\..\InputDocuments\ChrisSharp.pfx", FileMode.Open, FileAccess.Read)) 4 { 5 ks = new Pkcs12Store(file, "Sample"); 6 } 7 8 //let the Create factory decide which type should be used. 9 SignatureHandler handler = StandardSignatureHandler.Create(ks); 10 11 signField.SignatureHandler = handler; 12 13 //the document is signed. The following code will 14 //set some optional info. 15 signField.ContactInfo = "+31 (0)77 4748677"; 16 signField.Location = "The Netherlands"; 17 signField.Reason = "I fully agree!"; 18 19 //optional code to set image; 20 SignatureAppearance signedAppearance = new SignatureAppearance(); 21 signedAppearance.Style = SignatureAppearanceStyle.ImageAndText; 22 using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(@"..\..\..\InputDocuments\logo_pdfkit.gif")) 23 { 24 signedAppearance.Bitmap = bmp; 25 } 26 widget.SignedAppearance = signedAppearance; 27 widget.BackgroundColor = System.Drawing.Color.LightPink; 28 29 //don't forget: when writing documents which are signed, you also need access to reading! 30 using (FileStream fs = new FileStream(@"..\..\signature_document.pdf", FileMode.Create, FileAccess.ReadWrite)) 31 { 32 document.Write(fs); 33 }

VB.NET code sample

1 'open certificate store. 2 Dim ks As Pkcs12Store = Nothing 3 Using file As New FileStream("..\..\..\InputDocuments\ChrisSharp.pfx", FileMode.Open, FileAccess.Read) 4 ks = New Pkcs12Store(file, "Sample") 5 End Using 6 7 'let the Create factory decide which type should be used. 8 Dim handler As SignatureHandler = StandardSignatureHandler.Create(ks) 9 10 signField.SignatureHandler = handler 11 12 'the document is signed. The following code will 13 'set some optional info. 14 signField.ContactInfo = "+31 (0)77 4748677" 15 signField.Location = "The Netherlands" 16 signField.Reason = "I fully agree!" 17 18 'optional code to set image; 19 Dim signedAppearance As New SignatureAppearance() 20 signedAppearance.Style = SignatureAppearanceStyle.ImageAndText 21 Using bmp As New System.Drawing.Bitmap("..\..\..\InputDocuments\logo_pdfkit.gif") 22 signedAppearance.Bitmap = bmp 23 End Using 24 widget.SignedAppearance = signedAppearance 25 widget.BackgroundColor = System.Drawing.Color.LightPink 26 27 'don't forget: when writing documents which are signed, you also need access to reading! 28 Using fs As New FileStream("..\..\signature_document.pdf", FileMode.Create, FileAccess.ReadWrite) 29 document.Write(fs) 30 End Using

Here is the result: