PDF: How to add digital signature to 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 sign and timestamp a PDF document:
/// <summary>
/// Signs a PDF or PDF/A document using X509 Certificate.
/// </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>
''' Signs a PDF or PDF/A document using X509 Certificate.
''' </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
Please read how to verify the digital signatures in PDF document
here.
Please read how to remove digital signature from PDF document
here.