Add Line Annotation

This guide will show you how to add an arrowhead line between two points on a page in a PDF document without changing the original content.

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

Sample Input

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

The init function in lines 23-30 authenticates your request with your UNIDOC_LICENSE_API_KEY.

The main function in lines 32-89 validates your input and passes it as arguments to the annotatePdfAddLineAnnotation function. LineDef initializes the annotator.LineAnnotationDef struct and assigns values to the line properties, which can be changed as needed.

Lines 91-136 define the annotatePdfAddLineAnnotation function, which accepts inputPath, targetPageNum, outputPath and lineDef as parameters. The function reads the input PDF, draws a line between the specified points on the page, and writes the annotated PDF to the output path.

Run the code

Run this command to add a line 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_line.go input.pdf <page> <x1> <y1> <x2> <y2> output.pdf

# to reproduce sample output
go run pdf_annotate_add_line.go unidoc-report.pdf 5 60 350 140 500 output.pdf

Sample output

You will see an arrowhead line drawn on the specified location and page on the output PDF. page with line annotation

Got any Questions?

We're here to help you.