How to create Long Term Validation (LTV) when signing PDF documents externally
Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create a Long-Term Validation when signing a PDF document externally in C# and VB.
Steps to create an LTV programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using System.Collections.Generic; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf; using Syncfusion.Pdf.Security; using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.Pkcs; using System.Security.Cryptography;
VB.NET
Imports System.Collections.Generic Imports Syncfusion.Pdf.Parsing Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Security Imports System.Security.Cryptography.X509Certificates Imports System.Security.Cryptography.Pkcs Imports System.Security.Cryptography
- Use the following code snippet to create Long Term Validation (LTV).
C#
// Load an existing PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Input.pdf");
// Get the page of the existing PDF document
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
// Create a new PDF signature without PdfCertificate instance
PdfSignature signature = new PdfSignature(loadedDocument, loadedPage, null, "Signature1");
// Hook up the ComputeHash event
signature.ComputeHash += Signature_ComputeHash;
// Create X509Certificate2 from your certificate to create a long-term validity
X509Certificate2 x509 = new X509Certificate2("../../PDF.pfx", "password123");
// Create LTV with your public certificates
signature.CreateLongTermValidity(new List<X509Certificate2> { x509 });
// Save and close the PDF document
loadedDocument.Save("SignedDocument.pdf");
loadedDocument.Close(true);
VB.NET
'Load an existing PDF document
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("../../Input.pdf")
'Get the page of the existing PDF document
Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage)
'Create a new PDF signature without PdfCertificate instance
Dim signature As PdfSignature = New PdfSignature(loadedDocument, loadedPage, Nothing, "Signature1")
'Hook up the ComputeHash event
AddHandler signature.ComputeHash, AddressOf Signature_ComputeHash
'Create X509Certificate2 from your certificate to create long-term validity
Dim x509 As X509Certificate2 = New X509Certificate2("../../PDF.pfx", "password123")
'Create LTV with your public certificates
signature.CreateLongTermValidity(New List(Of X509Certificate2) From { x509 })
'Save and close the PDF document
loadedDocument.Save("SignedDocument.pdf")
loadedDocument.Close(True);
- Add the following code to sign a PDF document externally.
C#
private static void Signature_ComputeHash(object sender, PdfSignatureEventArgs ars)
{
// Get the document bytes
byte[] documentBytes = ars.Data;
SignedCms signedCms = new SignedCms(new ContentInfo(documentBytes), detached: true);
// Compute the signature using the specified digital ID file and the password
X509Certificate2 certificate = new X509Certificate2("../../PDF.pfx", "password123");
CmsSigner cmsSigner = new CmsSigner(certificate);
// Set the digest algorithm SHA256
cmsSigner.DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1");
signedCms.ComputeSignature(cmsSigner);
// Embed the encoded digital signature to the PDF document
ars.SignedData = signedCms.Encode();
}
VB.NET
Private Shared Sub Signature_ComputeHash(ByVal sender As Object, ByVal ars As PdfSignatureEventArgs)
'Get the document bytes
Dim documentBytes As Byte() = ars.Data
Dim signedCms As SignedCms = New SignedCms(New ContentInfo(documentBytes), detached:=True)
'Compute the signature using the specified digital ID file and the password
Dim certificate As X509Certificate2 = New X509Certificate2("../../PDF.pfx", "password123")
Dim cmsSigner As CmsSigner = New CmsSigner(certificate)
'Set the digest algorithm SHA256
cmsSigner.DigestAlgorithm = New Oid("2.16.840.1.101.3.4.2.1")
signedCms.ComputeSignature(cmsSigner);
'Embed the encoded digital signature to the PDF document
ars.SignedData = signedCms.Encode();
End Sub
You can download the working sample from CreateLongTermValidity.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you will find other options like digitally sign a PDF file, digitally sign an existing PDF document, remove the digital signature from an existing PDF document, and more with code examples.
Click here to explore the rich set of Syncfusion Essential® PDF features.
See Also:
How to digitally sign an existing PDF document using C# and VB.NET.
How to digitally sign a PDF file in C#, VB.NET.
How to apply one or more digital signatures to a PDF using C# and VB.NET.
PDF digital signature and timestamp in .NET.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.