How to Reorder PDF Pages Alphabetically with Acrobat JavaScript
Question details
Reorder PDF pages based on an alphabetically sorted array while handling the changing dynamic page indexes in Acrobat.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Executing custom JavaScript to sort and alphabetize PDF pages based on specific names or content stored in an array.
- Observed behavior
- Page indexes shift dynamically after each move operation, breaking the script because the original stored page numbers are no longer accurate.
Before running automated scripts on your documents, always create a backup copy of your original PDF to prevent unrecoverable data loss in case of a scripting error.
Create a New Document and Insert Pages in Order
This is the simplest and safest approach. Instead of calculating shifting indexes, extract or copy pages into a newly generated PDF sequentially.
Because PDF page indexes automatically close gaps and shift when a page is moved, managing the sequence in a single document requires complex math. Copying pages to a blank document bypasses this issue entirely.
Use JavaScript to sort your array of page names alphabetically.
Use the 'app.newDoc()' method in your Acrobat console to initialize a completely new, empty PDF file.
Loop through your sorted array and use the 'insertPages' method to copy the corresponding page from the original document into the newly created document.
Once the loop is complete, save the newly generated document containing the correctly alphabetized pages.

Update Array Indexes Dynamically After Each Move
If you must manipulate the original document directly, manually recalculate and update the stored page indexes after every single move operation.
Visually Reorder PDF Pages Using WPS Office
Skip the complicated JavaScript calculations and dynamic arrays entirely. WPS PDF provides a highly intuitive drag-and-drop interface, allowing you to instantly reorder, sort, and organize PDF pages visually without writing a single line of code.
- 1. Open Your PDF: Launch WPS Office and open your target PDF document using the built-in WPS PDF tool.
- 2. Access the Page Manager: Navigate to the 'Page' tab located on the top ribbon menu to reveal the thumbnail view of your entire document.
- 3. Drag to Reorder: Click and hold the thumbnail of the page you want to move, then drag it to your desired alphabetical position.
- 4. Save the Changes: Once all pages are sorted visually, click the 'Save' icon to apply and lock in the new sequence permanently.

Frequently Asked Questions
Why do page indexes change when moving PDF pages via script?
In Adobe Acrobat, PDF page indexes are dynamic and zero-based. When you move or delete a page, the document immediately adjusts the sequence to close the gap, shifting the index number of all subsequent pages.
Is it faster to move pages within the original file or create a new PDF?
Moving pages directly within the original file using JavaScript is technically more efficient in terms of processing time, but creating a new PDF file and extracting pages is significantly easier to script and far less prone to indexing logic errors.
Do I need to know JavaScript to alphabetize my PDF pages?
Not at all. While Acrobat allows advanced manipulation via JavaScript, you can easily alphabetize or custom-sort pages manually using the visual drag-and-drop page manager found in programs like WPS PDF or Acrobat's native Page Organizer.




