Skip to main content

Split PDFs

Split PDFs

Extracting a page range lets you share or process one section of a longer document separately. Extract two page ranges from an existing PDF into separate documents. This example follows SplitPdf.cs and uses DocumentD.pdf as its source.

Fluent recipe / 21Split PDFsExtract pages 1-4 and 5-12 of DocumentD.pdf into separate PDFs.

What you’ll create​

Output fileSource pagesPage count
SplitPdf_part1_output.pdf1–44
SplitPdf_part2_output.pdf5–128

The source must contain at least 12 pages. Pages after page 12 are not included in either output, and the source PDF remains unchanged.

Example code​

Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. The class extracts two page ranges and saves the results to an existing output directory.

Ensure DocumentD.pdf is in the Resources folder.

using System;
using System.IO;
using DynamicPDF.Flow;
using DynamicPDF.Flow.Merger;

namespace FluentExamples.Examples;

internal static class SplitPdf
{
public static void Run(string outputRoot)
{
var inputPath = Path.Combine(
AppContext.BaseDirectory,
"Resources", "DocumentD.pdf");
var source = PdfSource.FromFile(inputPath);

var part1Path = Path.Combine(
outputRoot, "SplitPdf_part1_output.pdf");

DocumentBuilder.Create()
.Pdf(source, startPage: 1, pageCount: 4)
.Save(part1Path);

Console.WriteLine(
$"Created {Path.GetFullPath(part1Path)}");

var part2Path = Path.Combine(
outputRoot, "SplitPdf_part2_output.pdf");

DocumentBuilder.Create()
.Pdf(source, startPage: 5, pageCount: 8)
.Save(part2Path);

Console.WriteLine(
$"Created {Path.GetFullPath(part2Path)}");
}
}
Imports System
Imports System.IO
Imports DynamicPDF.Flow
Imports DynamicPDF.Flow.Merger

Friend Module SplitPdf
Public Sub Run(outputRoot As String)
Dim inputPath = Path.Combine(
AppContext.BaseDirectory,
"Resources", "DocumentD.pdf")
Dim source = PdfSource.FromFile(inputPath)

Dim part1Path = Path.Combine(
outputRoot, "SplitPdf_part1_output.pdf")

DocumentBuilder.Create().
Pdf(source, startPage:=1, pageCount:=4).
Save(part1Path)

Console.WriteLine(
$"Created {Path.GetFullPath(part1Path)}")

Dim part2Path = Path.Combine(
outputRoot, "SplitPdf_part2_output.pdf")

DocumentBuilder.Create().
Pdf(source, startPage:=5, pageCount:=8).
Save(part2Path)

Console.WriteLine(
$"Created {Path.GetFullPath(part2Path)}")
End Sub
End Module

How it works​

1. Open the source PDF​

Path.Combine locates DocumentD.pdf in the Resources folder relative to the application directory. PdfSource.FromFile(inputPath) creates a source that both output documents reuse.

2. Prepare the output directory​

Make sure outputRoot names an existing output directory before calling Run. Path.Combine builds both output paths.

3. Extract pages 1–4​

The first DocumentBuilder.Create() starts a new document. .Pdf(source, startPage: 1, pageCount: 4) imports four pages beginning at page 1, preserving their order, and .Save(part1Path) writes the first output.

4. Extract pages 5–12​

A second builder starts a separate document. .Pdf(source, startPage: 5, pageCount: 8) imports eight pages beginning at page 5. The pageCount argument is a count, not the last page number. .Save(part2Path) writes the second output.

5. Locate the results​

Both PDFs are saved directly in outputRoot. Saving overwrites existing files with the same names. Console.WriteLine prints each full output path after its PDF is saved.

Next steps​

Adjust startPage and pageCount to extract different ranges within the source document. Explore Combine PDFs to join documents, or browse all examples.