USING IRONPDF

How to Create PDF Signatures in .NET

Updated March 10, 2024
Share:

This article will delve into the additional features and capabilities of IronPDF, along with sample source code snippets, to help you get started with integrating digital signing into your PDF file.

Understanding Digital Signatures and IronPDF

Digital signatures serve as electronic counterparts of traditional handwritten signatures, providing an added layer of security and trustworthiness to electronic documents. Digitally signing a PDF document becomes easy with IronPDF, allowing developers to generate, validate, manage and add digital signatures effortlessly.

Setting Up IronPDF

To ensure a smooth integration of the IronPDF library into your .NET project, it is crucial to follow these steps for setup and implementation. These instructions assume you are using Visual Studio as your development environment.

  1. Launch Visual Studio and open the application.
  2. From the "File" menu, select "New Project."
  3. In the dialog box that appears, choose the "Console App" template.
  4. Click on the Next button to proceed.

Please note that these initial steps will lay the foundation for incorporating the IronPDF library seamlessly into your project.

How to Create PDF Signature in .NET, Figure 2: Create a new project in Visual Studio Create a new project in Visual Studio

To proceed with creating your project, provide the necessary information in the designated fields. Enter a suitable name for your project in the "Project name" box, and specify the desired location for the new project in the "Location" field. Once you have entered this information, click the Next button to proceed further.

How to Create PDF Signature in .NET, Figure 3: Configure your new project Configure your new project

To configure the framework for your project, follow these steps. Start by selecting a .NET Framework from the options provided in the drop-down menu. For this scenario, .NET version 6.0 is recommended. Once you have made your selection, proceed by clicking the Create button to initiate the project creation process.

How to Create PDF Signature in .NET, Figure 4: .NET Framework selection .NET Framework selection

To proceed further, you will need to download the required IronPDF library for your solution. To achieve this, you can use the following code in the package manager.

:PackageInstall

As an alternative, the NuGet Package Manager can be used to look for the IronPDF package.

How to Create PDF Signature in .NET, Figure 5: Navigate and install the IronPDF package in NuGet Package Manager UI Navigate and install the IronPDF package in NuGet Package Manager UI

Adding Digital Signatures Programmatically

Let's explore how to add digital signatures to PDF documents using IronPDF.

Creating a New PDF Document

To get started, you need to create a new PDF document using the ChromePdfRenderer. Use the RenderHtmlAsPdf to convert an HTML string into a PDF document. Here's an example:

using IronPdf;

// Create a PDF from a HTML string
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");

// Export to a file or Stream
pdf.SaveAs("output.pdf");
using IronPdf;

// Create a PDF from a HTML string
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");

// Export to a file or Stream
pdf.SaveAs("output.pdf");
Imports IronPdf

' Create a PDF from a HTML string
Private renderer = New ChromePdfRenderer()
Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")

' Export to a file or Stream
pdf.SaveAs("output.pdf")
VB   C#

You can also render an HTML file into a PDF using the RenderHtmlFileAsPdf method. Here's an example:

using IronPdf;

// Create a PDF from an existing HTML file
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf("example.html");

// Export to a file or Stream
pdf.SaveAs("output.pdf");
using IronPdf;

// Create a PDF from an existing HTML file
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf("example.html");

// Export to a file or Stream
pdf.SaveAs("output.pdf");
Imports IronPdf

' Create a PDF from an existing HTML file
Private renderer = New ChromePdfRenderer()
Private pdf = renderer.RenderHtmlFileAsPdf("example.html")

' Export to a file or Stream
pdf.SaveAs("output.pdf")
VB   C#

Loading an Existing PDF Document

If you want to sign PDF documents that are ready-made, IronPDF allows you to load them using the PdfDocument.FromFile method. Here's an example:

using IronPdf;

// Load an existing PDF document
var document = PdfDocument.FromFile("path/to/document.pdf");
using IronPdf;

// Load an existing PDF document
var document = PdfDocument.FromFile("path/to/document.pdf");
Imports IronPdf

' Load an existing PDF document
Private document = PdfDocument.FromFile("path/to/document.pdf")
VB   C#

Generating a Self-Signed Certificate and Sign a PDF

Before adding a digital signature, you'll need a digital certificate. You can easily create open SSL digital certificates using Adobe Acrobat; they can be either PFX digital certificates or P12 digital certificates. Once these certificates are generated you can easily use it to Sign a PDF using IronPDF.

In the code below, you can sign a PDF file Using IronPDF and PFX file advanced electronic signature. This tutorial shows you how to generate certificates using Adobe Acrobat.

using IronPdf;
using IronPdf.Signing;
using IronSoftware.Drawing;
using System;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument doc = renderer.RenderHtmlAsPdf("<h1>foo</h1>");
var signature = new PdfSignature("Iron.pfx", "123456")
{
    SigningContact = "support@ironsoftware.com",
    SigningLocation = "Chicago, USA",
    SigningReason = "To show how to sign a PDF"
};
doc.Sign(signature);
doc.SaveAs("signed.pdf");
using IronPdf;
using IronPdf.Signing;
using IronSoftware.Drawing;
using System;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument doc = renderer.RenderHtmlAsPdf("<h1>foo</h1>");
var signature = new PdfSignature("Iron.pfx", "123456")
{
    SigningContact = "support@ironsoftware.com",
    SigningLocation = "Chicago, USA",
    SigningReason = "To show how to sign a PDF"
};
doc.Sign(signature);
doc.SaveAs("signed.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports IronSoftware.Drawing
Imports System

Private renderer As New ChromePdfRenderer()
Private doc As PdfDocument = renderer.RenderHtmlAsPdf("<h1>foo</h1>")
Private signature = New PdfSignature("Iron.pfx", "123456") With {
	.SigningContact = "support@ironsoftware.com",
	.SigningLocation = "Chicago, USA",
	.SigningReason = "To show how to sign a PDF"
}
doc.Sign(signature)
doc.SaveAs("signed.pdf")
VB   C#

Adding a Digital Signature to the PDF Document

To add electronic signatures to a PDF document, IronPDF provides the PdfSignature method. This method takes the certificate and other necessary parameters to create a digital signature field and embed it in the PDF document. Here's an example of digitally signing a PDF:

using IronPdf.Signing;
using IronSoftware.Drawing;

// Create PdfSignature object
var sig = new PdfSignature("IronSoftware.pfx", "123456");
sig.SignatureImage = new PdfSignatureImage("IronSoftware.png", 0, new CropRectangle(0, 600, 100, 100));
using IronPdf.Signing;
using IronSoftware.Drawing;

// Create PdfSignature object
var sig = new PdfSignature("IronSoftware.pfx", "123456");
sig.SignatureImage = new PdfSignatureImage("IronSoftware.png", 0, new CropRectangle(0, 600, 100, 100));
Imports IronPdf.Signing
Imports IronSoftware.Drawing

' Create PdfSignature object
Private sig = New PdfSignature("IronSoftware.pfx", "123456")
sig.SignatureImage = New PdfSignatureImage("IronSoftware.png", 0, New CropRectangle(0, 600, 100, 100))
VB   C#

Validating Digital Signatures

IronPDF allows you to validate digital signatures in PDF documents programmatically. You can use the VerifyPdfSignatures method to verify that the signatures present in the document are still valid. Here's an example:

using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("annual_census.pdf");
bool isValid = pdf.VerifyPdfSignatures();
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("annual_census.pdf");
bool isValid = pdf.VerifyPdfSignatures();
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("annual_census.pdf")
Private isValid As Boolean = pdf.VerifyPdfSignatures()
VB   C#

Watermarking PDFs

If a digital certificate is not available to sign a PDF document you can always add a watermark digitally in a corner or in an empty space to hold some sort of ownership on a PDF file.
In the source code below, apply a watermark to a PDF file using the ApplyWatermark method.

using IronPdf;
using IronPdf.Editing;

var pdf = PdfDocument.FromFile("invoice.pdf");
pdf.ApplyWatermark("<img src='signature.png'/>", 90, VerticalAlignment.Bottom, HorizontalAlignment.Right);
pdf.SaveAs("official_invoice.pdf");
using IronPdf;
using IronPdf.Editing;

var pdf = PdfDocument.FromFile("invoice.pdf");
pdf.ApplyWatermark("<img src='signature.png'/>", 90, VerticalAlignment.Bottom, HorizontalAlignment.Right);
pdf.SaveAs("official_invoice.pdf");
Imports IronPdf
Imports IronPdf.Editing

Private pdf = PdfDocument.FromFile("invoice.pdf")
pdf.ApplyWatermark("<img src='signature.png'/>", 90, VerticalAlignment.Bottom, HorizontalAlignment.Right)
pdf.SaveAs("official_invoice.pdf")
VB   C#

Licensing and Pricing

IronPDF is an outstanding C# library that offers developers the freedom to use it for development purposes without any cost. You can try the free trial to test all its functionality. Furthermore, it can also be licensed for commercial usage at any given time. The library caters to the needs of various entities, including individual developers, agencies, and multinational organizations, as well as SaaS and OEM redistribution.

With IronPDF, there are diverse licensing options to choose from, ensuring accessibility for all. These licenses encompass a range of benefits such as a 30-day money-back guarantee, one year of comprehensive support and upgrades, validation across development, staging, and production environments, and a perpetual license, which entails a one-time purchase.

For those seeking a budget-friendly option, the Lite package is available at a fixed price of $749, which does not include any recurring expenses. If you require more detailed information or assistance in determining the most suitable license for your needs, please refer to the product licensing page, where you can find additional resources and guidance.

How to Create PDF Signature in .NET, Figure 6: IronPDF Licensing IronPDF Licensing

Conclusion

Implementing digital signatures in PDF documents is a critical aspect of ensuring document integrity, authenticity, and security.

IronPDF, a comprehensive .NET PDF signature library, simplifies this process by providing developers with powerful tools and APIs.

This article explored the features and capabilities of IronPDF, along with sample code snippets, to help you get started with adding digital signatures to your PDF documents programmatically. By leveraging IronPDF's functionalities, you can enhance the security and trustworthiness of your PDF files, enabling secure document exchange in various applications.

Additionally, IronPDF offers developers methods to render PDF documents into images and extract text and content from a PDF. IronPDF is also capable of rendering charts in PDFs, adding barcodes, watermarking, and even handling PDF forms programmatically.

Remember to refer to the official IronPDF documentation and the sample codes provided in this article for further details and comprehensive implementation guidelines. For a detailed tutorial and complete overview of the digital signature library using IronPDF visit the following guidance.

< PREVIOUS
How to Use ChatGPT with IronPDF For C# Developer
NEXT >
How to View PDF Files in VB.NET

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 11,308,499 View Licenses >