Reordering pages in a PDF document using JavaScript

To reorder pages in a PDF document by reversing the order of pages.

Given the copy and delete page operations described in other sections, it is easy to re-arrange and sort pages.

JavaScript

1async function main() {
2 const doc = await PDFNet.PDFDoc.createFromURL(filename);
3
4 const page_num = await doc.getPageCount();
5 for (let i=1; i<=page_num; ++i)
6 {
7 const itr = await doc.getPageIterator(i);
8 const page = await itr.current();
9 doc.pageRemove(itr);
10 doc.pagePushFront(page);
11 }
12}
13PDFNet.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