Create PDF Outlines Using JavaScript
With PSPDFKit, it’s possible to create PDF outlines programmatically with JavaScript.
Creating, editing, or removing document outlines is available when using the Web SDK in standalone operational mode.
Here’s how to create a PDF outline in JavaScript:
const outline = PSPDFKit.Immutable.List([ new PSPDFKit.OutlineElement({ action: new PSPDFKit.Actions.GoToAction({ pageIndex: 1 }), children: PSPDFKit.Immutable.List([]), title: "My outline element 1" }), new PSPDFKit.OutlineElement({ action: new PSPDFKit.Actions.GoToAction({ pageIndex: 2 }), children: PSPDFKit.Immutable.List([]), title: "My outline element 2" }) ]); instance.setDocumentOutline(outline);
The example above creates an outline with two elements, each with a GoToAction
that takes the user to a destination (page 1, page 2) in the current document. There are several other actions you can use with outlines, such as opening a web link, submitting or resetting a form, or executing a script. These are described in the PDF actions guide.
Linking to URIs
Using URIAction
, you can link to a website from your outline:
const outline = PSPDFKit.Immutable.List([ new PSPDFKit.OutlineElement({ action: new PSPDFKit.Actions.URIAction({ uri: "https://pspdfkit.com" }), children: PSPDFKit.Immutable.List([]), title: "My outline element" }) ]); instance.setDocumentOutline(outline);