The Adobe PDF document format is used to share all kinds of information including data with a legal value such as in contracts or invoices. To ensure that a document has not been altered by a third-party and to verify that the author is the expected person, documents can be digitally signed.

A digital signature use the concept of hashing - a mathematical function that converts a block of data into a fixed-size string. The result of this function is always identical for this block of data, if the data block has not been altered.

This hash function is used on almost all elements of a document and is then stored inside the document.

When signing Adobe PDF documents, there are two different approaches:

  • Sign the whole document with a digital certificate
  • Sign individual signature fields with a digital certificate(s)

Technically, a visual representation of the signature compared to wet ink is not required to sign a document. But in many cases, an electronic signature that is captured using the TX Text Control Document Viewer, can be signed with a digital certificate.

This article shows how to implement both ways in a .NET 6 command line application.

Signing the Document

The first method shows how to digitally sign the complete document with one certificate. The signatures can be created with PFX, DER Cer or Base64 CER certificate files. Additionally, certificates can be loaded from raw data or selected from the local certificate store. The digital signature and the signed PDF document are cryptographically bound and secured which guarantees that the document has not been tampered.

public static void SignDocument() {
using (ServerTextControl tx = new ServerTextControl()) {
tx.Create();
// add dummy text
tx.Text = "This is a signed document";
SaveSettings saveSettings = new SaveSettings();
// add digital signature to sign complete document
saveSettings.DigitalSignature = new DigitalSignature(
new System.Security.Cryptography.X509Certificates.X509Certificate2(
"textcontrolself.pfx", "123"), null);
// export to PDF
tx.Save("signed_documents.pdf", StreamType.AdobePDF, saveSettings);
}
}
view raw test.cs hosted with ❤ by GitHub

A DigitalSignature TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
DigitalSignature Class
An instance of the DigitalSignature class represents an X.509 certificate which can be used to digitally sign a PDF or PDF/A document.
is created from a PFX certificate and is used to sign the document in the given SaveSettings.DigitalSignature TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
SaveSettings Class
DigitalSignature Property
Specifies a DigitalSignature object, which defines an X.509 certificate.
property.

When opening the created PDF document in Acrobat Reader, the digital signature is recognized and shows an invisible signature field.

Signing a PDF document

Signing Signature Fields

In the second method, a digital certificate is applied to the signed electronic signature representation using the SignatureFields TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
SaveSettings Class
SignatureFields Property
Specifies an array of DigitalSignature objects, each of which defines an X.509 certificate and is associated with a SignatureField in the document.
property. This property specifies an array of DigitalSignature TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
DigitalSignature Class
An instance of the DigitalSignature class represents an X.509 certificate which can be used to digitally sign a PDF or PDF/A document.
objects, each of which defines an X.509 certificate and is associated with a SignatureField TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
SignatureField Class
An instance of the SignatureField class represents a signature field in a Text Control document.
in the document. These certificates can be used to digitally sign a PDF or a PDF/A file.

In the following code snipped, a signature field is added to a new document and an SVG image is added as the visual signature representation. The created digital signature is associated with the signature field and then provided by the SaveSettings to export the document as an Adobe PDF document.

public static void SignFields() {
using (ServerTextControl tx = new ServerTextControl()) {
tx.Create();
// create a signature field
SignatureField signatureField = new SignatureField(
new System.Drawing.Size(2000, 2000), "txsign", 10);
// set image representation
signatureField.Image = new SignatureImage("signature.svg", 0);
// insert the field
tx.SignatureFields.Add(signatureField, -1);
// create a digital signature (for each field, if required)
DigitalSignature digitalSignature = new DigitalSignature(
new System.Security.Cryptography.X509Certificates.X509Certificate2(
"textcontrolself.pfx", "123"), null, "txsign");
// apply the signatures to the SaveSettings
SaveSettings saveSettings = new SaveSettings() {
SignatureFields = new DigitalSignature[] { digitalSignature }
};
// export to PDF
tx.Save("signed_fields.pdf", StreamType.AdobePDF, saveSettings);
}
}
view raw test.cs hosted with ❤ by GitHub

When loading this document into Acrobat Reader, the signature field is shown as a visual representation in the document. If you click on that field, a dialog confirms the validation status. Additionally, the signature field name is shown in the sidebar.

Signing a PDF document

When specifying additional properties to the signature field, those values are displayed in the signature details in Acrobat Reader:

// create a signature field
SignatureField signatureField = new SignatureField(
new System.Drawing.Size(2000, 2000), "txsign", 10) {
Image = new SignatureImage("signature.svg", 0),
SignerData = new SignerData(
"Tim Typer",
"Developer",
"5652 Text Control Dr",
"tim@textcontrol.com",
"Contract signature")
};
view raw test.cs hosted with ❤ by GitHub

Signing a PDF document