List Annotations in a PDF

This guide will show you how to get all the annotations on each page in a PDF document.

Sample Input

page with rect 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

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

cd unipdf-examples/extract

How it works

Lines 9-15 import the UniPDF packages and other required dependencies.

The init function in lines 17-24 authenticates your request with your UNIDOC_LICENSE_API_KEY.

The main function in lines 26-39 validates your input and passes it as an argument to the listAnnotations function.

Lines 41-80 define the listAnnotations function, which accepts inputPath as a parameter. The function reads the input PDF with the pdfReader method and gets the annotations for each page with getAnnotations() function.

The printAnnotations function in lines 69-73 returns all annotations for each page in the PDF to the terminal.

Run the code

Run this command to list the annotations per page in a PDF. This will also get all the required dependencies to run the program.

go run pdf_list_annotations.go input.pdf input2.pdf ...

Sample output

You will get a list of annotations per page in the PDF on the terminal.

-- Page 1
 1. *model.PdfAnnotationSquare: Dict("Type": Annot, "Rect": [139, 450, 199, 490], "AP": Dict("N": Ref(15 0), ), "C": [0, 0, 0], )

Got any Questions?

We're here to help you.