Tagging Tables
This guide demonstrates how to create a tagged PDF with a table using UniPDF. The table will be properly tagged in the document structure tree for accessibility compliance.
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 a table and tag it for accessibility.
Code Explanation
The code starts by initializing the metered license API key in the init function (lines 26-33). A StructTreeRoot and a base KDictionary of type Document are created (lines 39-47) to serve as the root of the document’s structure tree. A creator.Table is then initialized with 3 columns (line 66).
The drawTable function (lines 64-98) is used to populate the table with header and content rows. Crucially, table.AddTag(rootKObj) is called (line 95) to associate the table with the document structure tree. This method automatically generates the necessary structure elements, such as Table, TR (Table Row), TH (Table Header), and TD (Table Data), and adds them to the parent KDictionary. Finally, the table 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 a tagged table.
go run pdf_tag_table.go
Sample output
The output file pdf_tag_table.pdf will contain a table structure that is correctly interpreted by screen readers.
