After merging scanned files, you may find that the pages are in the wrong order, or you may want to move pages from the end of a document to the beginning. You can reorder PDF pages by specifying page indices, without first converting the PDF into images.
This article uses Spire.PDF for .NET to demonstrate two approaches: arranging pages in a custom order and reversing the page order of an entire document. Both examples use the same method, PdfPageCollection.ReArrange(int[] orderArray). The difference lies in how the ordering array is generated.
1. Install Spire.PDF for .NET
Spire.PDF for .NET is a library for reading, creating, and processing PDF files in .NET applications. This article uses its document loading, page reordering, and file saving APIs.
Run the following command in the NuGet Package Manager Console in Visual Studio:
Install-Package Spire.PDF
Alternatively, use the .NET CLI from your project directory:
dotnet add package Spire.PDF
The examples below use C# top-level statements. Each example can be placed in the Program.cs file of a console project that supports this syntax and run separately. If you use a relative path for the input file, place the file in the application's working directory. You can also use an absolute path.
2. Understand Page Indices and the Ordering Array
Before reordering pages, distinguish between the page positions shown in a PDF reader and the page indices used in code: indices start at 0, so page 1 corresponds to index 0, page 2 corresponds to index 1, and so on.
ReArrange() accepts an integer array. Each value represents a page index in the original document, and the sequence of values determines the new page order.
For example, consider the following array for a four-page PDF:
new int[] { 1, 0, 2, 3 }
The mapping is as follows:
| Position After Reordering | Index in the Array | Page in the Original Document |
|---|---|---|
| Page 1 | 1 | Original page 2 |
| Page 2 | 0 | Original page 1 |
| Page 3 | 2 | Original page 3 |
| Page 4 | 3 | Original page 4 |
This array swaps the first two pages and leaves the remaining two in their original order.
This article focuses on reordering while retaining all pages. The array should therefore include every page index from the original document exactly once, with values ranging from 0 to pdf.Pages.Count - 1.
3. Reorder PDF Pages in a Custom Sequence
Custom ordering is useful when you already know the desired page sequence, such as when swapping the first two pages or moving a cover page to the beginning of a document.
The following example uses a PDF containing exactly four pages. It places the original second page before the first, producing this sequence: original page 2, original page 1, original page 3, and original page 4.
using Spire.Pdf;
// Create a document object and load the PDF
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("input.pdf");
// Reorder using the original page indices: swap the first two pages
pdf.Pages.ReArrange(new int[] { 1, 0, 2, 3 });
// Save to a new file and close the document
pdf.SaveToFile("ChangeOrder.pdf");
pdf.Close();
The code follows three steps:
- Load the original PDF with
LoadFromFile(). - Call
pdf.Pages.ReArrange()to reorder the pages according to the original page indices in the array. - Save the result with
SaveToFile(), then callClose()to close the document.
To move the last page of this four-page document to the beginning, simply change the array to { 3, 0, 1, 2 }.
The array in this example is hard-coded for a four-page document. For files with a different number of pages, construct a complete array based on the actual page count instead of using this array directly.
4. Reverse the Page Order of an Entire PDF
To place the last page first, the second-to-last page second, and so on until the original first page becomes the last, you can automatically generate a reversed index array based on the total page count.
Compared with entering the array manually, this approach is better suited to documents with varying page counts.
using Spire.Pdf;
using System.Linq;
// Create a document object and load the PDF
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Input.pdf");
// Get the total page count
int pageCount = pdf.Pages.Count;
// Generate page indices in reverse order
int[] reverseOrder = Enumerable.Range(0, pageCount)
.Reverse()
.ToArray();
// Reorder the pages using the reversed indices
pdf.Pages.ReArrange(reverseOrder);
// Save as a PDF file and close the document
pdf.SaveToFile("Reversed.pdf", FileFormat.PDF);
pdf.Close();
The reversed array is generated as follows:
-
Enumerable.Range(0, pageCount)generatespageCountconsecutive integers starting at 0, covering all page indices. -
Reverse()reverses the index sequence. -
ToArray()converts the result into the integer array required byReArrange().
For example, a five-page document initially produces the indices { 0, 1, 2, 3, 4 }. Reversing them gives { 4, 3, 2, 1, 0 }, so the output document contains the original pages in the order 5, 4, 3, 2, and 1.
5. Practical Considerations
1. Page Order and Printed Page Numbers Are Different
Reordering changes a page's position within the document. It does not rewrite page numbers in headers or footers to match the new positions. For example, if the original page 5 is moved to the beginning, any existing “Page 5” text on that page must be handled separately.
If the document contains a table of contents, bookmarks, or internal links, check that they point to the expected destinations after saving. A correct page sequence alone does not guarantee that all navigation elements have been updated accordingly.
2. Match the Custom Array to the Actual Page Count
When reordering while retaining all pages, check the array length, index range, and duplicate values before calling the method. The numbers in the array represent original page indices; do not enter the one-based page numbers shown in a PDF reader directly.
3. Use Different Input and Output Paths
The examples save the results as new files to make comparison easier. When checking the output, confirm the page count first, then inspect the first page, the last page, and any pages that were moved.
4. Distinguish Example Code from Production Code
Exception handling is omitted here to keep the core page reordering logic clear. Production applications should also handle invalid input paths, file reading failures, and unwritable output directories. They should ensure that the document is closed and related resources are released even if an exception occurs.
Also, Spire.PDF for .NET and Free Spire.PDF for .NET are different packages. When using a trial license or the free edition, check the licensing terms and processing limits for your specific version so that these restrictions are not mistaken for problems with the page reordering logic.
6. Conclusion
The key to reordering PDF pages with Spire.PDF for .NET is to construct an array of original page indices that represents the desired sequence. If the target order is already known, specify the array directly. To reverse an entire document, generate the indices from Pages.Count and reverse them using LINQ.
With either approach, remember that indices start at 0, and check both the actual page order and the document's navigation elements after saving.

Top comments (0)