Tagging Forms
This guide demonstrates how to create a PDF with form fields (text fields, submit and reset buttons) 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 form fields, labels, and buttons, and how to tag them for accessibility.
Code Explanation
The code begins 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 53-59) to establish the document’s structure tree. The code then iterates through a predefined list of text fields (lines 66-108). For each field, a TextField is created using annotator.NewTextField (line 68). A tooltip (TU) is set for each field (line 74), which is a critical accessibility feature as it provides a description of the field to screen readers. The field is then added to the form, and its annotation is added to the page.
For each form field, a corresponding label is created as a paragraph (lines 81-83). This label is assigned a structure type of StructureTypeForm (line 87) and a unique MarkedContentID (line 90). A KDictionary is generated for the label, its Alt text is set (line 97), and it is added to the document’s structure tree. This ensures that the label is properly associated with the form field in the accessibility tree. Additionally, addSubmitButton and addResetButton functions are called (lines 110-118) to add submit and reset buttons to the form. Finally, the form and the structure tree are assigned to the creator, and the document is written to a file.
Run the code
Run this command to generate the PDF with tagged form fields.
go run pdf_tag_form.go
Sample output
The output file pdf_tag_form.pdf will contain an interactive form where fields are properly labeled and tagged, making them accessible to screen readers.
