Enabling LTV Using Second Revision

This guide showcases how to digitally sign a PDF file using a PKCS12 (.p12/.pfx) file and LTV enable the signature by adding a second revision to the document, containing the validation data.

Before you begin

You should get your API key from your UniCloud account.

If this is your first time using UniPDF SDK, follow this guide to set up a local development environment.

Project setup

Clone the project repository

In your terminal, clone examples repository using the following command: It contains the Go code we will be using for this guide.

git clone https://github.com/unidoc/unipdf-examples.git

Then navigate to the signatures folder in the unipdf-examples directory.

cd unipdf-examples/signatures

Configure environment variables

Configure your license key using the following command: Replace the UNIDOC_LICENSE_API_KEY with your API credentials from your UniCloud account.

Linux/Mac

export UNIDOC_LICENSE_API_KEY=PUT_YOUR_API_KEY_HERE

Windows

set UNIDOC_LICENSE_API_KEY=PUT_YOUR_API_KEY_HERE

How it works

The import section imports the necessary packages and other Go libraries. The init function authenticates your request by setting metered license key from your system environment.

The main function defined in lines 43-101, signs the input file using private key and certificate. The signing key pair is loaded and decoded from PKCS12 file provided in the command line arguments in lines 55-86. At line 89, the document is signed using signedBytes, err := signFile(inputPath, priv.(*rsa.PrivateKey), cert). The LTV is enabled, and the document is written to file using ltvEnable(bytes.NewReader(signedBytes), outputPath, certChain) at file 95.

The signFile function in lines 103-165, signs the file. It creates a signature handler, a PdfSignature object and then PdfFieldSignature in lines 129-136 and 139-152 respectively. Finally, it signs the document using appender.Sign(1, field) at line 154. The field argument here is a PdfFieldSignature type.

The ltvEnable function is used to LTV enable the certificate chain using ltv.EnableAll(certChain) at line 186. The document is then written to file using appender.WriteToFile(outputPath) at line 191.

Run the code

Use the following commands to run the code:

go run pdf_sign_ltv_extra_revision.go <FILE.p12> <P12_PASS> <INPUT_PDF_PATH> <OUTPUT_PDF_PATH> [<EXTRA_CERTS.pem>]

Got any Questions?

We're here to help you.