When working with PDF documents, splitting pages is a common requirement—for example, extracting specific chapters from a multi-page report, distributing meeting minutes by participant, or saving each page of a scanned document as a separate file. This article details how to split PDF pages in C# using a free library, with code examples for various scenarios.
Installation: Install the
FreeSpire.PDFpackage via NuGet (Install-Package FreeSpire.PDF). This is a free community edition with no restrictions when processing up to 10 pages per operation. Exceeding 10 pages will result in truncation, so this library is suitable for lightweight tasks.
1. Core Approach
Splitting a PDF essentially means: load source document → select target pages → create a new document → import pages → save. Free Spire.PDF provides a complete PdfDocument model that supports page-level copying and independent output.
Basic workflow:
- Load the source PDF file.
- Determine which pages to split (a single page, a consecutive range, or a custom set).
- Create a new
PdfDocumentinstance. - Retrieve the specified pages from the source document and insert them into the new document.
- Save the new document and release resources.
2. Splitting Each Page into a Separate PDF
If you need to save each page of a PDF as an independent file, PdfDocument.Split is the simplest method.
Method Signature
public void Split(string destFilePattern, int startNumber);
-
destFilePattern: Output file path pattern; must contain the{0}placeholder, e.g.,"C:\\out\\page_{0}.pdf". -
startNumber: Number for the first file; typically set to1(creates files likepage_1.pdf,page_2.pdf, …).
Example
using Spire.Pdf;
namespace SplitPdfDemo
{
internal class Program
{
static void Main(string[] args)
{
// Load the source PDF
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");
// Split each page into a separate file, numbering starts at 1
pdf.Split("Output/Page_{0}.pdf", 1);
pdf.Close();
Console.WriteLine("Splitting completed. Each page has been saved in the Output folder.");
}
}
}
3. Grouping Pages by Fixed Size
When you need to merge every N pages into one PDF (e.g., every 3 pages as a single file), Split cannot be used directly. Instead, you must manually work with PdfDocument page insertion.
Approach
- Load the source document.
- Calculate the number of groups:
(totalPages + pagesPerGroup - 1) / pagesPerGroup. - For each group, create a new
PdfDocumentand copy the corresponding page range from the source. - Save the new document.
Code Example
using Spire.Pdf;
using System;
namespace SplitPdf
{
internal class Program
{
static void Main(string[] args)
{
string inputFile = "sample.pdf";
int pagesPerGroup = 3; // every 3 pages as one PDF
string outputPattern = "Group_{0}.pdf";
// 1. Load the source document
PdfDocument source = new PdfDocument();
source.LoadFromFile(inputFile);
int totalPages = source.Pages.Count;
int groupCount = (totalPages + pagesPerGroup - 1) / pagesPerGroup;
// 2. Create a new document for each group
for (int g = 0; g < groupCount; g++)
{
PdfDocument groupDoc = new PdfDocument();
// Calculate start and end indexes for the current group (0-based)
int startIdx = g * pagesPerGroup;
int endIdx = Math.Min(startIdx + pagesPerGroup - 1, totalPages - 1);
// Copy pages
for (int i = startIdx; i <= endIdx; i++)
{
groupDoc.InsertPage(source, source.Pages[i]);
}
// Save the current group
string outputFile = string.Format(outputPattern, g + 1);
groupDoc.SaveToFile(outputFile);
groupDoc.Close();
}
source.Close();
Console.WriteLine($"Splitting completed. Every {pagesPerGroup} pages have been merged into one file.");
}
}
}
4. Other Common PDF Splitting Scenarios
4.1 Extract Specific Non‑Consecutive Pages
For example, extract only pages 2, 5, and 7:
PdfDocument source = new PdfDocument();
source.LoadFromFile("Sample.pdf");
PdfDocument result = new PdfDocument();
int[] wanted = { 2, 5, 7 };
foreach (int p in wanted)
{
if (p >= 1 && p <= source.Pages.Count)
result.InsertPage(source, source.Pages[p - 1]); // note: pages are 0‑based
}
result.SaveToFile("Extracted.pdf");
result.Close();
source.Close();
4.2 Extract All Odd or Even Pages
PdfDocument source = new PdfDocument();
source.LoadFromFile("AnnualReport1.pdf");
PdfDocument oddPages = new PdfDocument();
PdfDocument evenPages = new PdfDocument();
for (int i = 0; i < source.Pages.Count; i++)
{
if (i % 2 == 0) // 0‑based index: 0 → page 1 (odd)
oddPages.InsertPage(source, source.Pages[i]);
else
evenPages.InsertPage(source, source.Pages[i]);
}
oddPages.SaveToFile("OddPages.pdf");
evenPages.SaveToFile("EvenPages.pdf");
oddPages.Close();
evenPages.Close();
source.Close();
5. Summary
| Requirement | Implementation | Core API |
|---|---|---|
| Each page as a separate file | pdf.Split("pattern_{0}.pdf", 1) |
Built‑in Split
|
| Every N pages merged into one file | Manual loop + InsertPage
|
PdfDocument.InsertPage |
| Extract specific pages | Manual filtering + InsertPage
|
Same as above |
All code has been tested in real projects and can be reused directly.
Top comments (0)