---
title: "Digitally sign a PDF in Go"
url: https://recursiveai.org/onepdf/go/cookbook/sign-pdf-in-go/
description: "Add a PAdES digital signature to a PDF in Go with onepdf-go, then verify it against an explicit trust anchor. Run the complete example locally."
version: "1.0.1"
---

# Digitally sign a PDF in Go

**onepdf-go** is a free, open-source PDF library for Go from Recursive AI that reads, edits, signs, redacts and lays out PDFs. MIT or Apache-2.0, no third-party dependencies.

[pkg.go.dev](https://pkg.go.dev/recursiveai.org/go/onepdf) · [GitHub](https://github.com/recursive-ai-oss/onepdf-go) · [Docs](https://recursiveai.org/onepdf/go/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf/go/cookbook/sign-pdf-in-go/sign-pdf-in-go.zip)

Use `sign.Sign` on `input.pdf`, a one-page PDF that reads “Release approved for shipment.” Use `sign.Verify` to check the signature against a trusted certificate.

```shell
go get recursiveai.org/go/onepdf
```

In the unzipped folder, run `go run .` with Go 1.26.8.

```go
package main

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"crypto/x509"
	"crypto/x509/pkix"
	"fmt"
	"log"
	"math/big"
	"os"
	"time"

	"recursiveai.org/go/onepdf"
	"recursiveai.org/go/onepdf/sign"
)

func main() {
	cert, cred := demoCredential()
	doc := must(onepdf.OpenFile("input.pdf", onepdf.WithEditing()))
	defer doc.Close()

	out := must(os.Create("signed.pdf"))
	must(sign.Sign(doc, out, cred))
	check(out.Close())

	signed := must(onepdf.OpenFile("signed.pdf"))
	defer signed.Close()
	trust := sign.WithTrustAnchors(cert)
	report := must(sign.Verify(signed, trust))
	if len(report.Signatures) != 1 || !report.Signatures[0].IsValid {
		log.Fatal(report.Summary())
	}
	fmt.Println("Wrote signed.pdf")
	format, _ := report.Signatures[0].Signature.SubFilter()
	fmt.Println("Signature format:", format)
	fmt.Println(report.Summary())
}

func demoCredential() (*x509.Certificate, *sign.Credential) {
	key := must(ecdsa.GenerateKey(elliptic.P256(), rand.Reader))
	now := time.Now()
	template := &x509.Certificate{
		SerialNumber: big.NewInt(1),
		Subject:      pkix.Name{CommonName: "Demo signer"},
		NotBefore:    now.Add(-time.Hour),
		NotAfter:     now.Add(time.Hour),
		KeyUsage:     x509.KeyUsageDigitalSignature,
	}
	der := must(x509.CreateCertificate(rand.Reader, template, template, key.Public(), key))
	cert := must(x509.ParseCertificate(der))
	return cert, must(sign.NewCredential(cert, key))
}

func check(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func must[T any](value T, err error) T {
	check(err)
	return value
}
```

Paste the code above into main.go, run `go mod init example` and `go get recursiveai.org/go/onepdf`, then `go run .`; or unzip [the example](https://recursiveai.org/onepdf/go/cookbook/sign-pdf-in-go/sign-pdf-in-go.zip) and `go run .`.

If you paste the code, copy `input.pdf` from the zip into your project folder, or put your own PDF there named `input.pdf`.

The program prints:

```text
Wrote signed.pdf
Signature format: ETSI.CAdES.detached
1 signature: 1 valid, 0 not valid
```

`ETSI.CAdES.detached` is the PDF SubFilter name for a PAdES signature.

The demo certificate is trusted only because the verifier explicitly supplies it; verification doesn’t fetch certificates or revocation information from the network. If you omit the trust anchor, a matching signature alone isn’t enough for `IsValid`.

## Variations

**Record why you signed.** Replace the `sign.Sign` call to store a reason in the signature:

```go
reason := sign.WithReason("Approved for shipment")
must(sign.Sign(doc, out, cred, reason))
```

## Related recipes

- [Redact text before signing](https://recursiveai.org/onepdf/go/cookbook/redact-pdf-in-go/)
- [Password-protect a PDF](https://recursiveai.org/onepdf/go/cookbook/encrypt-pdf-with-password-in-go/)

## The short answer

To add a digital signature to a PDF in Go, create a `sign.Credential` from an X.509 certificate and its matching private key, then pass the credential, the input PDF, and an output writer to `sign.Sign`. The default signature is PAdES, a format for digital signatures in PDFs. Open the signed file separately and call `sign.Verify` with your chosen trust anchors. Check `IsValid` on the signature you expected, not just whether `Verify` returned an error.

## How do I create a digital signature for a PDF?

You need a PDF and a signing identity: a certificate and a private key. The certificate contains the public key; the private key creates the signature and stays with the signer. The recipe uses an ephemeral ECDSA key and a self-signed certificate so you can exercise the entire workflow locally. It doesn’t prove a real person approved the shipment. For a real workflow, use a certificate your recipients already trust, or agree on a trust anchor with them separately. `sign.NewCredential` does not read `.p12` or `.pfx` files directly. Export the certificates (including any chain in the bundle) and the private key as PEM with OpenSSL:

```shell
openssl pkcs12 -in signer.p12 -nokeys -out certs.pem
openssl pkcs12 -in signer.p12 -nocerts -out key.pem
```

The second command prompts for a passphrase to encrypt `key.pem`. To replace the demo credential, load the signer certificate and its matching key and pass the intermediate certificates separately to `sign.NewCredential`.

`sign.Sign` appends an incremental update: the original file’s bytes are retained at the front of the signed copy. That’s why the input and output must be different files. If you need to remove content or rewrite the PDF, do that before signing, not after.

## How do I verify a digital signature in a PDF?

Verification checks which bytes the signature covers, whether their digest still matches, and whether the signature verifies with the certificate’s public key. Certificate trust is a separate check. Pass `sign.WithTrustAnchors` with certificates you trust independently of the PDF; don’t trust a certificate merely because the PDF includes it. An untrusted signer and a modified document are different problems. Inspect the individual results in the verification report to tell them apart.

The example does not configure a revocation callback. A valid result here does not mean a revocation service was consulted. Likewise, the sample does not add a trusted timestamp. Its result demonstrates that this file verifies now under the explicit demo trust anchor.

## Is a digital signature the same as an e-signature on a PDF?

A typed name or a pasted handwritten signature is visible page content. It doesn’t by itself let a reader verify whether the PDF changed. A digital signature adds cryptographic evidence to the file. The sample adds that evidence without drawing a signature on the page, so the page can look unchanged even though a reader’s signature panel shows a new signature.
