Signing PAdES Baseline B Signature

This guide will show how to create PAdES B-B compatible digital signature for a 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. It contains the Go code we will be using for this guide.

git clone

Navigate to the signatures folder in the unipdf-examples directory.

cd unipdf-examples/signatures

Configure environment variables

Replace the UNIDOC_LICENSE_API_KEY with your API credentials from your UniCloud account.





How it works

The import section in lines 8-25, imports UniPdF packages and other Go libraries. The next section, which contains the init function sets the license key to authenticate your library request.

The main function which is defined in lines 38-137, does the signing of the PDF document and writing it to a file. In lines 39-48, the necessary file paths and password are obtained from the command line arguments.

In lines 51-59, the private and public key are decoded from the pfxPath as follows:

  // Get private key and X509 certificate from the PFX file.
	pfxData, err := ioutil.ReadFile(pfxPath)
	if err != nil {
		log.Fatal("Fail: %v\n", err)

	priv, cert, err := pkcs12.Decode(pfxData, password)
	if err != nil {
		log.Fatal("Fail: %v\n", err)

Then in lines 62-74, the ca certificate is decoded from the pemPath provided in the command line argument. From the input file a new PdfReader is created in line 83. Then a PdfAppender created from this reader in line 89. In line 95 a signature handler is created using sighandler.NewEtsiPAdESLevelB(priv.(*rsa.PrivateKey), cert, cacert).

A new model.PdfSignature is created in lines 101-108 using:

  signature := model.NewPdfSignature(handler)
	signature.SetName("PAdES B-B Signature PDF")
	signature.SetDate(time.Now(), "")

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

Signature fields and appearance is created in lines 11-124. Then in line 126 the document is signed using:

  if err = appender.Sign(1, field); err != nil {
		log.Fatal("Fail: %v\n", err)

Finally, the document is written to a file in line 131.

Run the code

To run the code use the following command.

go run pdf_sign_pades_b_b.go <FILE.PFX> <PASSWORD> <FILE.PEM> <INPUT_PDF_PATH> <OUTPUT_PDF_PATH>

Got any Questions?

We're here to help you.