---
title: "Managing PDF page order | Nutrient Python SDK"
canonical_url: "https://www.nutrient.io/guides/python/editor/manage-pdf-page-order/"
md_url: "https://www.nutrient.io/guides/python/editor/manage-pdf-page-order.md"
last_updated: "2026-06-08T19:21:59.260Z"
description: "How to manage PDF page order using Nutrient Python SDK."
---

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

[Download sample](https://www.nutrient.io/downloads/samples/python/manage-pdf-page-order.zip)

## 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:

```python

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:

```python

def main():

```

The `Document.open()` call opens the PDF document. The [context manager](https://docs.python.org/3/reference/datamodel.html#context-managers) 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:

```python

    try:
        with Document.open("input.pdf") as document:
            editor = PdfEditor.edit(document)
            pages = editor.page_collection
            initial_count = pages.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:

```python

            first_page = pages.first
            print(f"First page - Width: {first_page.width}, Height: {first_page.height}")

            last_page = pages.last
            print(f"Last page - Width: {last_page.width}, Height: {last_page.height}")

            page1 = pages.get_page(1)
            print(f"Page 1 - PageNumber: {page1.page_number}, Rotation: {page1.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:

```python

            for page in pages:
                print(f"Page: {page.width}x{page.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:

```python

            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:

```python

            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:

```python

            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`:

```python

            pages.remove_at(pages.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.
---

## Related pages

- [Merging PDFs](/guides/python/editor/merge-pdf-into-other-pdf.md)
- [Adding invisible digital signatures to a PDF document](/guides/python/editor/add-invisible-signature-to-pdf.md)
- [Nutrient Python SDK editor guides](/guides/python/editor.md)
- [Adding sticky note annotations to a PDF document](/guides/python/editor/add-sticky-note-annotations-to-pdf.md)
- [Adding annotations to a PDF document](/guides/python/editor/add-annotations-to-pdf.md)
- [Editing PDF form fields](/guides/python/editor/editing-pdf-form-fields.md)
- [Advanced digital signature workflows](/guides/python/editor/advanced-digital-signatures.md)
- [Adding redaction annotations to a PDF document](/guides/python/editor/add-redaction-annotations-to-pdf.md)
- [Adding visible digital signatures to a PDF document](/guides/python/editor/add-visible-signature-to-pdf.md)
- [Adding a custom page to a PDF document](/guides/python/editor/add-custom-page-to-pdf.md)
- [Adding free text annotations to a PDF document](/guides/python/editor/add-freetext-annotations-to-pdf.md)
- [Adding link annotations to a PDF document](/guides/python/editor/add-link-annotations-to-pdf.md)
- [Detecting and adding form fields to a PDF document](/guides/python/editor/detect-and-add-form-fields.md)
- [Editing PDF metadata with Nutrient Python SDK](/guides/python/editor/editing-pdf-metadata.md)
- [Filling PDF form fields](/guides/python/editor/fill-pdf-form.md)
- [Adding stamp annotations to a PDF document](/guides/python/editor/add-stamp-annotations-to-pdf.md)
- [Adding text markup annotations to a PDF document](/guides/python/editor/add-text-markup-annotations-to-pdf.md)
- [Adding shape annotations to a PDF document](/guides/python/editor/add-shape-annotations-to-pdf.md)
- [Adding interactive form fields to a PDF document](/guides/python/editor/add-form-fields-to-pdf.md)

