The Syncfusion .NET PDF library that allows users to split a PDF file into multiple sub-documents quickly and accurately. It also allows users to split, combine, import, and append PDFs.
The split PDF feature works seamlessly on various platforms: WinForms, WPF, Blazor, .NET MAUI, WinUI, Flutter, ASP.NET MVC, ASP.NET Core, UWP, Xamarin with Windows, Linux, and MacOS.
Here is an example of how to split PDF files in C# using the Syncfusion .NET PDF library. You can split a PDF file into a single page or multiple page PDF document with just a few lines of code.
//Create the FileStream object
using (FileStream inputFileStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
//Load the PDF document from stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFileStream))
{
//Iterate over the pages
for (int pageIndex = 0; pageIndex < loadedDocument.PageCount; pageIndex++)
{
//Create a new PdfDocument object
using (PdfDocument outputDocument = new PdfDocument())
{
//Import the page from the loadedDocument to the outputDocument
outputDocument.ImportPage(loadedDocument, pageIndex);
//Save the document into a filestream object
using (FileStream outputFileStream = new FileStream("Output" + pageIndex + ".pdf", FileMode.Create, FileAccess.Write))
{
outputDocument.Save(outputFileStream);
}
}
}
}
A range of pages can be split into a separate PDF document.
//Load the existing PDF file.
using (PdfLoadedDocument loadDocument = new PdfLoadedDocument(new FileStream("Input.pdf", FileMode.Open)))
{
//Subscribe to the document split event.
loadDocument.DocumentSplitEvent += LoadDocument_DocumentSplitEvent;
void LoadDocument_DocumentSplitEvent(object sender, PdfDocumentSplitEventArgs args)
{
//Save the resulting document.
using (FileStream outputStream = new FileStream(Guid.NewGuid().ToString() + ".pdf", FileMode.CreateNew))
{
args.PdfDocumentData.CopyTo(outputStream);
}
}
//Spit the document by ranges.
loadDocument.SplitByRanges(new int[,] { { 0, 5 }, { 5, 10 } });
}
Users can get specific pages or delete certain pages from a PDF document.
//Create the FileStream object
using (FileStream inputFilestream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
//Load the PDF document from stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFilestream))
{
//Creates a new PDF document
using (PdfDocument outputDocument = new PdfDocument())
{
//Remove the first page in the PDF document
loadedDocument.Pages.RemoveAt(0);
//Import certain pages to the new PDF document
outputDocument.ImportPage(loadedDocument, 0);
//Save the document into a filestream object
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.Write))
{
outputDocument.Save(outputFileStream);
}
}
}
Split a PDF document based on bookmarks.
//Create the FileStream object
using (FileStream fileStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
//Load the PDF document from stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream))
{
//Create bookmark object
PdfBookmarkBase bookmarks = loadedDocument.Bookmarks;
//Load the PDF document from stream
foreach (PdfBookmark bookmark in bookmarks)
{
if (bookmark.Destination != null)
{
if (bookmark.Destination.Page != null)
{
int endIndex = bookmark.Destination.PageIndex;
//Create a new PDF document
using (PdfDocument document = new PdfDocument())
{
foreach (PdfLoadedBookmark childBookmark in bookmark)
{
endIndex = childBookmark.Destination.PageIndex;
}
//Import the pages to the new PDF document
document.ImportPageRange(loadedDocument, bookmark.Destination.PageIndex, endIndex);
//Save the PDF file with the bookamark title's name.
using (var outputFileStream = new FileStream(bookmark.Title + ".pdf", FileMode.Create, FileAccess.Write))
{
document.Save(outputFileStream);
}
}
}
}
}
}
Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion is proud to hold the following industry awards.