x509certificate2 Add Digital Signature to PDF Programmatically
x509certificate2
can be used to get important information about an existing certificate (valid dates, issuer, etc.). IronPDF allows you to digitally sign a PDF using C#. You can create a new document or sign an existing PDF file. It requires just a single line of code, as illustrated in the simple steps below.
How to add Digital PDF Signature in C#
- Install C# library to add digital signature to PDFs
- Use Windows Form to better visualize the process
- Pass digital signature and private password to the
PdfSignature
class - Use the
SignPdfFile
method to open and sign an existing PDF - Check the signed PDF in the directory specified in step 4
Step 1
1. Get IronPDF
First, install IronPDF to your Visual Studio project. Get it in whichever way is easier for you, either from DLL download or on the NuGet website. Access the C# Library in Visual Studio and let's add a signature.
# Product Installation using NuGet
nuget install IronPdf
# Product Installation using NuGet
nuget install IronPdf
How to Tutorial
2. Understanding Digital Signatures
A Digital Signature is like an electronic driver's license or passport that proves your identity. A digital ID typically contains your name and email address, the name of the organization that issued it, a serial number, and an expiration date. Digital IDs are used for certificate security and digital signatures. This will need to be created with Adobe Acrobat for it to work.
3. Digitally Sign a PDF
Now, let's see the steps for creating an x509certificate2
to digitally sign a PDF using C#.
Today, the IronPDF library offers a simple way to apply signatures, saving time and effort with just a single line of code. You can use it for free during development to test your work. Then, decide on your project. Will you be creating a new document or signing an existing PDF?
In the code example below, a C# form is used to allow the user to select their desired PDF, which can receive a digital signature with a single click.
A .pfx file
(Personal Information Exchange Format) should be prepared, which is used to transfer a certificate with the help of a private key.
The SignPdfFile(FileName)
method from the PdfSignature
class is the main method for a digital signature. Simply select the desired file.
using System.Drawing;
using System.Windows.Forms;
using IronPdf;
namespace DigitalSign
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
// Open a dialog to select the desired PDF file
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName; // Display selected PDF path in the textbox
}
}
private void button2_Click(object sender, System.EventArgs e)
{
// Use PdfSignature method to digitally sign the selected PDF
new PdfSignature("Ironpdf.pfx", "123456").SignPdfFile(textBox1.Text);
// Provide user feedback that signing is complete
label3.Text = "Completed!";
label3.BackColor = Color.LightGreen;
label3.ForeColor = Color.Black;
}
}
}
using System.Drawing;
using System.Windows.Forms;
using IronPdf;
namespace DigitalSign
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
// Open a dialog to select the desired PDF file
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName; // Display selected PDF path in the textbox
}
}
private void button2_Click(object sender, System.EventArgs e)
{
// Use PdfSignature method to digitally sign the selected PDF
new PdfSignature("Ironpdf.pfx", "123456").SignPdfFile(textBox1.Text);
// Provide user feedback that signing is complete
label3.Text = "Completed!";
label3.BackColor = Color.LightGreen;
label3.ForeColor = Color.Black;
}
}
}
Imports System.Drawing
Imports System.Windows.Forms
Imports IronPdf
Namespace DigitalSign
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Open a dialog to select the desired PDF file
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
textBox1.Text = openFileDialog1.FileName ' Display selected PDF path in the textbox
End If
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Use PdfSignature method to digitally sign the selected PDF
Call (New PdfSignature("Ironpdf.pfx", "123456")).SignPdfFile(textBox1.Text)
' Provide user feedback that signing is complete
label3.Text = "Completed!"
label3.BackColor = Color.LightGreen
label3.ForeColor = Color.Black
End Sub
End Class
End Namespace
The above C# code represents a Windows Form application, where:
- A button is used to open a file dialog for selecting a PDF file.
- A second button triggers the signing of the selected PDF using a predefined `.pfx` file and password.
- Labels are updated to confirm the completion of the signing process.
4. Review Document Signature
As you can see in the output below, as long as a PDF file is selected and the Import Signature button is clicked, it successfully digitally signs the document. With IronPDF, it only took a single line of code.
IronPDF is the perfect tool for PDF-related tasks using C#. IronPDF offers developers methods to render PDF documents into images and extract text and content from a PDF. Additionally, IronPDF is capable of rendering charts in PDFs, adding barcodes using the IronBarcode library, enhancing security with passwords, watermarking, and even handling PDF forms programmatically.
Library Quick Access
Frequently Asked Questions
What is this software used for?
IronPDF is a C# library that allows developers to manage PDF tasks such as digital signing, redaction, encryption, rendering PDFs into images, extracting text, and more.
How do I install this PDF library?
You can install IronPDF into your Visual Studio project by downloading the DLL or using NuGet with the command `nuget install IronPdf`.
What is a digital signature?
A digital signature is an electronic mechanism that proves the authenticity of a digital message or document, similar to a handwritten signature or a stamped seal.
How can I digitally sign a PDF using this C# library?
To digitally sign a PDF using IronPDF, you need to create an `x509certificate2`, pass the digital signature and private password to the `PdfSignature` class, and use the `SignPdfFile` method to sign the PDF.
What is an x509certificate2?
An `x509certificate2` is a class in C# that provides methods for handling and managing X.509 certificates, which can be used for digital signatures and other certificate-related tasks.
Can I use this PDF tool for free?
Yes, you can use IronPDF for free during development to test its features before deciding on a purchase.
What file format is used for transferring a certificate with a private key?
The `.pfx` file format, also known as Personal Information Exchange Format, is used for transferring a certificate with the help of a private key.
What are the steps to add a digital signature to a PDF programmatically?
The steps include installing the IronPDF library, using Windows Form for visualization, passing the digital signature and password to the `PdfSignature` class, using the `SignPdfFile` method, and checking the signed PDF.
What is the SignPdfFile method?
The `SignPdfFile` method is part of the `PdfSignature` class in IronPDF and is used to digitally sign a PDF file with the provided certificate and private key.