Reorder PDF pages by API with one list of numbers

Reorder the pages of a PDF with one API call: send the new order as a list of page numbers. Duplicate a page by repeating it, drop one by leaving it out.

Pages 1, 2 and 3 reordered to 3, 1, 2 by one list of numbers

Most page-order jobs come down to one list. Put the summary first. Move the appendix to the end. Put the signed page back where it belongs. The PDF Organize API takes that list and builds the new document.

The order list

Send order as a list of 1-based page numbers. The result has exactly those pages, in that order:

curl https://api.justapi.tech/v1/pdf/organize \
  -H "Authorization: Bearer ak_live_..." \
  -H "Content-Type: application/json" \
  -d '{"source": {"url": "https://files.example.com/deck.pdf"}, "options": {"order": [3, 1, 2, 4, 5]}}'

Three rules cover everything else:

  • Leave a page out to drop it. [1, 2, 4] on a 4-page file drops page 3.
  • Repeat a number to duplicate a page. [1, 2, 3, 1] puts the cover at the end too.
  • Ranges work inside the list. ["5-", 1, "2-4"] moves pages 5 to the end to the front.

Negative numbers count from the end, so [-1, "1--2"] moves the last page to the front without knowing how many pages there are.

Building the list in your app

If your users drag thumbnails to reorder pages, keep the original page number on each thumbnail and send the list in display order when they save. To get the thumbnails, render the pages once with the PDF to Image API at a low DPI.

Lossless, and checked

Pages are copied as they are, with their fonts and images. Nothing is re-rendered. stats.pages in the response tells you how many pages the result has, and stats.source_pages how many the source had, so you can check the list did what you meant.

Failures and cost

A page number that does not exist fails with invalid_request and names the number. A signed PDF fails with signed_pdf; an encrypted one with encrypted_pdf. None of these are charged. A successful call costs $0.005 at the first tier.

If you only need to keep or drop pages without reordering, extract pages and remove pages are simpler. The free page organizer lets you test an order on one file first.