PDF: How to add digital signature to a PDF document?
In This Topic
PDF document can be signed using the digital certificate, which can be obtained from a trusted Certificate Authority (CA) such as DigiCert or GlobalSign. Also it is possible to
create self-signed digital certificate for internal or testing purposes.
VintaSoft Imaging .NET SDK can sign PDF document using digital certificate. SDK uses "byte range" technology to a sign a PDF document, i.e. SDK calculates the checksum for binary data of the entire document. Calculated checksum and digital signature is stored in the signature field of the interactive form of PDF document.
If PDF document should be signed using digital signature, the following steps must be done:
- Create a signature field that will store the signature.
- Determine the information about digital certificate.
- Add the digital certificate to the signature field.
- Define the visual appearance of the signature field. This step should be made if digital signature should have visual appearance, for example, looks like a real pen stroke.
- Add the signature field to the interactive form of PDF document.
- Add annotation, which represents the signature field, to the annotation list of PDF page where the signature must be located.
- Save changes in the PDF document - the PDF document will be signed during document saving.
If PDF document should contain signature field for further document signing by other person, the following steps must be done:
- Create a signature field that will store the signature.
- Define the visual appearance of the signature field. This step should be made if digital signature should have visual appearance, for example, looks like a real pen stroke.
- Add the signature field to the interactive form of PDF document.
- Add annotation, which represents the signature field, to the annotation list of PDF page where the signature must be located.
- Save changes in the PDF document.
Here is C#/VB.NET code that demonstrates how to add digital signature to a PDF document:
/// <summary>
/// Adds a digital signature to a PDF or PDF/A document.
/// </summary>
/// <param name="inputFilename">The filename of input PDF document.</param>
/// <param name="outputFilename">The filename of output PDF document.</param>
/// <param name="certificate">The X509 certificate.</param>
/// <param name="conformance">PDF/A conformance.</param>
public static void SignDocument(
string inputFilename,
string outputFilename,
X509Certificate2 certificate,
PdfDocumentConformance conformance)
{
// create digital signature helper
PdfPageDigitalSignatureHelper digitalSignatureHelper = new PdfPageDigitalSignatureHelper(certificate, new RectangleF(10, 10, 250, 75));
// set signing reason
digitalSignatureHelper.SigningReason = "Test signing";
// set text
digitalSignatureHelper.SignatureText = string.Format("Digitally signed by\n{0}", digitalSignatureHelper.SignerName);
PdfAConverter pdfAConverter = null;
// if specified PDF/A conformance
if (conformance != PdfDocumentConformance.Undefined)
{
// create PDF/A converter
pdfAConverter = (PdfAConverter)PdfDocumentConverter.Create(conformance);
//pdfAConverter.DefaultCmykIccProfileFilename = "DefaultCMYK.icc";
//pdfAConverter.DefaultRgbIccProfileFilename = "DefaultRGB.icc";
}
// sign PDF document and save PDF document to the output file
digitalSignatureHelper.SignDocument(inputFilename, outputFilename, 0, pdfAConverter);
}
''' <summary>
''' Adds a digital signature to a PDF or PDF/A document.
''' </summary>
''' <param name="inputFilename">The filename of input PDF document.</param>
''' <param name="outputFilename">The filename of output PDF document.</param>
''' <param name="certificate">The X509 certificate.</param>
''' <param name="conformance">PDF/A conformance.</param>
Public Shared Sub SignDocument(inputFilename As String, outputFilename As String, _
certificate As X509Certificate2, conformance As PdfDocumentConformance)
' create digital signature helper
Dim digitalSignatureHelper As New PdfPageDigitalSignatureHelper(certificate, New RectangleF(10, 10, 250, 75))
' set signing reason
digitalSignatureHelper.SigningReason = "Test signing"
' set text
digitalSignatureHelper.SignatureText = String.Format("Digitally signed by" & vbLf & "{0}", digitalSignatureHelper.SignerName)
Dim pdfAConverter As PdfAConverter = Nothing
' if specified PDF/A conformance
If conformance <> PdfDocumentConformance.Undefined Then
' create PDF/A converter
'pdfAConverter.DefaultCmykIccProfileFilename = "DefaultCMYK.icc";
'pdfAConverter.DefaultRgbIccProfileFilename = "DefaultRGB.icc";
pdfAConverter = DirectCast(PdfDocumentConverter.Create(conformance), PdfAConverter)
End If
' sign PDF document and save PDF document to the output file
digitalSignatureHelper.SignDocument(inputFilename, outputFilename, 0, pdfAConverter)
End Sub
Here is C#/VB.NET code that demonstrates how to add digital signature with timestamp to a PDF document:
using System.Drawing;
using System.Security.Cryptography.X509Certificates;
using Vintasoft.Imaging.Pdf;
/// <summary>
/// Adds digital signature with timestamp to a PDF page.
/// </summary>
public class PdfPageDigitalSignatureHelper_Timestamp
{
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 name of image file that defines the signature appearance
digitalSignatureHelper.SignatureImageFilename = "SignatureImage.svg";
// set the URL to the timestamp server
digitalSignatureHelper.TimestampServerUrl = "http://timestamp.comodoca.com/";
// 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 timestamp to a PDF page.
''' </summary>
Public Class PdfPageDigitalSignatureHelper_Timestamp
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 name of image file that defines the signature appearance
digitalSignatureHelper.SignatureImageFilename = "SignatureImage.svg"
' set the URL to the timestamp server
digitalSignatureHelper.TimestampServerUrl = "http://timestamp.comodoca.com/"
' 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
Please read how to verify the digital signatures in PDF document
here.
Please read how to remove digital signature from PDF document
here.