PDF: How to add 2 digital signatures to PDF document?
In This Topic
Here is C#/VB.NET code that demonstrates how to add 2 digital signatures to a PDF document:
using System.Drawing;
using System.Security.Cryptography.X509Certificates;
using Vintasoft.Imaging.Pdf;
/// <summary>
/// Adds two digital signatures with text appearance to a PDF page.
/// </summary>
public class PdfPageDigitalSignatureHelper_Multi
{
public static void Run()
{
// open PDF document
using (PdfDocument document = new PdfDocument("TestDocumentToSign.pdf"))
{
// get the X509 certificate from .pfx-file
using (X509Certificate2 certificate = new X509Certificate2("TestCertificate.pfx", "test"))
{
// create a helper that alows to add digital signature to a PDF document
PdfPageDigitalSignatureHelper digitalSignatureHelper =
new PdfPageDigitalSignatureHelper(certificate, new RectangleF(20, 20, 250, 100));
// set the reason for signing
digitalSignatureHelper.SigningReason = "Approve document";
// set the signature appearance text
digitalSignatureHelper.SignatureText = string.Format("Digitally signed by\n{0}, page 1",
digitalSignatureHelper.SignerName);
// add signature to first page
digitalSignatureHelper.AddSignature(document, 0);
// set the signature appearance text
digitalSignatureHelper.SignatureText = string.Format("Digitally signed by\n{0}, page 2",
digitalSignatureHelper.SignerName);
// add signature to second page
digitalSignatureHelper.AddSignature(document, 1);
}
// sign PDF document and save PDF document to the output file
document.SaveChanges("TestDocumentToSign-signed.pdf");
}
}
}
Imports System.Drawing
Imports System.Security.Cryptography.X509Certificates
Imports Vintasoft.Imaging.Pdf
''' <summary>
''' Adds two digital signatures with text appearance to a PDF page.
''' </summary>
Public Class PdfPageDigitalSignatureHelper_Multi
Public Shared Sub Run()
' open PDF document
Using document As New PdfDocument("TestDocumentToSign.pdf")
' get the X509 certificate from .pfx-file
Using certificate As New X509Certificate2("TestCertificate.pfx", "test")
' create a helper that alows to add digital signature to a PDF document
Dim digitalSignatureHelper As New PdfPageDigitalSignatureHelper(certificate, New RectangleF(20, 20, 250, 100))
' set the reason for signing
digitalSignatureHelper.SigningReason = "Approve document"
' set the signature appearance text
digitalSignatureHelper.SignatureText = String.Format("Digitally signed by" & vbLf & "{0}, page 1", _
digitalSignatureHelper.SignerName)
' add signature to first page
digitalSignatureHelper.AddSignature(document, 0)
' set the signature appearance text
digitalSignatureHelper.SignatureText = String.Format("Digitally signed by" & vbLf & "{0}, page 2", _
digitalSignatureHelper.SignerName)
' add signature to second page
digitalSignatureHelper.AddSignature(document, 1)
End Using
' sign PDF document and save PDF document to the output file
document.SaveChanges("TestDocumentToSign-signed.pdf")
End Using
End Sub
End Class