Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
A digital signature is a mathematical technique that verifies an electronic document's authenticity and integrity. It is used as an electronic signature to digitally sign documents in several jurisdictions, ensuring great security and legal validity.
A private key, which is only known to the signer, is used to establish a unique digital signature and connect it to the document when it is digitally signed. The name, email address, and other personal information of the signer are included in this signature. A digitally signed document's authenticity can only be confirmed if the recipient has access to the signer's public key. The legitimacy of the signature is confirmed by decrypting it using public keys.
In this tutorial, we compare how to add digital signatures to PDF documents using PDFSharp and IronPDF. Digital signatures are essential for verifying document authenticity, and PDF files are a popular format for such operations.
PDFsharp is a well-known open-source library for PDF creation and manipulation, while IronPDF is a robust .NET PDF library offering similar features with additional advanced functionalities.
This guide covers signing a PDF document with a private key, and verifying the signature, along with example source codes for both libraries.
Digital signatures ensure document integrity and offer robust security. They are commonly used for contracts, agreements, and other legal documents.
Key benefits:
PDFSharp is an open-source C# library primarily designed for creating and manipulating PDF documents. It is widely used for basic PDF tasks, such as generating simple PDF files, editing existing documents, and rendering graphics. However, its native support for advanced features like digital signatures is limited, and developers often need to rely on third-party libraries, such as BouncyCastle, to integrate such functionalities. PDFsharp is open-source, under the MIT License, making it a good choice for projects where cost and flexibility are a priority.
IronPDF is a robust .NET PDF library that provides a simple and powerful API for generating, editing, and manipulating PDFs. One of its standout features is the ease with which developers can implement digital signatures, which are essential for verifying document authenticity. In addition to digital signatures, IronPDF supports advanced functionalities such as HTML-to-PDF conversion, watermarking, and form handling. It is especially valuable for developers working on commercial projects where rapid implementation and robust features are a priority.
PDFsharp is an open-source library designed for PDF creation and manipulation in C#. However, while it does offer support to add a signature, you’ll need to integrate a third-party tool like BouncyCastle to ensure secure, accurate digital signing of PDF documents.
using System.Security.Cryptography.X509Certificates;
using PdfSharp.Drawing;
using PdfSharp.Pdf.Signatures;
using BouncyCastleSigner;
using PdfSharp.Quality;
class Program
{
static void Main(string[] args)
{
var font = new XFont("Verdana", 10.0, XFontStyleEx.Regular);
var document = new PdfSharp.Pdf.PdfDocument();
var pdfPage = document.AddPage();
var xGraphics = XGraphics.FromPdfPage(pdfPage);
var layoutRectangle = new XRect(0.0, 0.0, pdfPage.Width.Point, pdfPage.Height.Point);
xGraphics.DrawString("Signed sample document", font, XBrushes.Black, layoutRectangle, XStringFormats.TopCenter);
var options = new DigitalSignatureOptions
{
ContactInfo = "John Doe",
Location = "Seattle",
Reason = "License Agreement",
Rectangle = new XRect(36.0, 700.0, 400.0, 50.0),
AppearanceHandler = new SignatureAppearanceHandler()
};
var pdfSignatureHandler = DigitalSignatureHandler.ForDocument(document,
new PdfSharp.Snippets.Pdf.BouncyCastleSigner(GetCertificate(), PdfMessageDigestType.SHA256), options);
document.Save("PdfSharpSignature.pdf");
}
static (X509Certificate2, X509Certificate2Collection) GetCertificate()
{
var certFolder = IOUtility.GetAssetsPath("C:\\Users\\kyess\\AppData\\Roaming\\Adobe\\Acrobat\\DC\\Security");
var pfxFile = Path.Combine(certFolder, "IronSoftware.pfx");
var rawData = File.ReadAllBytes(pfxFile);
// This code is for demonstration only. Do not use password literals for real certificates in source code.
var certificatePassword = "Passw0rd";
var certificate = new X509Certificate2(rawData,
certificatePassword,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
var collection = new X509Certificate2Collection();
collection.Import(rawData, certificatePassword,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
return (certificate, collection);
}
}
using System.Security.Cryptography.X509Certificates;
using PdfSharp.Drawing;
using PdfSharp.Pdf.Signatures;
using BouncyCastleSigner;
using PdfSharp.Quality;
class Program
{
static void Main(string[] args)
{
var font = new XFont("Verdana", 10.0, XFontStyleEx.Regular);
var document = new PdfSharp.Pdf.PdfDocument();
var pdfPage = document.AddPage();
var xGraphics = XGraphics.FromPdfPage(pdfPage);
var layoutRectangle = new XRect(0.0, 0.0, pdfPage.Width.Point, pdfPage.Height.Point);
xGraphics.DrawString("Signed sample document", font, XBrushes.Black, layoutRectangle, XStringFormats.TopCenter);
var options = new DigitalSignatureOptions
{
ContactInfo = "John Doe",
Location = "Seattle",
Reason = "License Agreement",
Rectangle = new XRect(36.0, 700.0, 400.0, 50.0),
AppearanceHandler = new SignatureAppearanceHandler()
};
var pdfSignatureHandler = DigitalSignatureHandler.ForDocument(document,
new PdfSharp.Snippets.Pdf.BouncyCastleSigner(GetCertificate(), PdfMessageDigestType.SHA256), options);
document.Save("PdfSharpSignature.pdf");
}
static (X509Certificate2, X509Certificate2Collection) GetCertificate()
{
var certFolder = IOUtility.GetAssetsPath("C:\\Users\\kyess\\AppData\\Roaming\\Adobe\\Acrobat\\DC\\Security");
var pfxFile = Path.Combine(certFolder, "IronSoftware.pfx");
var rawData = File.ReadAllBytes(pfxFile);
// This code is for demonstration only. Do not use password literals for real certificates in source code.
var certificatePassword = "Passw0rd";
var certificate = new X509Certificate2(rawData,
certificatePassword,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
var collection = new X509Certificate2Collection();
collection.Import(rawData, certificatePassword,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
return (certificate, collection);
}
}
As you can see here, while it was able to create a digital signature field and apply the certificate to our new document, the process is extensive, manual, and not overly efficient to implement when compared to libraries such as IronPDF.
IronPDF provides developers with a concise method for digitally signing PDF documents.
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;
public class Program
{
static void Main(string[] args)
{
// Create PdfSignature object
var sig = new PdfSignature("IronSoftware.pfx", "your-password");
// Add granular information
sig.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256;
sig.TimeStampUrl = "http://timestamp.digicert.com";
sig.SignatureImage = new PdfSignatureImage("IronPdf.png", 0, new Rectangle(150, 100, 200, 200));
// Sign and save PDF document
sig.SignPdfFile("output.pdf");
}
}
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;
public class Program
{
static void Main(string[] args)
{
// Create PdfSignature object
var sig = new PdfSignature("IronSoftware.pfx", "your-password");
// Add granular information
sig.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256;
sig.TimeStampUrl = "http://timestamp.digicert.com";
sig.SignatureImage = new PdfSignatureImage("IronPdf.png", 0, new Rectangle(150, 100, 200, 200));
// Sign and save PDF document
sig.SignPdfFile("output.pdf");
}
}
This code demonstrates how to sign a PDF document using IronPDF's PdfSignature class. The program first creates a PdfSignature object, specifying the location of a .pfx certificate file and its password. It then sets additional signature properties, such as the hash algorithm (SHA256), timestamp URL (http://timestamp.digicert.com), and a custom image for the signature (IronPdf.png).
Finally, the SignPdfFile method is called to apply the digital signature to the PDF document and save it as output.pdf. This process ensures the integrity and authenticity of the PDF by embedding the digital signature along with a timestamp and visual image.
PDFSharp:
Requires external libraries (e.g., BouncyCastle) for advanced features like digital signing. IronPDF:
When comparing IronPDF and PDFsharp for adding digital signatures to PDFs in C#, both libraries offer distinct advantages depending on your project needs.
In summary, IronPDF is the best choice for developers looking for a simple, fast solution for digital signatures and related PDF tasks, especially in commercial environments. PDFsharp is more suited for basic PDF tasks but lacks the same ease of use and feature set for digital signatures, making it more suitable for simpler projects or those with additional customization needs.