Sign a PDF Document in C# and VB .NET
Developers commonly ask how they can programmatically add a signature to a PDF document using IronPDF and C# or VB.NET
Commonly this means different things to different developers:
- To add a graphical handwritten signature image to an existing Pdf from an image file
- To cryptographically sign a PDF document to ensure it can not been tampered with
- To cryptographically sign a PDF document h AND add a human handwritten signature.
You can download a file project from this link.
1 - Stamping a Handwritten Signature onto a PDF Page
This method will stamp a signature PNG onto an existing PDF page. It could be used for a company stamp or a hand written signature. Opacity is Supported.
The HtmlStamp class supports stamping of any HTML content, which may be resized and positioned on the page using: Width, Height, Top, Bottom, Left and Right in Pixels
//PDFs can be edited or amended by stamping new HTML content into the foreground or background.
using IronPdf;
using IronPdf.Editing;
// open an existing PDF document or create a new one
using PdfDocument Pdf = PdfDocument.FromFile(@"C:\Path\To\UnSigned.pdf");
var SignatureStamp = new HtmlStamp() { Html = "<img src='signature.png' />", Width = 150, Height = 50, Bottom = 300, Left=85, ZIndex = HtmlStamp.StampLayer.OnTopOfExistingPDFContent };
Pdf.StampHTML(SignatureStamp,1);
Pdf.SaveAs(@"C:\Path\To\Signed.pdf");
//PDFs can be edited or amended by stamping new HTML content into the foreground or background.
using IronPdf;
using IronPdf.Editing;
// open an existing PDF document or create a new one
using PdfDocument Pdf = PdfDocument.FromFile(@"C:\Path\To\UnSigned.pdf");
var SignatureStamp = new HtmlStamp() { Html = "<img src='signature.png' />", Width = 150, Height = 50, Bottom = 300, Left=85, ZIndex = HtmlStamp.StampLayer.OnTopOfExistingPDFContent };
Pdf.StampHTML(SignatureStamp,1);
Pdf.SaveAs(@"C:\Path\To\Signed.pdf");
'PDFs can be edited or amended by stamping new HTML content into the foreground or background.
Imports IronPdf
Imports IronPdf.Editing
' open an existing PDF document or create a new one
Private PdfDocument As using
Private SignatureStamp = New HtmlStamp() With {
.Html = "<img src='signature.png' />",
.Width = 150,
.Height = 50,
.Bottom = 300,
.Left=85,
.ZIndex = HtmlStamp.StampLayer.OnTopOfExistingPDFContent
}
Pdf.StampHTML(SignatureStamp,1)
Pdf.SaveAs("C:\Path\To\Signed.pdf")
2 - Cryptographically signing a PDF document
The following method cryptographically signs a PDF using a .pfx and .p12 X509Certificate2 digital certificate which can be created easily with Adobe Reader.
Read: https://helpx.adobe.com/acrobat/using/digital-ids.html
using IronPdf;
// 123456 below represents the signature password
new IronPdf.PdfSignature("CertificateFile.p12", "123456").SignPdfFile("UnSecure.pdf");
using IronPdf;
// 123456 below represents the signature password
new IronPdf.PdfSignature("CertificateFile.p12", "123456").SignPdfFile("UnSecure.pdf");
Imports IronPdf
' 123456 below represents the signature password
Call (New IronPdf.PdfSignature("CertificateFile.p12", "123456")).SignPdfFile("UnSecure.pdf")
3 - Cryptographically signing a PDF document and do so with a Hand Written signature.
This more advanced example allows the .pfx / .p12 X509Certificate2 digital id signing method to be combined with a scan of a handwritten signature.
var Signature = new IronPdf.PdfSignature("Iron.pfx", "123456");
// Step 3. Optional signing options and a handwritten Signature graphic
Signature.SigningContact = "support@ironsoftware.com";
Signature.SigningLocation = "Chicago, USA";
Signature.SigningReason = "To show how to sign a PDF";
Signature.LoadSignatureImageFromFile("handwriting.png");
Signature.SignPdfFile("UnSecure.pdf");
var Signature = new IronPdf.PdfSignature("Iron.pfx", "123456");
// Step 3. Optional signing options and a handwritten Signature graphic
Signature.SigningContact = "support@ironsoftware.com";
Signature.SigningLocation = "Chicago, USA";
Signature.SigningReason = "To show how to sign a PDF";
Signature.LoadSignatureImageFromFile("handwriting.png");
Signature.SignPdfFile("UnSecure.pdf");
Dim Signature = New IronPdf.PdfSignature("Iron.pfx", "123456")
' Step 3. Optional signing options and a handwritten Signature graphic
Signature.SigningContact = "support@ironsoftware.com"
Signature.SigningLocation = "Chicago, USA"
Signature.SigningReason = "To show how to sign a PDF"
Signature.LoadSignatureImageFromFile("handwriting.png")
Signature.SignPdfFile("UnSecure.pdf")
Cryptographic PDF signing may also be achieved during the PDF creation and rendering ( HTML to PDF ) process
// Step 1. Create a PDF
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
using PdfDocument doc = Renderer.RenderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>");
// Step 2. Create a Signature.
// You may create a .pfx or .p12 PDF signing certificate using Adobe Acrobat Reader.
// Read: https://helpx.adobe.com/acrobat/using/digital-ids.html
var Signature = new IronPdf.PdfSignature("Iron.pfx", "123456");
// Step 3. Optional signing options and a handwritten Signature graphic
Signature.SigningContact = "support@ironsoftware.com";
Signature.SigningLocation = "Chicago, USA";
Signature.SigningReason = "To show how to sign a PDF";
Signature.LoadSignatureImageFromFile("handwriting.png");
//Step 4. Sign the PDF with the PdfSignature. Multiple signing certificates may be used
doc.SignPdfWithDigitalSignature(Signature);
//Step 4. The PDF is not signed until saved to file, steam or byte array.
doc.SaveAs("signed.pdf");
// Step 1. Create a PDF
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
using PdfDocument doc = Renderer.RenderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>");
// Step 2. Create a Signature.
// You may create a .pfx or .p12 PDF signing certificate using Adobe Acrobat Reader.
// Read: https://helpx.adobe.com/acrobat/using/digital-ids.html
var Signature = new IronPdf.PdfSignature("Iron.pfx", "123456");
// Step 3. Optional signing options and a handwritten Signature graphic
Signature.SigningContact = "support@ironsoftware.com";
Signature.SigningLocation = "Chicago, USA";
Signature.SigningReason = "To show how to sign a PDF";
Signature.LoadSignatureImageFromFile("handwriting.png");
//Step 4. Sign the PDF with the PdfSignature. Multiple signing certificates may be used
doc.SignPdfWithDigitalSignature(Signature);
//Step 4. The PDF is not signed until saved to file, steam or byte array.
doc.SaveAs("signed.pdf");
' Step 1. Create a PDF
Dim Renderer As New IronPdf.ChromePdfRenderer()
Using doc As PdfDocument = Renderer.RenderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>")
' Step 2. Create a Signature.
' You may create a .pfx or .p12 PDF signing certificate using Adobe Acrobat Reader.
' Read: https://helpx.adobe.com/acrobat/using/digital-ids.html
Dim Signature = New IronPdf.PdfSignature("Iron.pfx", "123456")
' Step 3. Optional signing options and a handwritten Signature graphic
Signature.SigningContact = "support@ironsoftware.com"
Signature.SigningLocation = "Chicago, USA"
Signature.SigningReason = "To show how to sign a PDF"
Signature.LoadSignatureImageFromFile("handwriting.png")
'Step 4. Sign the PDF with the PdfSignature. Multiple signing certificates may be used
doc.SignPdfWithDigitalSignature(Signature)
'Step 4. The PDF is not signed until saved to file, steam or byte array.
doc.SaveAs("signed.pdf")
End Using