Remove Pages from a PDF Using JavaScript

You can remove pages in two ways using our page editor API.

Using keepPages removes all the pages from the document except for the pages specified in the pageIndexes array:

instance.applyOperations([
  {
    type: "keepPages",
    pageIndexes: [0, 1, 2] // Remove all pages except pages 0 to 2.
  }
]);

Using removePages removes the pages specified in the pageIndexes array:

instance.applyOperations([
  {
    type: "removePages",
    pageIndexes: [8, 9, 11] // Remove pages 8, 9, and 11.
  }
]);

After this operation is complete, call exportPDF to get a buffer containing the data for the final PDF.

If you need to apply this operation and export in one step, you can provide the same argument you provide to applyOperations to exportPDFWithOperations to get a buffer containing the data for the final PDF.