# Adjust PDF Page Order with Python

In daily work, we often need to organize PDF documents (such as reports, contracts, and data collections). Common requirements include swapping out-of-order pages, rearranging pages according to custom logic, and reversing page order. Manual operation with tools like Adobe Acrobat is not only inefficient but may also involve paid subscriptions. In contrast, implementing automated processing via Python code can both improve efficiency and adapt to batch document scenarios. This article will introduce how to use **Free Spire.PDF for Python** (a free PDF processing library) to quickly achieve flexible adjustment of PDF page order.

---

## 1. Environment Preparation
### 1.1 Install the Free Python Library
Free Spire.PDF for Python is a lightweight free PDF processing library that does not depend on third-party software such as Adobe Acrobat. It can be quickly installed via pip:
```bash
pip install Spire.PDF.Free
```

### 1.2 Core Advantages of the Library
- Free and easy to use with an intuitive API design;
- Supports operations such as adding, deleting, moving, rearranging, and rotating PDF pages;
- Compatible with mainstream PDF formats without additional dependencies.

> Note: The free version has a slight limit on the number of document pages (supports processing up to 10 pages), making it suitable for personal or small-scale projects.

---

## 2. Core ReArrange Method
Free Spire.PDF for Python provides a concise built-in method **`ReArrange`**, which takes a list of integers as a parameter. The elements in the list are the indices of the original PDF pages, and the order of the list represents the new page order. Core advantages:
- Natively built-in method that eliminates the need to manually create a new PDF document, reducing code volume by more than 50%;
- Modifies the page order directly;
- Supports any custom page index sequence to adapt to all rearrangement scenarios.

---

## 3. Python Code Example for Rearranging PDF Pages
**Requirement**: Adjust the original PDF page order (1, 2, 3, 4) to 4, 3, 1, 2 (corresponding indexes: 3, 2, 0, 1):
```python
from spire.pdf.common import *
from spire.pdf import *

inputFile = "Sample.pdf"
outputFile = "Rearranged_PDF_Pages.pdf"

# Load the PDF file
pdf = PdfDocument()
pdf.LoadFromFile(inputFile)

# Adjust the PDF page order
pdf.Pages.ReArrange([3, 2, 0, 1])

# Save the document
pdf.SaveToFile(outputFile, FileFormat.PDF)
pdf.Close()
```
**Notes**:
- **Page Indexes**: The page index of Free Spire.PDF starts from 0. It is essential to ensure that the length of the passed index list is consistent with the number of pages in the original PDF; otherwise, an index out-of-bounds exception will be thrown.
- **Resource Release**: `pdf.Close()` is a mandatory operation to release the memory occupied by the PDF document and avoid memory leaks caused by long-term operation.

---

## 4. Summary
The `ReArrange` method greatly simplifies the code volume for rearranging PDF pages, requiring only one core line of code to complete adjustments in any order. The code in this article can be directly reused, adapting to most PDF page order adjustment needs in daily office work, and is an efficient solution for Python-based automated PDF processing.
