Get Outlines

Bookmarks list the content per page in a PDF document for accessibilty. Each bookmark is associated with a specific page in the document, allowing users to navigate content in the PDF document.

This guide will show you how to retrieve outlines or bookmarks from a PDF document and save them to a JSON file. The resulting JSON file can be used with the set outlines guide to apply outlines to a PDF document.

Sample Input

Get PDF Outline

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.

Project setup

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 outlines folder in the unipdf-examples directory.

cd unipdf-examples/outlines

Configure environment variables

Replace the UNIDOC_LICENSE_API_KEY with your API credentials from your UniDocaccount.

Linux/Mac

export UNIDOC_LICENSE_API_KEY=PUT_YOUR_API_KEY_HERE

Windows

set UNIDOC_LICENSE_API_KEY=PUT_YOUR_API_KEY_HERE

How it works

Lines 11-18 import the UniPDF packages and other required dependencies.

The init function in lines 20-27 authenticate your request with your UNIDOC_LICENSE_API_KEY.

The main function in lines 29-44 validates your input and passes it as an arguement to the getOutlines function.

Lines 46-65 define the getOutlines function, which accepts inputPath as a parameter. The function reads the PDF from the inputPath with pdfReader and calls the GetOutlines method to return the PDF’s outlines.

Run the code

Run this command to retrieve the input PDF’s outlines. This will also get all the required dependencies to run the program.

go run pdf_get_outlines.go input.pdf > outlines.json 

Sample output

You will get the PDF’s outlines or bookmarks similar to this in a JSON file.

{
    "entries": [
        {
            "title": "Table of Contents",
            "dest": {
                "page": 1,
                "mode": "XYZ",
                "x": 0,
                "y": 792,
                "zoom": 0
            }
        },
        {
            "title": "1. Document control",
            "dest": {
                "page": 2,
                "mode": "XYZ",
                "x": 50,
                "y": 652,
                "zoom": 0
            },
            "entries": [
                {
                    "title": "1.1. Issuer details",
                    "dest": {
                        "page": 2,
                        "mode": "XYZ",
                        "x": 50,
                        "y": 634,
                        "zoom": 0
                    }
                }
            ]
        },
        {...}

Got any Questions?

We're here to help you.