Enabling LTV Using One Revision

This guide will explain how to digitally sign a PDF file using a PKCS12 (.p12/.pfx) file and LTV enable the signature in one PDF revision.

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

In this example code, the import section in lines 10-26 imports UniPDF packages and other necessary Go libraries. The init function which is defined in lines 29-36, loads the API key and set the license.

The main function in lines 40-155, contains the code to sign the document. In lines 41-49, the inputs are obtained from the command line arguments. The private key and X509 certificate are loaded and decoded from the PKCS12 file. In lines 63-83, the certificate chain are loaded and decoded from issuer certificate data provided via the command line arguments.

In line 98, a new PdfAppender is created from the PdfReader using appender, err := model.NewPdfAppender(reader). A new signature handler is created in line 104 from the private key and the certificate object. Then a new PdfSignature is created and initialized in lines 110-117 using the following piece of code:

signature := model.NewPdfSignature(handler)
signature.SetName("Test Sign LTV enable")
signature.SetReason("TestSignLTV")
signature.SetDate(time.Now(), "")

if err := signature.Initialize(); err != nil {
  log.Fatal("Fail: %v\n", err)
}

The signature field and appearance is created in lines 120-133 using functions from the annotator package. Then the document is signed using appender.Sign(1, field) at line 135. The model.LTV type is created at line 140. After that the LTV is then enabled using ltv.EnableChain(certChain) at line 145. Finally, the document is written to file using appender.WriteToFile(outputPath) at line 150.

Run the code

Use the following command to run the code:

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

Got any Questions?

We're here to help you.