Managing PDF page order
Managing PDF page order programmatically enables teams to automate document assembly, reorganize content, and build dynamic PDF workflows. Whether you’re building document merging systems, implementing page reordering tools, or creating automated report generators, the page collection API provides complete control over page structure, allowing you to add, insert, move, swap, and remove pages without manual PDF editing.
How Nutrient helps you achieve this
Nutrient Java SDK handles PDF page manipulation and document structure management. With the SDK, you don’t need to worry about:
- Parsing PDF page tree structures and object references
- Managing page inheritance hierarchies and resource dictionaries
- Handling content stream updates and media box adjustments
- Complex index recalculation after modifications and page tree rebalancing
Instead, Nutrient provides an API that handles all the complexity behind the scenes, letting you focus on your business logic.
Complete implementation
Below is a complete working example that demonstrates managing PDF page order. The following lines set up the Java application. The package declaration and import statements bring in all the necessary classes from the Nutrient SDK:
package io.nutrient.Sample;
import io.nutrient.sdk.*;import io.nutrient.sdk.editors.*;import io.nutrient.sdk.editors.pdf.pages.*;import io.nutrient.sdk.enums.*;
public class ManagePdfPageOrder {The main method defines the entry point that will contain the page manipulation logic:
public static void main(String[] args) {The Document.open() call opens the PDF document. The try-with-resources statement ensures the document is automatically closed when you’re done, preventing resource leaks:
try (Document document = Document.open("input.pdf")) { PdfEditor editor = PdfEditor.edit(document); PdfPageCollection pages = editor.getPageCollection(); int initialCount = pages.getCount();The following lines demonstrate different ways to access pages. You can get the first or last page directly, or access any page by its 1-based page number. Each page object provides properties like dimensions and rotation that describe the page’s characteristics:
PdfPage firstPage = pages.getFirst(); System.out.println("First page - Width: " + firstPage.getWidth() + ", Height: " + firstPage.getHeight());
PdfPage lastPage = pages.getLast(); System.out.println("Last page - Width: " + lastPage.getWidth() + ", Height: " + lastPage.getHeight());
PdfPage page1 = pages.getPage(1); System.out.println("Page 1 - PageNumber: " + page1.getPageNumber() + ", Rotation: " + page1.getRotation());The following code block iterates through all pages using a for-each loop. Iteration is useful for batch operations like analyzing document structure, applying transformations to every page, or collecting statistics about page dimensions:
for (PdfPage page : pages) { System.out.println("Page: " + page.getWidth() + "x" + page.getHeight()); }The following lines add new pages to the document. The first adds a page with custom dimensions (612×792 points for US Letter size), while the second uses predefined page sizes like A4 for convenience. Both methods append pages to the end of the document:
PdfPage newPage = pages.add(612.0f, 792.0f); System.out.println("Added Letter size page");
PdfPage a4Page = pages.add(PdfPageSizes.PdfPageSizeA4); System.out.println("Added A4 page");The following code inserts a page at a specific position. The insert method takes an index and dimensions, allowing you to place pages precisely within the document structure rather than just appending. The method is useful for inserting cover pages, section dividers, or content at specific locations:
PdfPage insertedPage = pages.insert(0, 500.0f, 700.0f); System.out.println("Inserted page at index 0");The following code blocks demonstrate page reordering operations. The swap method exchanges two pages by their index positions, while moveTo relocates a page from one position to another, automatically shifting intervening pages to maintain continuity:
pages.swap(0, 1); System.out.println("Swapped pages at index 0 and 1");
pages.moveTo(0, 2); System.out.println("Moved page from index 0 to index 2");The following code removes a page by index. The example removes the last page by using the current page count minus one, demonstrating safe index calculation for removal operations. Page indices are zero-based, so the last page is always at count - 1:
pages.removeAt(pages.getCount() - 1); System.out.println("Removed last page");
editor.saveAs("output.pdf"); editor.close(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); e.printStackTrace(); } }}Conclusion
The page management logic consists of several key operations:
- Open the document and create an editor.
- Access the page collection and individual pages.
- Iterate through pages for batch operations.
- Add pages with custom or predefined dimensions.
- Insert pages at specific positions.
- Reorder pages using swap and move operations.
- Remove pages by index.
- Save and close the editor.
Nutrient handles PDF page tree structures and content stream updates so you don’t need to understand page inheritance hierarchies or manage index recalculation manually.