Tagging Annotations

This guide demonstrates how to create a PDF with text annotations that are properly tagged in the document structure tree for accessibility compliance using UniPDF.

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 accessibility folder in the unipdf-examples directory.

cd unipdf-examples/accessibility

How it works

The following example code shows how to create text annotations and add them to the structure tree.

Code Explanation

The code starts by initializing the metered license API key in the init function (lines 19-26). A StructTreeRoot and a base KDictionary of type Document are then created (lines 46-52) to form the foundation of the document’s structure tree. A new page is also initialized to hold the content (lines 55-61).

Three PdfAnnotationText objects are created (lines 64-104), each with specific properties such as content, position (rectangle), open status, and name. To ensure these annotations are accessible, GenerateKDict() is called for each one (e.g., line 76). This method generates the corresponding structure element, which is then added to the document’s main KDictionary. The annotations themselves are added to the page using page.AddAnnotation (lines 107-109). Finally, the StructTreeRoot is set on the creator, the page is added, and the complete document is written to a file.

Run the code

Run this command to generate the PDF with tagged annotations.

go run pdf_tag_annots.go

Sample output

The output file pdf_tag_annots.pdf will contain text annotations that are properly tagged, ensuring they are accessible to assistive technologies.

Sample Output

Got any Questions?

We're here to help you.