PRODUCT COMPARISONS

PDFsharp Sign PDF documents Digitally vs IronPDF (Code Example)

Published February 19, 2025
Share:

Introduction

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.

Why Are Digital Signatures Important?

Digital signatures ensure document integrity and offer robust security. They are commonly used for contracts, agreements, and other legal documents.

Key benefits:

  • More secure and tamper-proof than traditional signatures.
  • Verified electronically, reducing manual verification efforts.
  • Enable remote signing of documents globally.
  • Provide greater assurance than traditional signatures.

PDFsharp Overview

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.

Key Features

  • Open-source and free under the MIT License.
  • Basic PDF creation and manipulation.
  • Can be extended with external libraries like BouncyCastle for digital signatures.
  • Lacks out-of-the-box support for advanced PDF features such as HTML-to-PDF conversion and complex form handling.

IronPDF Overview

PDFsharp Sign PDF documents Digitally vs IronPDF (Code Example): Figure 1 - image.png

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.

Key Features

  • Commercial license with paid support, and a free trial available.
  • Easy-to-use API with modern features for digital signing and document manipulation.
  • Includes built-in support for HTML-to-PDF format conversion, form handling, and PDF annotations (such as attachment annotations).
  • Seamless integration with advanced functionalities like timestamping, visual signature images, and encryption.

Adding a Digital Signature Programmatically with PDFsharp

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.

Steps to Add a Digital Signature with PDFsharp

  1. Install PDFsharp and BouncyCastle via NuGet.
  2. Create a digital certificate using X509Certificate2.
  3. Sign the PDF with BouncyCastle.

Example Code

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

Output

PDFsharp Sign PDF documents Digitally vs IronPDF (Code Example): Figure 2

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.

Adding a Digital Signature with 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");
    }
}

Output

PDFsharp Sign PDF documents Digitally vs IronPDF (Code Example): Figure 3

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:

  • Open-source under MIT License.
  • Requires external libraries (e.g., BouncyCastle) for advanced features like digital signing. IronPDF:

  • Commercial license with pricing based on developers and deployment instances.
  • Free trial available
  • Free for development use

Conclusion: IronPDF vs PDFsharp for Digital Signatures in C#

When comparing IronPDF and PDFsharp for adding digital signatures to PDFs in C#, both libraries offer distinct advantages depending on your project needs.

  • IronPDF is ideal for developers, whether they be independent freelance software developers, or developers working for a company, seeking a simple, easy-to-use API for applying digital signatures to PDFs, and comes with modern features. Its seamless integration with digital signature application, HTML-to-PDF conversion and other PDF functionalities makes it a great choice for commercial projects that prioritize ease of use and rapid implementation. With paid support and a clear commercial license structure, IronPDF is well-suited for businesses that require a straightforward, reliable solution.
  • PDFsharp excels in basic PDF creation and manipulation but lacks the advanced features and direct support for digital signatures that IronPDF offers. While PDFsharp is open-source and free to use, its API is less intuitive for working with digital signatures compared to IronPDF, and developers may need to employ additional solutions or third-party libraries to handle these features.

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.

Regan Pun

Regan Pun

Software Engineer

 LinkedIn

Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
IronPDF vs PDFsharp PDF-to-Image Conversion (Code Example)
NEXT >
PDFsharp Extract Text From PDF vs IronPDF (Example)