Tagging Grids
This guide demonstrates how to create a tagged PDF with a grid (table) using UniPDF. The grid 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 grid and tag it for accessibility.
Code Explanation
The code starts by initializing the metered license API key in the init function (lines 18-25). A StructTreeRoot and a base KDictionary of type Document are created (lines 31-39) to serve as the root of the document’s structure tree. A creator.Grid is then initialized with 3 columns (line 59).
The drawGrid function (lines 57-111) is used to populate the grid with header, content, and footer rows. Crucially, grid.AddTag(rootKObj) is called (line 108) to associate the grid 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 grid 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 grid.
go run pdf_tag_grid.go
Sample output
The output file pdf_tag_grid.pdf will contain a table structure that is correctly interpreted by screen readers.
