Some test text!

menu
Split pageskeyboard_arrow_down

Extracting pages from PDF using JavaScript

If the user only wants to download part of a document, the extractPages function can be used to download specific pages. For example:

WebViewer(...)
  .then(instance => {
    const { docViewer, annotManager } = instance;

    docViewer.on('documentLoaded', async () => {
      const doc = docViewer.getDocument();
      const pagesToExtract = [2, 3];

      // only include annotations on the pages to extract
      const annotList = annotManager.getAnnotationsList().filter(annot => pagesToExtract.indexOf(annot.PageNumber) > -1);
      const xfdfString = await annotManager.exportAnnotations({ annotList });
      const data = await doc.extractPages(pagesToExtract, xfdfString);
      const arr = new Uint8Array(data);
      const blob = new Blob([arr], { type: 'application/pdf' });

      // in this example we are using FileSaver.js library  'SaveAs' function but any technique to save a blob works
      saveAs(blob, 'downloaded.pdf');
    });
  });

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close