
Digital signatures provide authenticity, integrity, and non-repudiation for PDF documents, making them essential for legal contracts, financial documents, and other sensitive materials. In this comprehensive tutorial, we'll explore how to digitally sign PDFs in C# using the powerful Spire.PDF for .NET library. We'll cover basic signing, custom appearances, timestamps, and signature fields with detailed code explanations.
Table of Contents:
- .NET Library for Adding Digital Signatures to PDF
- How to Digitally Sign PDFs in C#
- Digitally Sign a PDF with a Certificate
- Customize PDF Signature Appearance
- Add a Timestamp to PDF Digital Signature
- Create a Signable Signature Field in PDF
- Conclusion
- FAQs
.NET Library for Adding Digital Signatures to PDF
Spire.PDF for .NET is a robust library that enables developers to create, read, edit, and convert PDF documents programmatically. For digital signatures, it provides comprehensive support through key classes in the Spire.Pdf.Interactive.DigitalSignatures namespace:
- PdfOrdinarySignatureMaker : The primary class for creating standard PDF signatures
- PdfSignature : Represents a digital signature in a PDF document
- PdfSignatureAppearance : Controls the visual representation of the signature
- PdfPKCS7Formatter : Handles cryptographic formatting including timestamps
- PdfSignatureField : Represents a signature field in an interactive PDF form
Before implementing any signature functionality, ensure you have:
- Spire.PDF for .NET installed via NuGet
- A valid digital certificate (PFX file) with private key access
- Proper permissions to sign documents
How to Digitally Sign PDFs in C#
- Step 1. Install Spire.PDF for .NET.
- Step 2. Use PdfDocument to load the PDF file.
- Step 3. Load the digital certificate file using X509Certificate2.
- Step 4. Use PdfOrdinarySignatureMaker to create and apply the digital signature.
- Step 5. Save the signed PDF with the embedded signature.
Digitally Sign a PDF with a Certificate
To sign a PDF, you need a digital certificate, usually in PFX format. This certificate serves to verify the identity of the signer. Below is a code snippet demonstrating how to sign a PDF using a digital certificate.
Code Example:
using Spire.Pdf;
using Spire.Pdf.Interactive.DigitalSignatures;
using System.Security.Cryptography.X509Certificates;
namespace DigitallySignPdf
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument object to work with PDF files
PdfDocument doc = new PdfDocument();
// Load an existing PDF file from the specified path
doc.LoadFromFile("C:/Users/Administrator/Desktop/Input.pdf");
// Specify the path to the PFX certificate and its password
string filePath = "C:/Users/Administrator/Desktop/certificate.pfx";
string password = "e-iceblue";
// Load the X.509 certificate from the PFX file
X509Certificate2 x509 = new X509Certificate2(filePath, password,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.EphemeralKeySet);
// Create a PdfOrdinarySignatureMaker to handle the signature process using the loaded certificate
PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(doc, x509);
// Create the signature on the PDF document with a specified signature name
signatureMaker.MakeSignature("signature 1");
// Save the signed PDF to a new file
doc.SaveToFile("Signed.pdf");
// Release resources
doc.Dispose();
}
}
}
Key Components Explained:
- PdfDocument : The central class representing the PDF document. It provides methods for loading, manipulating, and saving PDF files.
- X509Certificate2 : The .NET class for handling digital certificates. The key storage flags are crucial:
- MachineKeySet: Stores keys in the machine-level key store
- EphemeralKeySet: Prevents key persistence in memory for better security
- PdfOrdinarySignatureMaker : The workhorse for digital signatures. It:
- Manages the signing process
- Handles cryptographic operations
- Embeds the signature in the PDF
- MakeSignature() : The method that actually applies the signature. The string parameter names the signature, which must be unique within the document.
This basic implementation creates an invisible digital signature. The signature validates the document's integrity but doesn't provide a visual representation.
Output:

Customize PDF Signature Appearance
A digital signature can be customized to include additional information such as the signer's name, contact info, and even a visual signature image. This enhances the visual appeal and informational value of the signature.
Code Example:
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Interactive.DigitalSignatures;
using System.Security.Cryptography.X509Certificates;
namespace CustomSignatureAppearance
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument object to work with PDF files
PdfDocument doc = new PdfDocument();
// Load an existing PDF file from the specified path
doc.LoadFromFile("C:/Users/Administrator/Desktop/Input.pdf");
// Specify the path to the PFX certificate and its password
string filePath = "C:/Users/Administrator/Desktop/certificate.pfx";
string password = "e-iceblue";
// Load the X.509 certificate from the PFX file
X509Certificate2 x509 = new X509Certificate2(filePath, password,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.EphemeralKeySet);
// Create a PdfOrdinarySignatureMaker to handle the signature process using the loaded certificate
PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(doc, x509);
// Get the signature
PdfSignature signature = signatureMaker.Signature;
// Configure the signature properties like the signer's name, contact information, location, and sign reason
signature.Name = "Gary";
signature.ContactInfo = "726349";
signature.Location = "U.S.";
signature.Reason = "This is the final version.";
// Create a signature appearance
PdfSignatureAppearance appearance = new PdfSignatureAppearance(signature);
// Set labels for the signature
appearance.NameLabel = "Signer: ";
appearance.ContactInfoLabel = "Phone: ";
appearance.LocationLabel = "Location: ";
appearance.ReasonLabel = "Reason: ";
// Load an image
PdfImage image = PdfImage.FromFile("C:/Users/Administrator/Desktop/signature.png");
// Set the image as the signature image
appearance.SignatureImage = image;
// Set the graphic mode as SignImageAndSignDetail
appearance.GraphicMode = GraphicMode.SignImageAndSignDetail;
// Get the last page
PdfPageBase page = doc.Pages[doc.Pages.Count - 1];
// Add the signature to a specified location of the page
signatureMaker.MakeSignature("signature 1", page, 54.0f, 330.0f, 280.0f, 90.0f, appearance);
// Save the signed PDF to a new file
doc.SaveToFile("Signed.pdf");
// Release resources
doc.Dispose();
}
}
}
Key Features Explained:
-
PdfSignature Metadata :
- Name: Identifies the signer
- ContactInfo: Provides contact details
- Location: Geographical signing location
- Reason: Purpose of signing
-
PdfSignatureAppearance : Controls visual elements:
- Label customization for metadata fields
- Image integration via SignatureImage property
- Layout controlthrough GraphicMode (options: SignImageOnly, SignDetailOnly, SignImageAndSignDetail)
-
Precise Placement : The extended MakeSignature overload allows specifying:
- Target page
- X/Y coordinates
- Width/Height dimensions
Output:

To enhance the visibility and trustworthiness of your digitally signed PDF when opened in Adobe Reader, you can enable a validation indicator by applying the following method:
signatureMaker.SetAcro6Layers(false);

Add a Timestamp to PDF Digital Signature
Timestamping a digital signature adds an additional layer of security by proving when the document was signed. This is particularly important for long-term validity.
Code Example:
using Spire.Pdf;
using Spire.Pdf.Interactive.DigitalSignatures;
using Spire.Pdf.Security;
using System.Security.Cryptography.X509Certificates;
namespace SignPdfWithTimestamp
{
class Program
{
static void Main(string[] args)
{
// Load PDF document
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("C:/Users/Administrator/Desktop/Input.pdf");
// Specify the path to the PFX certificate and its password
string filePath = "C:/Users/Administrator/Desktop/certificate.pfx";
string password = "e-iceblue";
// Load the X.509 certificate from the PFX file
X509Certificate2 x509 = new X509Certificate2(filePath, password,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.EphemeralKeySet);
// Initialize PDFPKCS#7Formatter
PdfPKCS7Formatter formatter = new PdfPKCS7Formatter(x509, false);
// Set the timestamp service to a public timestamp server
formatter.TimestampService = new TSAHttpService("http://tsa.cesnet.cz:3161/tsa");
// Initialize OCSP service for online certificate status checking
formatter.OCSPService = new OCSPHttpService(null);
// Apply signature
PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(doc, formatter);
signatureMaker.MakeSignature("signature 1");
// Save and cleanup
doc.SaveToFile("SignWithTimeStamp.pdf");
doc.Dispose();
}
}
}
Timestamp Implementation Details:
- PdfPKCS7Formatter : Enhances basic signing with:
- Timestamp support
- OCSP revocation checking
- Advanced cryptographic formatting
- TSAHttpService : Connects to a Time Stamp Authority (TSA) server. Public TSAs include:
- http://timestamp.digicert.com
- http://tsa.cesnet.cz:3161/tsa
- http://timestamp.sectigo.com
- OCSPHttpService : Optional Online Certificate Status Protocol service for real-time certificate validity checking.
Output:

Create a Signable Signature Field in PDF
Digital signature fields allow users to sign PDF documents interactively. This is essential for forms that require user signatures.
Code Example:
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
namespace AddDigitalSignatureFiled
{
class Program
{
static void Main(string[] args)
{
// Initialize a new PdfDocument object
PdfDocument doc = new PdfDocument();
// Load the existing PDF from the specified path
doc.LoadFromFile("C:/Users/Administrator/Desktop/Input.pdf");
// Retrieve the last page of the document
PdfPageBase page = doc.Pages[doc.Pages.Count - 1];
// Create a signature field on the specified page
PdfSignatureField signatureField = new PdfSignatureField(page, "signature");
// Customize the appearance of the signature field
signatureField.BorderWidth = 1.0f;
signatureField.BorderStyle = PdfBorderStyle.Solid;
signatureField.BorderColor = new PdfRGBColor(System.Drawing.Color.Black);
signatureField.HighlightMode = PdfHighlightMode.Outline;
signatureField.Bounds = new RectangleF(54.0f, 350.0f, 200.0f, 100.0f);
// Enable form creation if none exists in the document
doc.AllowCreateForm = (doc.Form == null);
// Add the signature field to the document's form
doc.Form.Fields.Add(signatureField);
// Save the modified document to a new file
doc.SaveToFile("SignatureField.pdf", FileFormat.PDF);
doc.Dispose();
}
}
}
Signature Field Features:
- PdfSignatureField : Represents a signable field in a PDF form with properties for:
- Visual styling (border, color)
- Positioning and sizing
- Interaction behavior
- Form Handling : The code automatically handles PDF form creation if none exists.
- Deferred Signing : Fields can be added now and signed later by end-users or additional processes.
Output:

Conclusion
Digitally signing PDFs with C# using Spire.PDF provides a robust solution for document authentication. Throughout this tutorial, we've explored:
- Basic certificate-based signing
- Custom signature appearances with images and metadata
- Timestamp integration for long-term validation
- Signature fields for form-based workflows
By implementing these techniques, you can enhance document security, compliance, and user trust in your applications. Whether for contracts, legal documents, or internal approvals, Spire.PDF simplifies end-to-end digital signing while maintaining industry standards.
FAQs
Q1: How do I verify a digitally signed PDF?
Spire.PDF provides verification capabilities through the PdfSignature class. You can check the VerifySignature method to validate signatures programmatically. For detailed guide, refer to: Verify Digital Signature in PDF with C#.
Q2: What certificate formats are supported?
Spire.PDF works with standard X.509 certificates, typically in PFX/P12 format for signing as they contain both public and private keys.
Q3: Can I add multiple signatures to a PDF?
Yes, you can add multiple signatures either by creating multiple signature fields or by incrementally signing the document.
Q4: How do I handle certificate expiration?
Using timestamps ensures signatures remain valid after certificate expiration. For long-term validation, consider using LTV (Long-Term Validation) enabled signatures.
Q5: Does Spire.PDF offer additional security options beyond digital signatures?
Yes, Spire.PDF allows you to password-protect your PDF documents and set specific document permissions. These features can be used alongside digital signatures to further enhance security.
Get a Free License
To fully experience the capabilities of Spire.PDF for .NET without any evaluation limitations, you can request a free 30-day trial license.