Published June 21, 2023
How to Create PDF Signatures in .NET
In today's digital age, the need for secure and trustworthy document exchange is paramount. The ability to add digital signatures to PDF documents ensures the integrity, authenticity, and non-repudiation of the content. IronPDF, a comprehensive .NET core PDF library, provides developers with a robust set of tools to implement pdf signatures programmatically. In this article, we 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 to 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 signature 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.
- Launch Visual Studio and open the application.
- From the "File" menu, select "New Project."
- In the dialog box that appears, choose the "Console App" template.
- 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.
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.
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, we recommend using the well-established .NET version 6.0. Once you have made your selection, proceed by clicking the "Create" button to initiate the project creation process.
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, we can use the NuGet Package Manager to look for the IronPDF package.
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")
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")
Loading an Existing PDF Document
If you want to sign PDF documents that are ready-made, IronPDF allows you to load it 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")
Generating a Self-Signed Certificate and Sign a PDF
Before adding a digital signing, you'll need a digital certificate. You can easily create the open SSL digital certificates using Adobe Acrobat; it 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, we will see how 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")
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 to digitally sign 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))
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()
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 empty space to hold some sort of ownership on a PDF file.
In the source code below, we 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")
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 30-day 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, 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.
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.
In this article, we 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.
Remember to refer to the official IronPDF documentation and the sample codes provided in this article for further details and comprehensive implementation guidelines. For detailed Tutorial and complete overview of Digital signature library using IronPDF visit the following link for guidance.