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

page to add file attachment 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-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:

  1. X and Y: The position of the annotation icon in the page.
  2. Width and Height: The dimensions of the annotation icon.
  3. EmbeddedFile: Embedded file object we created earlier at line 67.
  4. Description: The description of the annotation.
  5. IconName: The name of the predefined annotation icon we are going to use. Available values are:
    • Graph
    • Push
    • Pin
    • Paperclip
    • Tag
  6. Color: The color of the annotation icon.
  7. 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. page with file attachment annotation

Got any Questions?

We're here to help you.