Products Technologies Demo Docs Blog Support Company

Signing PDF Documents with Smart Card Certificates in C#

TX Text Control can be used to digitally sign Adobe PDF and PDF/A documents with X.509 certificates. This article shows how to sign documents using smart cards and PIV cards using the certificate store.

Signing PDF Documents with Smart Card Certificates in C#

TX Text Control is able to digitally sign Adobe PDF and PDF/A documents with X.509 certificates. The certificate can be assigned in the SaveSettings class when saving document.

Signing With Certificate Files

TX Text Control can be used to sign PDF documents with a X509Certificate2 that is created from a PFX certificate file. This is typically used for automated processes where a PFX file can be stored on a server and no user interaction is required. The following code shows how to sign a PDF document with a PFX file:

[HttpPost]
public string ExportPDF(string document)
{
    byte[] bPDF;

    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
    {
        tx.Create();
        tx.Load(Convert.FromBase64String(document),
                TXTextControl.BinaryStreamType.InternalUnicodeFormat);

        X509Certificate2 cert = new X509Certificate2
            (Server.MapPath("~/App_Data/textcontrolself.pfx"),
             "yourpassword");

        TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings()
        {
            DigitalSignature = new TXTextControl.DigitalSignature(cert, null)
        };

        // save the document as PDF
        tx.Save(out bPDF, TXTextControl.BinaryStreamType.AdobePDFA, saveSettings);
    }

    // return as Base64 encoded string
    return Convert.ToBase64String(bPDF);
}

Signing With Smart Cards

By accepting a certificate provided by the Windows certificate store, you can sign documents with smart cards, PIV cards or USBs such as YubiKey.

The X509Certificate2UI class provides a UI to select and view X.509 certificates. In the following code, the SelectFromCollection method is used to open a dialog for selecting the certificate. All registered certificates including smart cards are listed in the dialog box.

// get and open certificate store for current user
System.Security.Cryptography.X509Certificates.X509Store store = 
  new System.Security.Cryptography.X509Certificates.X509Store(
    System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser);

store.Open(System.Security.Cryptography.X509Certificates.OpenFlags.ReadOnly);

// retrieve the certificate using the integrated Windows UI
System.Security.Cryptography.X509Certificates.X509Certificate2Collection certificates = 
  System.Security.Cryptography.X509Certificates.X509Certificate2UI.SelectFromCollection(
    store.Certificates, 
    "Choose your certificate", 
    "Please select a certificate that is used by Text Control to sign your PDF document.", 
    System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection);

// apply the first selected certificate
TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings() {
  DigitalSignature = new TXTextControl.DigitalSignature(certificates[0], null)
};

// export the PDF
textControl1.Save("test.pdf", TXTextControl.StreamType.AdobePDFA, saveSettings);

Certificate

After the document has been created and is opened in Acrobat Reader, the valid signatures are shown in the Signatures sidebar:

PDF Acrobat Reader

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Windows Forms

Text Control combines the power of a reporting tool and an easy-to-use WYSIWYG word processor - fully programmable and embeddable in your Windows Forms application. TX Text Control .NET for Windows Forms is a royalty-free, fully programmable rich edit control that offers developers a broad range of word processing features in a reusable component for Visual Studio.

See Windows Forms products

Related Posts

ASP.NETWindows FormsWPF

Extract Text and Data from PDF Documents in C#

TX Text Control can be used to create and edit Adobe PDF documents programmatically. But it is also possible to import PDF documents to read, extract and manipulate them. This article shows…


ASP.NETWindows FormsWPF

Creating PDF Files using TX Text Control .NET in C#

TX Text Control allows developers to create PDF files programmatically using C#. This article shows various ways to create Adobe PDF documents.


ASP.NETJavaScriptWindows Forms

Generating Interactive PDF Forms by Injecting JavaScript

Using TX Text Control, it is possible to export documents with form fields to fillable PDFs. This article shows how to inject JavaScript to add interaction to form fields.


ASP.NETWindows FormsWPF

Form Field Handling in PDF Documents

Since TX Text Control supports form fields, it is possible to either export form fields in the PDF document or to flatten the form fields to export text only without the field functionality. This…


ASP.NETWindows FormsWPF

Creating ZUGFeRD Compliant PDF Invoices in C#

ZUGFeRD / Factur-X documents can be created and extracted using TX Text Control X19. This article shows how to create a valid ZUGFeRD compliant invoice PDF document from scratch.