Split PDF file in C# and VB.NET
With GemBox.Pdf, you can split a PDF file into several PDF files in your C# or VB.NET application.
As PDF pages are self-contained (all information required for their appearance and interactivity is either stored or referenced from the page), they can be easily cloned to other PDF files.
To split a PDF file, you simply need to clone each of its pages into a new PDF file.
The following example shows how you can split a PDF file so that each page goes into its own PDF file.

using System.IO;
using System.IO.Compression;
using GemBox.Pdf;
class Program
{
static void Main()
{
// If using Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
var fileNameWithoutExt = Path.GetFileNameWithoutExtension("%InputFileName%");
// Open source PDF file and create a destination ZIP file.
using (var source = PdfDocument.Load("%InputFileName%"))
using (var archiveStream = File.OpenWrite($"{fileNameWithoutExt}.zip"))
using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, leaveOpen: true))
for (int index = 0; index < source.Pages.Count; index++)
{
// Create new ZIP entry for each source document page.
var entry = archive.CreateEntry($"{fileNameWithoutExt}{index + 1}.pdf");
// Open ZIP entry stream.
using (var entryStream = entry.Open())
// Create destination document.
using (var destination = new PdfDocument())
{
// Clone source document page to destination document.
destination.Pages.AddClone(source.Pages[index]);
// Save destination document to ZIP entry stream.
destination.Save(entryStream);
}
}
}
}
Imports System.IO
Imports System.IO.Compression
Imports GemBox.Pdf
Module Program
Sub Main()
' If using Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Dim fileNameWithoutExt = Path.GetFileNameWithoutExtension("%InputFileName%")
' Open source PDF file and create a destination ZIP file.
Using source = PdfDocument.Load("%InputFileName%")
Using archiveStream = File.OpenWrite($"{fileNameWithoutExt}.zip")
Using archive = New ZipArchive(archiveStream, ZipArchiveMode.Create, leaveOpen:=True)
For index As Integer = 0 To source.Pages.Count - 1
' Create new ZIP entry for each source document page.
Dim entry = archive.CreateEntry($"{fileNameWithoutExt}{index + 1}.pdf")
' Open ZIP entry stream.
Using entryStream = entry.Open()
' Create destination document.
Using destination = New PdfDocument()
' Clone source document page to destination document.
destination.Pages.AddClone(source.Pages(index))
' Save destination document to ZIP entry stream.
destination.Save(entryStream)
End Using
End Using
Next
End Using
End Using
End Using
End Sub
End Module
In the above example, the uploaded PDF file is split so that each of its pages is cloned to a new PdfDocument
, which is then saved to a ZIP archive that gets saved to a file.
If you want to clone a PDF page to an existing PdfDocument
, then see this Cloning example.
See also
Next steps
Published: December 13, 2018 | Modified: September 8, 2021 | Author: Stipo Rubic