Set Outlines

This guide will show you how to apply outlines(bookmarks) from a JSON file to a PDF document.

An outline tree is a collection of outline items. Each outline item has a title, a location in the PDF document, and optional outline items. When you click on the title of an outline in a PDF document, you are redirected to the location the outline points to in the document.

The outlines should be similar to the JSON file generated by the Get outlines guide.

{
            "title": "string",
            "dest": {
                "page": 1,
                "mode": "XYZ",
                "x": 0,
                "y": 792,
                "zoom": 0,
                "entries": []
            }

For more information about creating outlines, see Outline Item.

To get the x and y coordinates,extract text coordinates from the input PDF. The Llx(first value) and Ury(last value) are the x and y coordinates respectively.

page is page_number -1

Sample Input

This is a sample structure of an outline item in JSON format:

{
    "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
                    }
                },
                {
                    "title": "1.2. Document History",
                    "dest": {
                        "page": 2,
                        "mode": "XYZ",
                        "x": 50,
                        "y": 506,
                        "zoom": 0
                    }
                }
            ]
        },
        {...}

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 UniCloudaccount.

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-19 import the UniPDF packages and other required dependencies.

Lines 21-28 authenticate your request with your UNIDOC_LICENSE_API_KEY with the init function.

The main function in lines 30-49 validates your input and passes it as arguements to the applytOutlines function.

Lines 51-84 define the applyOutlines function, which accepts inputPath, outlinesPath and outputPath as parameters. The function reads the JSON file from the outlinesPath and add the outlines to the input PDF. The outlined PDF document is saved to the outputPath.

Run the code

Run this command to apply outlines to the input PDF. 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 JSON file.

Got any Questions?

We're here to help you.