Managing PDF page order
Use page-order operations to assemble and reorganize PDF documents.
Common use cases include:
- Reordering report sections
- Inserting cover and divider pages
- Building document assembly pipelines
- Removing unwanted pages before export
This guide shows how to add, insert, move, swap, and remove pages.
Download sampleHow Nutrient helps
Nutrient Java SDK handles page manipulation and document structure updates.
The SDK handles:
- 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
Complete implementation
This example demonstrates common page-order operations in one workflow:
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 {Create the main method as the sample entry point:
public static void main(String[] args) {Open the document with try-with-resources, create an editor, and get the page collection:
try (Document document = Document.open("input.pdf")) { PdfEditor editor = PdfEditor.edit(document); PdfPageCollection pages = editor.getPageCollection(); int initialCount = pages.getCount();Access pages in different ways:
- First page with
getFirst() - Last page with
getLast() - Specific page by 1-based number with
getPage(1)
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());Iterate through pages for analysis or batch operations:
for (PdfPage page : pages) { System.out.println("Page: " + page.getWidth() + "x" + page.getHeight()); }Add pages to the end of the document:
- Custom size with
add(width, height) - Predefined size with
add(PdfPageSizes.…)
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");Insert a page at a specific index with insert(index, width, height):
PdfPage insertedPage = pages.insert(0, 500.0f, 700.0f); System.out.println("Inserted page at index 0");Reorder pages with:
swap(indexA, indexB)to exchange two pagesmoveTo(fromIndex, toIndex)to relocate one page
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");Remove a page by index.
In this sample, the last page is removed with pages.getCount() - 1 (zero-based index):
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
Use this workflow to manage PDF page order:
- 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.
For related document workflows, refer to the Java SDK guides.