Extract Pages from PDFs Using JavaScript

PSPDFKit for Web enables you to combine pages from multiple documents by importing pages from one document into another.

Before importing, you’ll need to load the PDF that will receive the pages. This is done using the pspdfkit#load() method.

The following example shows how to load the PDF in headless mode so that the user isn’t presented with a UI:

PSPDFKit.load({
  document: documentURL,
  headless: true
});

To import the pages of the other document, you’ll need to use the importDocument operation.

Provide the document you wish to import as a Blob or file in the same way you would with the load method.

Additionally, to specify which pages should be imported, you’ll need to provide a few other things.

First, specify where the pages should be inserted:

  • afterPageIndex — The index of the page in the new document the new pages should be imported after

  • beforePageIndex — The index of the page in the new document the new pages should be imported before

After that, specify which pages you want to import:

  • importedPageIndexes — An array containing the index of the pages that should be inserted from the document being imported

Finally, specify that the imported pages should be treated as one page for any additional operations applied at the same time by passing true to treatImportedDocumentAsOnePage.

This means you only need to specify the index of where the pages will be inserted for future operations instead of having to calculate and provide the index for every single imported page.

In the example below, the pages 0, 2, and 5 from the imported document will be inserted before page 0 in the loaded document:

PSPDFKit.load({
  document: documentURL,
  headless: true
}).then((instance) => {
  instance.applyOperation([
    {
      type: "importDocument",
      document: otherDocumentBlob,
      beforePageIndex: 0,
      importedPageIndexes: [0, 2, 5]
    }
  ]);
});

Exporting a PDF

After this operation is complete, you can call instance#exportPDF to get an ArrayBuffer containing the data for the final PDF.

If you need to apply this operation and export the resulting document in one step, you can provide the same argument passed to instance#applyOperations to instance#exportPDFWithOperations instead, and it’ll resolve to an ArrayBuffer containing the final PDF.