PDF Editing Issues

Sort PDF Pages by Size and Orientation with Acrobat JavaScript

Rana GarciaRana Garcia Nov 19, 2026 868 views

Question details

The user wants to sort mixed-size PDF pages into orientation groups and order each group by page dimensions.

Sort PDF Pages by Size and Orientation with Acrobat JavaScript
Product
Adobe Acrobat
Device & OS
not provided
Scenario
A PDF contains pages of different sizes and orientations that need to be grouped and reordered using JavaScript.
Observed behavior
The desired output is a PDF with portrait, square, and landscape pages grouped and sorted according to a defined page box and dimension criteria.
Before you start

Decide which page box and sorting criteria to use. The example below uses the Crop Box, groups pages as portrait, square, then landscape, and sorts each group by width and then height; test it on a saved copy first.

Solution 1Recommended

Sort pages with an Acrobat JavaScript console script

Use this approach in Adobe Acrobat when you want to create a new PDF sorted by Crop Box dimensions.

The script treats dimensions within 0.01 PDF points as square. Pages with equal dimensions retain their original relative order. Page rotation and the selected page box can affect the measured dimensions, so check the result against representative pages.

The source PDF must be saved and accessible by file path. Document security restrictions may prevent page extraction or insertion.

1
Open a saved copy

In Acrobat, use File > Save As to save a copy, then open that copy and make sure it is the active document.

2
Open the JavaScript console

Open the JavaScript console with Ctrl+J on Windows or Command+J on macOS. Enable Acrobat JavaScript in Preferences if it is disabled.

3
Run the sorting script

Paste the script below into the console, select it, and run it with Ctrl+Enter or Command+Enter: var src = this; var sourcePath = src.path; if (!sourcePath) throw new Error('Save the source PDF first.'); var groups = [[], [], []]; for (var i = 0; i < src.numPages; i++) { var box = src.getPageBox('Crop', i); var w = Math.abs(box[2] - box[0]); var h = Math.abs(box[1] - box[3]); var group = Math.abs(w - h) <= 0.01 ? 1 : (w < h ? 0 : 2); groups[group].push({page: i, width: w, height: h}); } function comparePages(a, b) { return (a.width - b.width) || (a.height - b.height) || (a.page - b.page); } var order = []; for (var g = 0; g < groups.length; g++) { groups[g].sort(comparePages); order = order.concat(groups[g]); } var out = app.newDoc(); for (var j = 0; j < order.length; j++) { out.insertPages({nPage: out.numPages - 1, cPath: sourcePath, nStart: order[j].page, nEnd: order[j].page}); } out.deletePages(0); app.alert('Pages sorted. Review and save the new PDF.');

4
Review and save the result

In the new PDF, inspect the page thumbnails and dimensions, then choose File > Save As to save the sorted document under a new name.

Sort pages with an Acrobat JavaScript console script
Choose a different page box: Replace 'Crop' with 'Trim' in getPageBox to sort by the Trim Box instead. To sort by area rather than width and height, change the comparator to compare width × height and define a tie-breaker.
Reorder PDF pages

Manually arrange PDF pages in WPS Office

Use WPS PDF page management to arrange pages in a chosen size and orientation order. This is a manual alternative; it does not run the Acrobat JavaScript sort.

  1. 1. Determine the order: Check the relevant page-box dimensions in a PDF tool that displays them, and write down the desired portrait, square, and landscape page order.
  2. 2. Open the PDF: In WPS Office, select Open and choose the PDF you want to rearrange.
  3. 3. Arrange the pages: Open Page Management or Organize Pages in WPS PDF, then drag the page thumbnails into the order you recorded.
  4. 4. Save the result: Review the thumbnail sequence and use File > Save As to save the reordered PDF with a new name.
Reorder mixed-size PDF pages using page thumbnailsOpen and work with PDF files as well as Microsoft Word, Excel, and PowerPoint formatsReview the page sequence before saving the reordered file
microsoft office alternative - wps office

Frequently Asked Questions

Should I sort by the Crop Box or the Trim Box?

Use the Crop Box when the displayed page boundary is your criterion. Use the Trim Box when the intended trimmed page size matters. In the script, change getPageBox('Crop', i) to getPageBox('Trim', i) to use the Trim Box.

How does the script handle pages with the same dimensions?

Within each orientation group, the comparator sorts by width, then height, and finally the original page index. Pages with identical measured dimensions therefore remain in their original relative order.

Why might a rotated page appear in an unexpected group?

Rotation and page-box settings can affect the dimensions returned for a page. Check the page's rotation and the selected box in Acrobat, and test representative pages before relying on the sorted output.

Can WPS Office automatically run this Acrobat JavaScript?

The script is intended for the Acrobat JavaScript console. In WPS PDF, you can instead use Page Management or Organize Pages to drag thumbnails into a manually determined order.