Adding Digital Signatures to PDF Documents in C#
Using TX Text Control, adding digital and electronic signatures to documents is an easy task. This article shows how to sign the complete document with a digital certificate and how to sign individual signature fields.

The Adobe PDF document format is used in many types of documents including data with a legal value such as in agreements and contracts. Documents can be digitally signed to ensure that a document has not been altered by a third-party and to verify that the author is the expected person.
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 with TX Text Control, 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.
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.
Typically, the document signing process is a server-side task that gives a safe place to access the certificate with the private key. The following method uses the Server
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);
}
}
When opening the created PDF document in Acrobat Reader, the digital signature is recognized and shows an invisible signature field.
Signing Signature Fields
In the second method, a digital certificate is applied to the signed electronic signature representation using the Signature
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);
}
}
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.
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")
};
Related Posts
ASP.NETASP.NET CoreDigital Signing
Digitally Signing Adobe PDF Documents
Using TX Text Control, there are two ways of signing PDF documents: The whole document and individual signature fields. This article shows how to implement both ways in a .NET 6 command line…
Mining PDFs with Regex in C#: Practical Patterns, Tips, and Ideas
Mining PDFs with Regex in C# can be a powerful technique for extracting information from documents. This article explores practical patterns, tips, and ideas for effectively using regular…
Streamline Data Collection with Embedded Forms in C# .NET
Discover how to enhance your C# .NET applications by embedding forms for data collection. This article explores the benefits of using Text Control's ASP.NET and ASP.NET Core components to create…
Adding QR Codes to PDF Documents in C# .NET
This article explains how to add QR codes to PDF documents with the Text Control .NET Server component in C#. It provides the necessary steps and code snippets for effectively implementing this…
Adding SVG Graphics to PDF Documents in C# .NET
In this article, we will explore how to add SVG graphics to PDF documents using C# .NET. We will use the TX Text Control .NET Server component to demonstrate the process of rendering SVG images in…