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 without manual PDF editing.

How Nutrient helps you achieve this

Nutrient Python SDK handles PDF page manipulation and document structure management. With the SDK, you don’t need to worry about:

  • Parsing PDF page tree structures
  • Managing page inheritance hierarchies
  • Handling content stream updates
  • Complex index recalculation after modifications

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 Python application. The import statements bring in all the necessary classes from the Nutrient SDK:

from nutrient_sdk import Document
from nutrient_sdk import PdfEditor
from nutrient_sdk import PdfPageSizes
from nutrient_sdk import NutrientException

The main() function defines the entry point that will contain the page manipulation logic:

def main():

The Document.open() call opens the PDF document. The context manager(opens in a new tab) syntax ensures the document is automatically closed when you’re done, preventing resource leaks. The PdfEditor.edit() method creates a PdfEditor instance and retrieves the page collection. The editor provides methods for accessing and modifying the document’s page structure:

try:
with Document.open("input.pdf") as document:
editor = PdfEditor.edit(document)
pages = editor.get_page_collection()
initial_count = pages.get_count()

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:

first_page = pages.get_first()
print(f"First page - Width: {first_page.get_width()}, Height: {first_page.get_height()}")
last_page = pages.get_last()
print(f"Last page - Width: {last_page.get_width()}, Height: {last_page.get_height()}")
page1 = pages.get_page(1)
print(f"Page 1 - PageNumber: {page1.get_page_number()}, Rotation: {page1.get_rotation()}")

The following code block iterates through all pages using a for loop. Iteration is useful for batch operations like analyzing document structure or applying transformations to every page:

for page in pages:
print(f"Page: {page.get_width()}x{page.get_height()}")

The following lines add new pages to the document. The first adds a page with custom dimensions (612×792 points for US Letter), while the second uses predefined page sizes like A4 for convenience:

new_page = pages.add(612.0, 792.0)
print("Added Letter size page")
a4_page = pages.add(page_size=PdfPageSizes.PDF_PAGE_SIZE_A4)
print("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:

inserted_page = pages.insert(0, 500.0, 700.0)
print("Inserted page at index 0")

The following code blocks demonstrate page reordering operations. The swap method exchanges two pages by index, while move_to relocates a page from one position to another, shifting intervening pages automatically:

pages.swap(0, 1)
print("Swapped pages at index 0 and 1")
pages.move_to(0, 2)
print("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. The final code block saves the changes and closes the editor. The try-except block handles potential errors using NutrientException:

pages.remove_at(pages.get_count() - 1)
print("Removed last page")
editor.save_as("output.pdf")
editor.close()
except NutrientException as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()

Conclusion

The page management logic consists of several key operations:

  1. Open the document and create an editor.
  2. Access the page collection and individual pages.
  3. Iterate through pages for batch operations.
  4. Add pages with custom or predefined dimensions.
  5. Insert pages at specific positions.
  6. Reorder pages using swap and move operations.
  7. Remove pages by index.
  8. 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.