Retrieving Revision of a PDF Document

This guide will explain the example code that is used to retrieve the specific or previous revision of a PDF document signed with private/public key pair.

Sample Input

sample PDF file

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

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.





How it works

The import section imports the necessary UniPDF packages and other Go libraries. Then next section, which is the init function gets the metered license key from the system environment which is used to authenticate your library request.

In line 37, the main function starts. The inputPath and outputPath destination of the files are parsed from the command line arguments. The addSignature(pdfReader, 0) function call is used to add a signature to the document. Here the PdfReader is used as an argument. Using the returned document buffer, another PdfReader is instantiated in line 78. Then the specific revision is obtained using retrieveSpecificRevision(pdfReader2, 0, outputPath). The commented line of code in line 85, shows how to retrieve previous revision.

The retrievePreviousRevision function in lines 93-110 gets the previous revision of the document. This is done using the following code.

prevReader, err := reader.GetPreviousRevision()
if err != nil {
  return err

appender, err := model.NewPdfAppender(prevReader)
if err != nil {
  return err

The rest of the function just writes the document to file at the specified output path.

The retrieveSpecificRevision function gets the specific revision number provided in the parameter of the function. This is done using the GetRevision method of the mode.PdfReader object as follows:

prevReader, err := reader.GetRevision(revisionNumber)
if err != nil {
  return err

appender, err := model.NewPdfAppender(prevReader)
if err != nil {
  return err

The second part of the above snippet gets a model.PdfAppender from the reader. After this, the document is written to file at the specified output path using the Appender.

The addSignature function which is used to sign a given document, starts in line 131. This function first generates private/public key pair using generateSigKeys() in line 144. In line 150, a signature handler is created from the private key and the certificate using sighandler.NewAdobePKCS7Detached(priv, cert). Then a new model.PdfSignature is created from this handler in the next lines, which is 156-159. Then the signature is initialized using signature.Initialize() method. This initialization step prepares the signature dictionary for signing. A model.PdfFieldSignature is created in lines 156-180. Finally, the document is signed using appender.Sign(totalPage, sigField) and gets returned.

The generateSigKeys() function, which is used to generate private/public key pair previously, is defined in lines 203-239. The rsa.GenerateKey, x509.CreateCertificate and x509.ParseCertificate functions from the crypto library are used here to create the key pair.

Run the code

Run the code using the following command.

go run pdf_sign_get_revision.go <INPUT_PDF_PATH> <OUTPUT_PDF_PATH>

Sample output

2023/08/22 20:29:06 PDF file successfully signed
2023/08/22 20:29:07 PDF file successfully signed
2023/08/22 20:29:07 PDF file successfully signed. Output path: signed_and_get_revision.pdf

Signed and Get Revision

Got any Questions?

We're here to help you.