Tagging Lists

This guide demonstrates how to create a tagged PDF document with nested lists using UniPDF. It shows how to properly structure a PDF with accessibility tags by creating a document structure tree with K dictionaries for hierarchical list elements.

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 nested lists and tag them.

Code Explanation

The code starts by initializing the metered license API key in the init function (lines 29-36). A StructTreeRoot and a base KDictionary of type Document are created (lines 41-49) to serve as the root of the document’s structure tree. A creator.List is then initialized to represent the main list (line 51).

To ensure accessibility, list.AddTag(docK) is called (line 55), which associates the list with the document structure and generates the L (List) structure element. The code then iterates to create nested sublists (lines 63-81). For each sublist, l.AddTag(currentKDict) is used (line 67) to associate it with the parent list item’s body (LBody). GenerateKDict() is called (line 77) to retrieve the KDictionary for the sublist, which then serves as the parent for the next level of nesting. Finally, the list is drawn to the creator, the StructTreeRoot is set, and the document is written to a file.

Run the code

Run this command to generate the PDF with tagged lists.

go run pdf_tag_list.go

Sample output

The output file pdf_tag_list.pdf will contain nested lists that are correctly structured as lists in the accessibility tree.

Sample Output

Got any Questions?

We're here to help you.