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.
How Nutrient helps you achieve this
Nutrient .NET 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. Start with the SDK imports:
using Nutrient;Open the PDF document and create the editor with using declarations(opens in a new tab) so the runtime disposes both when you’re done, preventing resource leaks. PdfEditor.Edit() creates a PdfEditor instance, and the PageCollection property retrieves the page collection. The editor provides methods for accessing and modifying the document’s page structure:
try{ using Document document = Document.Open("input.pdf"); using PdfEditor editor = PdfEditor.Edit(document); var pages = editor.PageCollection; Console.WriteLine($"Document has {pages.Count} page(s)");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:
var firstPage = pages.First ?? throw new InvalidOperationException("The document has no pages."); Console.WriteLine($"First page - Width: {firstPage.Width}, Height: {firstPage.Height}");
var lastPage = pages.Last ?? throw new InvalidOperationException("The document has no pages."); Console.WriteLine($"Last page - Width: {lastPage.Width}, Height: {lastPage.Height}");
var page1 = pages.GetPage(1); Console.WriteLine($"Page 1 - PageNumber: {page1.PageNumber}, Rotation: {page1.Rotation}");The following code block iterates through all pages. Iteration is useful for batch operations like analyzing document structure or applying transformations to every page:
for (int i = 1; i <= pages.Count; i++) { var page = pages.GetPage(i); Console.WriteLine($"Page: {page.Width}x{page.Height}"); }The following lines add new pages to the document. The first call — Add — adds a page with custom dimensions (612×792 points for US Letter), while the second — Add — uses predefined page sizes like A4 for convenience:
pages.Add(612.0f, 792.0f); Console.WriteLine("Added Letter size page");
pages.Add(PdfPageSizes.PdfPageSizeA4); Console.WriteLine("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:
pages.Insert(0, 500.0f, 700.0f); Console.WriteLine("Inserted page at index 0");The following code blocks demonstrate page reordering operations. The Swap method exchanges two pages by index, while MoveTo relocates a page from one position to another, shifting intervening pages automatically:
pages.Swap(0, 1); Console.WriteLine("Swapped pages at index 0 and 1");
pages.MoveTo(0, 2); Console.WriteLine("Moved page from index 0 to index 2");The following code removes a page by index with RemoveAt. 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 then saves the changes to a new file. The catch block handles potential errors using NutrientException.
pages.RemoveAt(pages.Count - 1); Console.WriteLine("Removed last page");
editor.SaveAs("output.pdf"); Console.WriteLine("Successfully saved to output.pdf");}catch (NutrientException e){ Console.Error.WriteLine($"Error: {e.Message}"); Environment.Exit(1);}Conclusion
The page management logic consists of several key operations:
- 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 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.