Split PDF pages using JavaScript

To split a PDF document into multiple pages.

JavaScript

1async function main() {
2 const doc = await PDFNet.PDFDoc.createFromURL(filename);
3 const page_num = await doc.getPageCount();
4 for (let i=1; i<=page_num; ++i)
5 {
6 const newDoc = await PDFNet.PDFDoc.create();
7 newDoc.insertPages(0, doc, i, i, PDFNet.PDFDoc.InsertFlag.e_none);
8 const buf = await newDoc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
9
10 //optionally save the blob to a file or upload to a server
11 const blob = new Blob([buf], { type: 'application/pdf' });
12 }
13}
14PDFNet.runWithCleanup(main);

Merge, Copy, Delete, and Rearrange PDF Pages
Full code sample which illustrates how to copy pages from one document to another, how to delete, and rearrange pages and how to use ImportPages().

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales