Copy Page with Accessibility
This guide demonstrates how to copy pages from an existing PDF file to a new PDF file while preserving the structure tree information, which is essential for maintaining accessibility.
Sample input

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 copy pages and their associated structure tree.
Code Explanation
The code starts by initializing the metered license API key in the init function (lines 22-29). The main function (lines 31-146) then parses the command-line arguments to get the input path, output path, and the list of page numbers to copy. A creator.Creator is initialized, and viewer preferences are configured (lines 42-58). A new StructTreeRoot is also created (lines 64-70) to hold the structure tree for the new output document.
The input PDF is read using model.NewPdfReaderFromFile (line 76), and its original StructTreeRoot is retrieved (lines 83-87) to access the existing accessibility tags. The code then iterates through the specified page numbers (lines 99-140). For each page, it duplicates the page content and creates a new Section KDictionary (lines 117-122). It then deep copies the K objects (structure elements) from the original page to this new section using the deepCopyKObject function (lines 158-189), ensuring that all accessibility tags are preserved. The page numbers within these copied K objects are updated to reflect their new position using setKPageNumber (lines 148-156). Finally, the duplicated page is added to the creator, the new StructTreeRoot is set, and the document is written to the output file.
Run the code
Run this command to copy pages from an input PDF to an output PDF.
go run pdf_copy_page_with_accessibility.go <INPUT_PDF_PATH> <OUTPUT_PDF_PATH> <PAGE_NUMBERS>
Example:
go run pdf_copy_page_with_accessibility.go input.pdf output.pdf 1 2
