Digitally sign a PDF form in C# or VB.NET

PDF forms, Digital signatures, Security
7/8/2014

Import a digital signature and sign a PDF.

Digital signatures can be used for many types of documents where traditional hand written signatures were used in the past. This code sample shows you how you can import a digital signature and use it to sign a PDF document in C# or VB.NET.

C# code sample

1 using (FileStream inFile = new FileStream(@"..\..\../inputdocuments/f1040a.pdf", FileMode.Open, FileAccess.Read)) 2 { 3 // open document with signature fioeld 4 Document document = new Document(inFile); 5 6 // retrieve signature field 7 SignatureField field = document.Fields["SignHere"] as SignatureField; 8 9 // open certicate store. 10 Pkcs12Store ks = null; 11 using (FileStream file = new FileStream(@"..\..\../inputdocuments/ChrisSharp.pfx", FileMode.Open, FileAccess.Read)) 12 { 13 ks = new Pkcs12Store(file, "Sample"); 14 } 15 16 // let the SignatureHandler factory decide which type should be used. 17 SignatureHandler handler = StandardSignatureHandler.Create(ks); 18 19 field.SignatureHandler = handler; 20 21 // set some optional info. 22 field.ContactInfo = "+31 (0)77 4748677"; 23 field.Location = "The Netherlands"; 24 field.Reason = "I fully agree!"; 25 26 // optional code to set image: 27 // enumerate widgets 28 foreach (SignatureWidget widget in field.Widgets) 29 { 30 SignatureAppearance signedAppearance = new SignatureAppearance(); 31 signedAppearance.Style = SignatureAppearanceStyle.ImageAndText; 32 signedAppearance.Bitmap = new System.Drawing.Bitmap(@"..\..\../inputdocuments/logo_pdfkit.gif"); 33 widget.SignedAppearance = signedAppearance; 34 widget.BackgroundColor = System.Drawing.Color.LightPink; 35 } 36 37 // write the modified document to disk 38 // note: signing requires read-write file access 39 using (FileStream outFile = new FileStream(@"..\..\signed.pdf", FileMode.Create, FileAccess.ReadWrite)) 40 { 41 document.Write(outFile); 42 } 43 }

VB .NET

1 Sub Main() 2 3 Using inFile As New FileStream("..\..\../inputdocuments/f1040a.pdf", FileMode.Open, FileAccess.Read) 4 ' open document with signature fioeld 5 Dim document As New Document(inFile) 6 7 ' retrieve signature field 8 Dim field As SignatureField = TryCast(document.Fields("SignHere"), SignatureField) 9 10 ' open certicate store. 11 Dim ks As Pkcs12Store = Nothing 12 Using file As New FileStream("..\..\../inputdocuments/ChrisSharp.pfx", FileMode.Open, FileAccess.Read) 13 ks = New Pkcs12Store(file, "Sample") 14 End Using 15 16 ' let the SignatureHandler factory decide which type should be used. 17 Dim handler As SignatureHandler = StandardSignatureHandler.Create(ks) 18 19 field.SignatureHandler = handler 20 21 ' set some optional info. 22 field.ContactInfo = "+31 (0)77 4748677" 23 field.Location = "The Netherlands" 24 field.Reason = "I fully agree!" 25 26 ' optional code to set image: 27 ' enumerate widgets 28 For Each widget As SignatureWidget In field.Widgets 29 Dim signedAppearance As New SignatureAppearance() 30 signedAppearance.Style = SignatureAppearanceStyle.ImageAndText 31 signedAppearance.Bitmap = New System.Drawing.Bitmap("..\..\../inputdocuments/logo_pdfkit.gif") 32 widget.SignedAppearance = signedAppearance 33 widget.BackgroundColor = System.Drawing.Color.LightPink 34 Next 35 36 ' write the modified document to disk 37 ' note: signing requires read-write file access 38 Using outFile As New FileStream("..\..\signed.pdf", FileMode.Create, FileAccess.ReadWrite) 39 document.Write(outFile) 40 End Using 41 End Using 42 End Sub

The signature field in the PDF now looks like this:

pdf-digital-signature.png