For the complete documentation index, see llms.txt. This page is also available as Markdown.

Add an outline to PDFs using JavaScript

Requirements

These packages are required to use these features in production. Trial keys have unlimited access to all features

Package: Page ManipulationLive demo

To add a new outline item in an existing document.

Webviewer(...).then(async instance => {
  const { documentViewer, PDFNet } = instance.Core;
  await PDFNet.initialize();

  documentViewer.addEventListener('documentLoaded', async () => {
    const doc = documentViewer.getDocument();
    const pdfDoc = await doc.getPDFDoc();

    // create outline and add it to the first page
    const myitem = await PDFNet.Bookmark.create(pdfDoc, 'My outline');
    await myitem.setAction(await PDFNet.Action.createGoto(await PDFNet.Destination.createFit(await pdfDoc.getPage(1))));
    await pdfDoc.addRootBookmark(myitem);

    // refresh document outline
    instance.UI.reloadOutline();
  });
});
Webviewer(...).then(async instance => {
  const { docViewer, PDFNet } = instance;
  await PDFNet.initialize();

  docViewer.on('documentLoaded', () => {
    const doc = docViewer.getDocument();
    const pdfDoc = await doc.getPDFDoc();

    // create outline and add it to the first page
    const myitem = await PDFNet.Bookmark.create(pdfDoc, 'My outline');
    await myitem.setAction(await PDFNet.Action.createGoto(await PDFNet.Destination.createFit(await pdfDoc.getPage(1))));
    await pdfDoc.addRootBookmark(myitem);

    // refresh document outline
    const bookmarks = await doc.getBookmarks();
    instance.updateOutlines(bookmarks);
  });
});

Last updated

Was this helpful?