Add an outline to PDFs using JavaScript

To add a new outline item in an existing document.

JavaScript (v6.0+)

1Webviewer(...).then(async instance => {
2 const { docViewer, PDFNet } = instance;
3 await PDFNet.initialize();
4
5 docViewer.on('documentLoaded', () => {
6 const doc = docViewer.getDocument();
7 const pdfDoc = await doc.getPDFDoc();
8
9 // create outline and add it to the first page
10 const myitem = await PDFNet.Bookmark.create(pdfDoc, 'My outline');
11 await myitem.setAction(await PDFNet.Action.createGoto(await PDFNet.Destination.createFit(await pdfDoc.getPage(1))));
12 await pdfDoc.addRootBookmark(myitem);
13
14 // refresh document outline
15 const bookmarks = await doc.getBookmarks();
16 instance.updateOutlines(bookmarks);
17 });
18});

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales