Add Rectangle Annotation

This guide will show you how to add a rectangle annotation to a specific page in a PDF document without altering its original content.

The x, y, width and height coordinates are in the PDF coordinate system, where (0,0) is in the lower left corner of the page.

Sample Input

page to add 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 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 11-19 import the UniPDF packages and other required dependencies.

The init function in lines 21-28 authenticates your request using your UNIDOC_LICENSE_API_KEY.

The main function in lines 30-77 validates your input and passes them as arguments to the annotatePdfAddRectAnnotation function.

Lines 80-138 define the annotatePdfAddRectAnnotation function, which accepts inputPath, pageNumber, outputPath, x,y, width and height as parameters. The function reads the input PDF, creates the rectangle at the specified location and writes the annotated PDF to the output path.

Run the code

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

go run pdf_annotate_add_rectangle.go input.pdf page x y width height output.pdf

# to reproduce sample output
go run pdf_annotate_add_rectangle.go input.pdf 1 100 100 50 20 output.pdf

Sample output

You will see a rectangle added to the specified location and page on the output PDF. page with rect annotation

Got any Questions?

We're here to help you.