Set Language Identifier
This guide demonstrates how to set the language identifier for a PDF document and its content using UniPDF. Setting the correct language is essential for accessibility, enabling screen readers to pronounce and interpret the content correctly.
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 set the language for the entire document and for specific paragraphs.
Code Explanation
The code begins by initializing the metered license API key in the init function (lines 22-29). In the main function (lines 31-103), the default language for the document is set to US English using c.SetLanguage("en-US") (line 34). The SetPdfWriterAccessFunc (lines 37-43) is then used to set the Marked flag in the document catalog, which is a requirement for accessible documents. A StructTreeRoot and a base KDictionary are created (lines 46-52) to establish the document’s structure.
The example then demonstrates how to handle content with different languages. First, a paragraph with English text (“Hello World”) is created (lines 55-57). Its KDictionary is generated, and the language is explicitly set to British English (“en-GB”) using pMarkedContent.Lang (line 68). This paragraph is then drawn and added to the structure tree. Next, a second paragraph with Indonesian text (“Halo Dunia”) is created (lines 77-83). Similarly, its KDictionary is generated, and its language is set to Indonesian (“id-ID”) (line 90). This paragraph is also drawn and added to the structure tree. Finally, the StructTreeRoot is assigned to the creator, and the document is written to a file.
Run the code
Run this command to generate the PDF with language identifiers.
go run pdf_set_language_identifier.go
Sample output
The output file pdf_set_language_identifier.pdf will contain text with specified language attributes, which can be verified using a PDF accessibility checker.
