File Attachment Annotation
This guide shows how to create a file attachment annotation that will be pinned in certain position in the page.
Sample Input
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-18
import the UniPDF packages and other required dependencies.
The init
function in lines 20-27
authenticates your request with your UNIDOC_LICENSE_API_KEY
.
The main function in lines 29-46
validates your input and passes it as arguments to the annotatePdfAddFile
function.
Line 49-59
is where the input PDF file is loaded into a PdfReader object and then on line 61
we’ll get the first page of the PDF file.
Line 66
is where the embedded file object is created and then on line 72-75
we’ll assign some properties for the embedded file.
Line 85-102
is where the embedded file annotations definition are defined. The properties assigned to the annotations are:
X
andY
: The position of the annotation icon in the page.Width
andHeight
: The dimensions of the annotation icon.EmbeddedFile
: Embedded file object we created earlier at line67
.Description
: The description of the annotation.IconName
: The name of the predefined annotation icon we are going to use. Available values are:- Graph
- Push
- Pin
- Paperclip
- Tag
Color
: The color of the annotation icon.CreationDate
: The creation date of the annotation.
Afterwards, we construct an annotation using annotator.CreateFileAttachmentAnnotation
function on line 105
and add it into the page on line 111
.
The rest of the fuction is where we write the PDF document to a file.
Run the code
Run this command to add a file 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_file.go input.pdf output.pdf file_to_be_attached
# to reproduce sample output
go run pdf_annotate_add_file.go ../attachment/minimal.pdf output.pdf \
../attachment/dummy.xml
Sample output
When opened in Adobe Reader, you will see the attachment icon drawn on the specified location and page on the output PDF and the attachment description appears as a comment.