Sign Documents

  • 6 minutes to read

The PDF Document API allows you to apply multiple signatures. A signature is used to authenticate the user's identity and document contents.

VisualSignature

A signature contains validation data (certificate, hash algorithm, timestamp, etc.) and visual parameters (image and signer information). The signature appears in the corresponding form field that defines that signature's location and size.

You can apply a signature to an existing form field or create a new signature field and sign it.

Create a Signature Form Field

The PdfSignatureFieldInfo class allows you specify the signature field's parameters (name, location and rotation angle).

The code sample below shows how to create a new signature field:

//Specify the name and location of the signature field
var signatureFieldInfo = new PdfSignatureFieldInfo(1);
signatureFieldInfo.Name = "SignatureField";
signatureFieldInfo.SignatureBounds = new PdfRectangle(10, 10, 150, 150);
signatureFieldInfo.RotationAngle = PdfAcroFormFieldRotation.Rotate90;

Sign a Form Field

Use the PdfSignatureBuilder class to apply a signature to the form field. The PDF Document API supports PKCS#7 signatures with X.509 certificates. The PdfDocumentSigner class allows you to sign and save a document.

The table below lists API used to sign a form field:

API Description
PdfDocumentSigner Signs the document. Pass the document to sign to the object's constructor.
Pkcs7Signer Creates a PKCS#7 signature. Specify the signature certificate and password in the object's constructor.
PdfSignatureBuilder Applies a PKCS#7 signature to a form field. Specify the signature field's name in the object's constructor to sign this field. If the field with the specified name does not exist, an ArgumentException is thrown.
Use the PdfSignatureFieldInfo object to create a new form field. Refer to the previous section for a code sample.
PdfSignatureBuilder.SetImageData Specifies the signature's image data. You can provide data in the BMP, JPEG, PNG, and EMF+ formats. The image is not displayed if the form field's SignatureBounds property is not specified.
PdfSignatureBuilder.Name Gets or sets the name of the person or authority who signs the document.
PdfSignatureBuilder.Location Gets or sets the location associated with the signer's identity.
PdfSignatureBuilder.ContactInfo Specifies the signer's contact information.
PdfSignatureBuilder.Reason Gets or sets the reason to sign the document (for example, "I Agree").
PdfDocumentSigner.SaveDocument Applies the signature(s) and saves the document.

The code sample below shows how to apply a digital signature to a new and existing form field and sign a document with these signatures:

NOTE

A complete code sample project is available at: How to Apply Multiple Signatures.

using System;
using DevExpress.Pdf;
using System.Diagnostics;
using System.IO;

//Load a document to sign:
using (var signer = new PdfDocumentSigner("Document.pdf"))
{
    //Create a PKCS#7 signature:
    Pkcs7Signer pkcs7Signature = 
    new Pkcs7Signer("Signing Documents//testcert.pfx", "123", 
    PdfHashAlgorithm.SHA256);

    //Apply a signature with a new signature field created before:
    var cooperSignature = 
    new PdfSignatureBuilder(pkcs7Signature, signatureFieldInfo);

    //Specify an image and signer information:
    cooperSignature.SetImageData(File.ReadAllBytes
    ("Signing Documents//JaneCooper.jpg"));
    cooperSignature.Location = "USA";
    cooperSignature.Name = "Jane Cooper";
    cooperSignature.Reason = "Acknowledgement";

    //Apply a signature to an existing form field:
    var santuzzaSignature = 
    new PdfSignatureBuilder(pkcs7Signature, "Sign");

    //Specify an image and signer information:
    santuzzaSignature.SetImageData(File.ReadAllBytes
    ("Signing Documents//SantuzzaValentina.jpg"));
    santuzzaSignature.Location = "Australia";
    santuzzaSignature.Name = "Santuzza Valentina";
    santuzzaSignature.Reason = "I Agree";

    //Add signatures to an array:
    PdfSignatureBuilder[] signatures = { cooperSignature, santuzzaSignature };

    //Sign and save the document:
    signer.SaveDocument("SignedDocument.pdf", signatures);
}

You can create the Pkcs7SignerBase descendant and use a custom PKCS#7 signature builder. Refer to the How to: Use a Custom Signer Class to Apply Signatures to the PDF Document repository for a code sample.

Re-Sign the Form Field

You can remove a signature from a form field and sign it with another signature. Call the PdfDocumentSigner.ClearSignatureField method with the form field's name passed as a parameter. Use the PdfDocumentSigner.GetSignatureFieldNames() to retrieve signed field names.

The code sample below shows how to retrieve signed form field names and apply a new signature to the first field:

using DevExpress.Pdf;
using System.Diagnostics;
using System.IO;

//Load a document to sign
using (var signer = new PdfDocumentSigner("Document.pdf"))
{
    //Create a PKCS#7 signature
    Pkcs7Signer pkcs7Signature = new Pkcs7Signer("Signing Documents//certificate.pfx", "123", PdfHashAlgorithm.SHA256);

    //Retrieve all signed form fields:
    var signedFields = signer.GetSignatureNames(false);

    //Clear the first signature field:
    signer.ClearSignatureField(signedFields[0]);

    //Apply a new signature to this form field
    var santuzzaSignature = new PdfSignatureBuilder(pkcs7Signature, signedFields[0]);

    //Specify image and signer information
    santuzzaSignature.SetImageData(File.ReadAllBytes("Signing Documents//SantuzzaValentina.jpg"));
    santuzzaSignature.Location = "Australia";
    santuzzaSignature.Name = "Santuzza Valentina";
    santuzzaSignature.Reason = "I Agree";

    //Sign and save the document
    signer.SaveDocument("SignedDocument.pdf", santuzzaSignature);
}

Call the PdfDocumentSigner.ClearSignatureFields() method to clear all signature fields.

Add a Signature Timestamp

Use the PdfTsaClient object to create a timestamp. Specify the URI of a timestamp server in the object constructor. You can specify the username and password to log into the server. If the server rejects the timestamp request, a TspValidationException is thrown.

Pass the PdfTsaClient object to the Pkcs7Signer instance as a constructor parameter. The code sample below shows how to create a signature with a timestamp:

ITsaClient tsaClient = 
new PdfTsaClient(new Uri(@"https://freetsa.org/tsr"), PdfHashAlgorithm.SHA256);
Pkcs7Signer pkcs7Signature = 
new Pkcs7Signer("Signing Documents//testcert.pfx", "123", PdfHashAlgorithm.SHA256, tsaClient);

You can create an ITsaClient interface implementation to create a custom timestamp client. Refer to the How to: Use a Custom Timestamp Client to Apply Signatures to the PDF Document repository for a code sample.

TIP

Use the PdfDocumentProcessor to sign a PDF document. Refer to the How to: Use PdfDocumentProcessor to Add a Visual Signature into a PDF article for a code sample. This approach has limitations.

See Also