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

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

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.

using (FileStream inFile = new FileStream("f1040a.pdf", FileMode.Open, FileAccess.Read))
{
    // open document with signature fioeld
    Document document = new Document(inFile);

    // retrieve signature field
    SignatureField field = document.Fields["SignHere"] as SignatureField;

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

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

    field.SignatureHandler = handler;

    // set some optional info.
    field.ContactInfo = "+31 (0)77 4748677";
    field.Location = "The Netherlands";
    field.Reason = "I fully agree!";

    // optional code to set image:
    // enumerate widgets 
    foreach (SignatureWidget widget in field.Widgets)
    {
        SignatureAppearance signedAppearance = new SignatureAppearance();
        signedAppearance.Style = SignatureAppearanceStyle.ImageAndText;
        signedAppearance.Bitmap = new System.Drawing.Bitmap("logo.gif");
        widget.SignedAppearance = signedAppearance;
        widget.BackgroundColor = System.Drawing.Color.LightPink;
    }

    // write the modified document to disk
    // note: signing requires read-write file access
    using (FileStream outFile = new FileStream("signed.pdf", FileMode.Create, FileAccess.ReadWrite))
    {
        document.Write(outFile);
    }
}
Sub Main()
  Using inFile As New FileStream("f1040a.pdf", FileMode.Open, FileAccess.Read)
    ' open document with signature fioeld
    Dim document As New Document(inFile)

    ' retrieve signature field
    Dim field As SignatureField = TryCast(document.Fields("SignHere"), SignatureField)

    ' open certicate 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 SignatureHandler factory decide which type should be used.
    Dim handler As SignatureHandler = StandardSignatureHandler.Create(ks)
    field.SignatureHandler = handler

    ' set some optional info.
    field.ContactInfo = "+31 (0)77 4748677"
    field.Location = "The Netherlands"
    field.Reason = "I fully agree!"

    ' optional code to set image:
    ' enumerate widgets 
    For Each widget As SignatureWidget In field.Widgets
      Dim signedAppearance As New SignatureAppearance()
      signedAppearance.Style = SignatureAppearanceStyle.ImageAndText
      signedAppearance.Bitmap = New System.Drawing.Bitmap("..\..\../inputdocuments/logo_pdfkit.gif")
      widget.SignedAppearance = signedAppearance
      widget.BackgroundColor = System.Drawing.Color.LightPink
    Next

    ' write the modified document to disk
    ' note: signing requires read-write file access
    Using outFile As New FileStream("..\..\signed.pdf", FileMode.Create, FileAccess.ReadWrite)
      document.Write(outFile)
    End Using
  End Using
End Sub

The signature field in the PDF now looks like this:

Pdf Digital Signature