Products
PDFKit.NET 4.0
Created
11/2/2011
Tags
Digital signatures Manipulate PDF

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.

Document document = new Document();
Page page = new Page(PageSize.A4);
document.Pages.Add(page);

//create a new SignatureField
SignatureField signField = new SignatureField("SignatureField");

//create widget, which specifies the location and size.
SignatureWidget widget = new SignatureWidget(45, page.Height - 200, 150, 40);
signField.Widgets.Add(widget);
page.Widgets.Add(widget);
document.Fields.Add(signField);
Dim document As New Document()
Dim page As New Page(PageSize.A4)
document.Pages.Add(page)

'create a new SignatureField
Dim signField As New SignatureField("SignatureField")

'create widget, which specifies the location and size.
Dim widget As New SignatureWidget(45, page.Height - 200, 150, 40)
signField.Widgets.Add(widget)
page.Widgets.Add(widget)
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.

//open certificate store.
Pkcs12Store ks = null;
using (FileStream file = new FileStream(@"..\..\..\InputDocuments\ChrisSharp.pfx", FileMode.Open, FileAccess.Read))
{
    ks = new Pkcs12Store(file, "Sample");
}

//let the Create factory decide which type should be used.
SignatureHandler handler = StandardSignatureHandler.Create(ks);

signField.SignatureHandler = handler;

//the document is signed. The following code will 
//set some optional info.
signField.ContactInfo = "+31 (0)77 4748677";
signField.Location = "The Netherlands";
signField.Reason = "I fully agree!";

//optional code to set image;
SignatureAppearance signedAppearance = new SignatureAppearance();
signedAppearance.Style = SignatureAppearanceStyle.ImageAndText;
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(@"..\..\..\InputDocuments\logo_pdfkit.gif"))
{
    signedAppearance.Bitmap = bmp;
}
widget.SignedAppearance = signedAppearance;
widget.BackgroundColor = System.Drawing.Color.LightPink;

//don't forget: when writing documents which are signed, you also need access to reading!
using (FileStream fs = new FileStream(@"..\..\signature_document.pdf", FileMode.Create, FileAccess.ReadWrite))
{
    document.Write(fs);
}
'open certificate store.
Dim ks As Pkcs12Store = Nothing
Using file As New FileStream("..\..\..\InputDocuments\ChrisSharp.pfx", FileMode.Open, FileAccess.Read)
	ks = New Pkcs12Store(file, "Sample")
End Using

'let the Create factory decide which type should be used.
Dim handler As SignatureHandler = StandardSignatureHandler.Create(ks)

signField.SignatureHandler = handler

'the document is signed. The following code will 
'set some optional info.
signField.ContactInfo = "+31 (0)77 4748677"
signField.Location = "The Netherlands"
signField.Reason = "I fully agree!"

'optional code to set image;
Dim signedAppearance As New SignatureAppearance()
signedAppearance.Style = SignatureAppearanceStyle.ImageAndText
Using bmp As New System.Drawing.Bitmap("..\..\..\InputDocuments\logo_pdfkit.gif")
	signedAppearance.Bitmap = bmp
End Using
widget.SignedAppearance = signedAppearance
widget.BackgroundColor = System.Drawing.Color.LightPink

'don't forget: when writing documents which are signed, you also need access to reading!
Using fs As New FileStream("..\..\signature_document.pdf", FileMode.Create, FileAccess.ReadWrite)
	document.Write(fs)
End Using

Here is the result:

result-create-digitally-signed-pdf.png