Skip to main content

Add Outlines and Bookmarks to PDFs


Add bookmarks to new PDF and existing PDFs using the pdf endpoint.

The DynamicPDF API's pdf endpoint makes adding bookmarks to a PDF straight-forward. You can create new bookmarks and attach pre-existing bookmarks. The following example illustrates both ways to create bookmarks.

tip

Check out Getting Started and Task Roadmap if you are new to The DynamicPDF API.

info

Refer to the documentation for more information on working with outlines (JSON Instructions Schema - outlines).

Calling Endpoint Directly

The following JSON instructions document illustrates creating three new PDFs and adding new bookmarks. It also merges an existing PDF containing an outline and adds it to the newly created one. The following instructions document defines the following processing instructions:

  • defines three page inputs that each creates a single-page PDF,
  • defines a pdf input that uploads an existing PDF with a pre-existing outline,
  • defines an outline that creates three new bookmarks for the three created PDFs,
  • appends the existing PDF's outline to the created outline,
  • and then merge the four documents into one combined PDF containing the new outline.
"inputs": [
{
"type": "page",
"pageWidth": 612,
"pageHeight": 792,
"elements": [
{
"type": "text",
"text": "Hello World 1",
"placement": "topCenter"
}
],
"id": "a"
},
{
"type": "page",
"pageWidth": 612,
"pageHeight": 792,
"elements": [
{
"type": "text",
"text": "Hello World 2",
"placement": "topCenter"
}
],
"id": "b"
},
{
"type": "page",
"pageWidth": 612,
"pageHeight": 792,
"elements": [
{
"type": "text",
"text": "Hello World 3",
"placement": "topCenter"
}
],
"id": "c"
},
{
"type": "pdf",
"mergeOptions": {
"outlines": false
},
"resourceName": "PdfOutlineInput.pdf",
"id": "pdfoutlineinput"
}
],
"outlines": [
{
"text": "Root Outline",
"children": [
{
"text": "Page 1",
"linkTo": {
"inputID": "a",
"pageOffset": 0,
"pageZoom": "fitPage"
}
},
{
"text": "Page 2",
"linkTo": {
"inputID": "b",
"pageOffset": 0,
"pageZoom": "fitPage"
}
},
{
"text": "Page 3",
"linkTo": {
"inputID": "c",
"pageOffset": 0,
"pageZoom": "fitPage"
}
},
{
"fromInputID": "pdfoutlineinput"
}
]
}
]
}

Run In Postman

curl --location 'https://api.dpdf.io/v1.0/pdf'
--header 'Authorization: Bearer DP--api-key--
--form 'Instructions=@C:/temp/solutions/outlines/instructions.json'
--form 'Resource=@C:/temp/solutions/outlines/PdfOutlineInput.pdf'

info

You can also add other elements to an outline such as URLs, using the linkTo property.

Calling Endpoint Using Client Library

You can also use one of the client libraries rather than calling the pdf endpoint directly. The following example illustrates.

public static void Run(string apiKey, string basePath, string outputPath)
{
Pdf pdf = new Pdf();
pdf.ApiKey = apiKey;

PageInput pageInput = pdf.AddPage();
TextElement element = new TextElement("Hello World 1", ElementPlacement.TopCenter);
pageInput.Elements.Add(element);

PageInput pageInput1 = pdf.AddPage();
TextElement element1 = new TextElement("Hello World 2", ElementPlacement.TopCenter);
pageInput1.Elements.Add(element1);

PageInput pageInput2 = pdf.AddPage();
TextElement element2 = new TextElement("Hello World 3", ElementPlacement.TopCenter);
pageInput2.Elements.Add(element2);

var inputA = pdf.AddPdf(new PdfResource(basePath + "PdfOutlineInput.pdf"));
inputA.Id = "pdfoutlineinput";

Outline rootOutline = pdf.Outlines.Add("Root Outline");

rootOutline.Children.Add("Page 1", pageInput);
rootOutline.Children.Add("Page 2", pageInput1);
rootOutline.Children.Add("Page 3", pageInput2);
rootOutline.Children.AddPdfOutlines(inputA);

PdfResponse pdfResponse = pdf.Process();

if (pdfResponse.IsSuccessful)
{
File.WriteAllBytes(outputPath + "/outlines-output.pdf", pdfResponse.Content);
}
else
{
Console.WriteLine(pdfResponse.ErrorJson);
}
}
Source: OutlinesSolution.cs
info

The pdf endpoint takes a JSON instructions document that provides instructions for creating, transforming, merging, and formatting inputs into a combined PDF. Refer to documentation on the instructions schema for information on how to use the pdf endpoint.

   Follow us on social media for latest news!