Products Technologies Demo Docs Blog Support Company

How to Create Self-Signed Certificates on Windows to Sign PDFs in .NET C#

This article shows how to create self-signed certificates on Windows to sign PDF documents in .NET C#. The article explains how to create a self-signed certificate using PowerShell and how to sign a PDF document using the TX Text Control .NET Server component.

How to Create Self-Signed Certificates on Windows to Sign PDFs in .NET C#

In document workflows, digital signatures provide authenticity and integrity. When working with PDF documents, a self-signed certificate can be used for testing or internal purposes before obtaining a trusted certificate from a Certificate Authority (CA). This blog post walks you through the process of creating a self-signed certificate using PowerShell, converting it to a PFX file, and using it to digitally sign a PDF.

Using PowerShell to Create a Self-Signed Certificate

Windows provides built-in PowerShell cmdlets to create and manage certificates. Open PowerShell as an administrator and run the following command:

$cert = New-SelfSignedCertificate `
  -Subject "CN=MyTXCert" `
  -CertStoreLocation "Cert:\CurrentUser\My" `
  -KeyExportPolicy Exportable `
  -KeySpec Signature `
  -FriendlyName "My PDF Signing Certificate" `
  -NotAfter (Get-Date).AddYears(5)

The command creates a self-signed certificate and stores it in the Personal certificate store of the current user. Here is a description of the parameters used:

Parameter Description
Subject Common Name (CN) of the certificate.
CertStoreLocation Certificate store location. The default value is "Cert:\CurrentUser\My".
KeyExportPolicy Exportable: The private key can be exported.
KeySpec Key specification.
FriendlyName A readable name for the certificate.
NotAfter Expiration date of the certificate (5 years in the example).

After you run the command, you can view the certificate in the Certificate Manager (certmgr.msc) under Personal > Certificates.

Created Certificate

Exporting the Certificate to a PFX File

To use the certificate for digital signing, export it to a Personal Exchange Format (PFX) file. Run the following command in PowerShell:

$Password = ConvertTo-SecureString -String "123" -Force -AsPlainText

Export-PfxCertificate `
    -Cert "Cert:\CurrentUser\My\$($cert.Thumbprint)" `
    -FilePath "C:\Path\To\Certificate.pfx" `
    -Password $Password

This command exports the certificate to a PFX file. Here is a description of the parameters used:

Parameter Description
ConvertTo-SecureString Securely defines the password for the PFX file.
Export-PfxCertificate Exports the certificate with the private key.
FilePath Path to the PFX file.
Password Password to protect the PFX file.

After you run the command, you can view the PFX file in the specified location.

Signing a PDF with the Certificate

Now that we have a PFX file, we can use it to sign a PDF document. In a .NET-based application, such as one that uses TX Text Control .NET, you can apply the digital signature using the DigitalSignature property.

The following code snippet shows how to sign a PDF document using a PFX file:

using System.Security.Cryptography.X509Certificates;
using TXTextControl;

// Define the certificate password and path
const string password = "123";
const string certificatePath = "certificate.pfx";

var cert = new X509Certificate2(certificatePath, password, X509KeyStorageFlags.Exportable);

// Initialize TXTextControl to create and save a document with the digital signature
using (var tx = new ServerTextControl())
{
    tx.Create();
    tx.Text = "Hello, World!";

    // Prepare the digital signature for the document
    var saveSettings = new SaveSettings
    {
        DigitalSignature = new DigitalSignature(cert, null)
    };

    // Save the document as a PDF with the digital signature
    tx.Save("result.pdf", StreamType.AdobePDF, saveSettings);
}

Conclusion

Creating a self-signed certificate is a simple process using PowerShell. This certificate can be used for testing or internal purposes before obtaining a trusted certificate from a CA. The certificate can be exported to a PFX file and used to digitally sign PDF documents in a .NET-based application.

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.NETAIASP.NET Core

Automating PDF/UA Accessibility with AI: Describing DOCX Documents Using TX…

This article shows how to use TX Text Control together with the OpenAI API to automatically add descriptive texts (alt text and labels) to images, links, and tables in a DOCX. The resulting…


ASP.NETASP.NET CoreJava

Converting Office Open XML (DOCX) to PDF in Java

Learn how to convert Office Open XML (DOCX) documents to PDF in Java using the powerful ServerTextControl library. This guide provides step-by-step instructions and code examples to help you…


ASP.NETASP.NET CoreDS Server

Extending DS Server with Custom Digital Signature APIs

In this article, we will explore how to extend the functionality of DS Server by integrating custom digital signature APIs. We will cover the necessary steps to create a plugin that allows DS…


ASP.NETASP.NET CorePDF

Why PDF/UA and PDF/A-3a Matter: Accessibility, Archiving, and Legal Compliance

It is more important than ever to ensure that documents are accessible, archivable, and legally compliant. PDF/UA and PDF/A-3a are two effective standards for addressing these needs. This article…


ASP.NETASP.NET CoreMarkdown

Convert Markdown to PDF in a Console Application on Linux and Windows

Learn how to convert Markdown files to PDF in a console application on Linux and Windows using TX Text Control .NET Server for ASP.NET. This tutorial provides step-by-step instructions and code…