PDF: How to add digital signature with text appearance to a PDF document?
In This Topic
Here is C#/VB.NET code that demonstrates how to add digital signature with text appearance to a PDF document using
PdfPageDigitalSignatureHelper class:
using System.Drawing;
using System.Security.Cryptography.X509Certificates;
using Vintasoft.Imaging.Pdf;
/// <summary>
/// Adds digital signature with text appearance to a PDF page.
/// </summary>
public class PdfPageDigitalSignatureHelper_TextAppearance
{
public static void Run()
{
// 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}", digitalSignatureHelper.SignerName);
// add digital signature to the first PDF page, sign PDF document and save PDF document to the output file
digitalSignatureHelper.SignDocument("TestDocumentToSign.pdf", "TestDocumentToSign-signed.pdf", 0);
}
}
}
Imports System.Drawing
Imports System.Security.Cryptography.X509Certificates
Imports Vintasoft.Imaging.Pdf
''' <summary>
''' Adds digital signature with text appearance to a PDF page.
''' </summary>
Public Class PdfPageDigitalSignatureHelper_TextAppearance
Public Shared Sub Run()
' 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}", digitalSignatureHelper.SignerName)
' add digital signature to the first PDF page, sign PDF document and save PDF document to the output file
digitalSignatureHelper.SignDocument("TestDocumentToSign.pdf", "TestDocumentToSign-signed.pdf", 0)
End Using
End Sub
End Class
Here is C#/VB.NET code that demonstrates how to add digital signature with text appearance to a PDF document using
PdfPage.AddVisibleDigitalSignatureWithTextAppearance method:
using System.Drawing;
using System.Security.Cryptography.X509Certificates;
using Vintasoft.Imaging.Pdf;
using Vintasoft.Imaging.Pdf.Tree;
/// <summary>
/// Adds visible digital signature with text appearance to a PDF page.
/// </summary>
public class AddVisibleDigitalSignatureWithTextAppearanceExample
{
public static void Run()
{
// get the X509 certificate from .pfx-file
using (X509Certificate2 certificate = new X509Certificate2("TestCertificate.pfx", "test"))
{
// get the signer name from X509 certificate
string signerName = certificate.GetNameInfo(X509NameType.SimpleName, false);
// open PDF document
using (PdfDocument pdfDocument = new PdfDocument("TestDocumentToSign.pdf"))
{
// get the first PDF page
PdfPage pdfPage = pdfDocument.Pages[0];
// add visible digital signature with text appearance to the PDF page
pdfPage.AddVisibleDigitalSignatureWithTextAppearance(
certificate, "Signature", "Approve document", string.Format("Digitally signed by\n{0}", signerName),
new RectangleF(20, 20, 250, 100), false);
// sign PDF document and save PDF document to the output file
pdfDocument.SaveChanges("TestDocumentToSign-signed-text.pdf");
}
}
}
}
Imports System.Drawing
Imports System.Security.Cryptography.X509Certificates
Imports Vintasoft.Imaging.Pdf
Imports Vintasoft.Imaging.Pdf.Tree
''' <summary>
''' Adds visible digital signature with text appearance to a PDF page.
''' </summary>
Public Class AddVisibleDigitalSignatureWithTextAppearanceExample
Public Shared Sub Run()
' get the X509 certificate from .pfx-file
Using certificate As New X509Certificate2("TestCertificate.pfx", "test")
' get the signer name from X509 certificate
Dim signerName As String = certificate.GetNameInfo(X509NameType.SimpleName, False)
' open PDF document
Using pdfDocument As New PdfDocument("TestDocumentToSign.pdf")
' get the first PDF page
Dim pdfPage As PdfPage = pdfDocument.Pages(0)
' add visible digital signature with text appearance to the PDF page
pdfPage.AddVisibleDigitalSignatureWithTextAppearance(certificate, "Signature", _
"Approve document", String.Format("Digitally signed by" & vbLf & "{0}", signerName), _
New RectangleF(20, 20, 250, 100), False)
' sign PDF document and save PDF document to the output file
pdfDocument.SaveChanges("TestDocumentToSign-signed-text.pdf")
End Using
End Using
End Sub
End Class