Add Text Annotation

This guide will show you how to add text annotation to each page in a PDF document without changing the original content.

Sample Input

page to add text annotation

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.

Clone the project repository

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

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

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

cd unipdf-examples/annotations

How it works

Lines 10-17 import the UniPDF packages and other required dependencies.

The init function in lines 19-26 authenticates your request with your UNIDOC_LICENSE_API_KEY.

The main function in lines 28-45 validates your input and passes it as arguments to the annotatePdfAddTextAnnotation function.

Lines 91-136 define the annotatePdfAddTextAnnotation function, which accepts inputPath, outputPath and annotationText as parameters. The function reads the input PDF, applies the text annotation to every page in the PDF, and writes the annotated PDF to the output path.

Run the code

Run this command to add a text annotation to each page in a PDF. This will also get all the required dependencies to run the program.

go run pdf_annotate_add_text.go input.pdf output.pdf text

# reproduce sample output
go run pdf_annotate_add_text.go unidoc-report.pdf newtext-output.pdf "This is a text annotation"

Sample output

You will see a yellow-colored box at the bottom-left corner of each page in the output PDF. When you hover over or click the yellow box, you will see the text annotation. page with text annotation

Got any Questions?

We're here to help you.