Products Technologies Demo Docs Blog Support Company

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.

Adding Digital Signatures to PDF Documents in C#

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 ServerTextControl class to create a document in order to export the document as PDF. During the saving process, the digital signature is applied using the SaveSettings.DigitalSignature property.

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 a PDF document

Signing Signature Fields

In the second method, a digital certificate is applied to the signed electronic signature representation using the SignatureFields property. This property specifies an array of DigitalSignature objects, each of which defines an X.509 certificate and is associated with a SignatureField 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);
  }
}

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")
};

Signing a PDF document

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

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…


ASP.NETASP.NET CoreDocument Creation

Why PDF Creation Belongs at the End of the Business Process

This article discusses why placing PDF creation at the end of the business process is important for ensuring accuracy and efficiency. The most scalable systems delay PDF generation until the…


ASP.NETASP.NET CoreForms

Designing the Perfect PDF Form with TX Text Control in .NET C#

Learn how to create and design interactive PDF forms using TX Text Control in .NET C#. This guide covers essential features and best practices for effective form design.


ASP.NETASP.NET CoreMIME

Why Defining MIME Types for PDF/A Attachments Is Essential

The PDF/A standard was created to ensure the long-term reliable archiving of digital documents. An important aspect of the standard involves properly handling embedded files and attachments within…


ASP.NETASP.NET CorePDF

Validate Digital Signatures and the Integrity of PDF Documents in C# .NET

Learn how to validate digital signatures and the integrity of PDF documents using the PDF Validation component from TX Text Control in C# .NET. Ensure the authenticity and compliance of your…

Summarize this blog post with:

Share on this blog post on: