Split PDF Documents using .NET PDF Library
The Syncfusion® .NET PDF library supports Splitting PDF file into single-page or multiple-page PDF documents.
Check the following video to quickly get started with split PDF document in .NET using the PDF Library.
Splitting a PDF file into individual pages
The Syncfusion® .NET PDF library allows splitting the pages of an existing PDF document into multiple individual PDF documents using Split method of the PdfLoadedDocument class.
Refer to the following code example to split a PDF into individual pages.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Set a output path
const string destinationFilePattern = "Output" + "{0}.pdf";
//Split the pages into separate documents
loadedDocument.Split(destinationFilePattern);
//Close the document
loadedDocument.Close(true);using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Set a output path
const string destinationFilePattern = "Output" + "{0}.pdf";
//Split the pages into separate documents
loadedDocument.Split(destinationFilePattern);
//Close the document
loadedDocument.Close(true);Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
'Load the PDF document
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Set a output path
Const destinationFilePattern As String = "Output" + "{0}.pdf"
'Split the pages into separate documents
loadedDocument.Split(destinationFilePattern)
'Close the document
loadedDocument.Close(True)You can download a complete working sample from GitHub.
Split a range of pages into a separate PDF document
The Syncfusion® .NET PDF library allows splitting a certain range of pages into a separate PDF document using the SplitByRanges method of the PdfLoadedDocument class.
Refer to the following code example to split a range of pages.
using Syncfusion.Pdf.Parsing;
//Create the values.
int[,] values = new int[,] { { 2, 5 }, { 8, 10 } };
//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Set a output path
const string destinationFilePattern = "Output" + "{0}.pdf";
//Split the pages into fixed number
loadedDocument.SplitByRanges(destinationFilePattern, values);
//close the document
loadedDocument.Close(true);using Syncfusion.Pdf.Parsing;
//Create the values.
int[,] values = new int[,] { { 2, 5 }, { 8, 10 } };
//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Set a output path
const string destinationFilePattern = "Output" + "{0}.pdf";
//Split the pages into fixed number
loadedDocument.SplitByRanges(destinationFilePattern, values);
//close the document
loadedDocument.Close(true);Imports Syncfusion.Pdf.Parsing
'Create the values.
Dim values As Integer(,) = New Integer(,) {{2, 5}, {8, 10}}
'Load the PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Set a output path
Const destinationFilePattern As String = "Output" + "{0}.pdf"
'Split the pages into fixed number.
loadedDocument.SplitByRanges(destinationFilePattern, values)
'Close the document.
loadedDocument.Close(True)Download a complete working sample from GitHub.
Split by a fixed number of pages into a PDF document
The Syncfusion® .NET PDF library allows splitting by fixed number of pages of an existing PDF document using the SplitByFixedNumber method of the PdfLoadedDocument class.
Refer to the following code example to split by a fixed number of pages.
using Syncfusion.Pdf.Parsing;
//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Set a output path
const string destinationFilePattern = "Output" + "{0}.pdf";
//Split the pages into fixed number
loadedDocument.SplitByFixedNumber(destinationFilePattern, 4);
//close the document
loadedDocument.Close(true);using Syncfusion.Pdf.Parsing;
//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Set a output path
const string destinationFilePattern = "Output" + "{0}.pdf";
//Split the pages into fixed number
loadedDocument.SplitByFixedNumber(destinationFilePattern, 4);
//close the document
loadedDocument.Close(true);Imports Syncfusion.Pdf.Parsing
'Load the PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Set a output path
Const destinationFilePattern As String = "Output" + "{0}.pdf"
'Split the pages into fixed number
loadedDocument.SplitByFixedNumber(destinationFilePattern, 4)
'Close the document.
loadedDocument.Close(True)Download a complete working sample from GitHub.
Split a PDF document based on PDF bookmarks
A PDF document may contain bookmarks that indicate different sections.The Syncfusion® .NET PDF library allows splitting a PDF document into sections using the PdfBookmark class.
Refer to the following code example to split a PDF using bookmarks.
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf;
// Load the PDF document
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf"))
{
PdfBookmarkBase bookmarks = loadedDocument.Bookmarks;
// Iterate all the bookmarks and their page ranges
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 document
document.Save("Output.pdf");
}
}
}
}
}using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf;
// Load the PDF document
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf"))
{
PdfBookmarkBase bookmarks = loadedDocument.Bookmarks;
// Iterate all the bookmarks and their page ranges
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 document
document.Save("Output.pdf");
}
}
}
}
}Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
Imports Syncfusion.Pdf.Interactive
Using loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
Dim bookmarks As PdfBookmarkBase = loadedDocument.Bookmarks
For Each bookmark As PdfBookmark In bookmarks
If bookmark.Destination IsNot Nothing Then
If bookmark.Destination.Page IsNot Nothing Then
Dim endIndex As Integer = bookmark.Destination.PageIndex
Using document As PdfDocument = New PdfDocument()
For Each childBookmark As PdfLoadedBookmark In bookmark
endIndex = childBookmark.Destination.PageIndex
Next
document.ImportPageRange(loadedDocument, bookmark.Destination.PageIndex, endIndex)
document.Save("Output.pdf")
End Using
End If
End If
Next
End UsingDownload a complete working sample from GitHub.
Remove Unused Resources when Splitting PDF Documents
The Syncfusion® PDF library enables the splitting of PDF documents and offers the capability to eliminate unused resources during the process. By enabling the RemoveUnusedResources property on the PdfSplitOptions class, any resources that are not in use will be deleted, thereby optimizing the final PDF document. The default value for this property is false.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
//Create the values.
int[,] values = new int[,] { { 2, 5 }, { 8, 10 } };
//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Set an output file pattern.
const string destinationFilePattern = "Output{0}.pdf";
//Create the split options object.
PdfSplitOptions splitOptions = new PdfSplitOptions();
//Enable the removal of unused resources property.
splitOptions.RemoveUnusedResources = true;
//Split the document by ranges.
loadedDocument.SplitByRanges(destinationFilePattern, values, splitOptions);
//Close the document.
loadedDocument.Close(true);using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
//Create the values.
int[,] values = new int[,] { { 2, 5 }, { 8, 10 } };
//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Set an output file pattern.
const string destinationFilePattern = "Output{0}.pdf";
//Create the split options object.
PdfSplitOptions splitOptions = new PdfSplitOptions();
//Enable the removal of unused resources property.
splitOptions.RemoveUnusedResources = true;
//Split the document by ranges.
loadedDocument.SplitByRanges(destinationFilePattern, values, splitOptions);
//Close the document.
loadedDocument.Close(true);Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
'Create the values.
Dim values As Integer(,) = New Integer(,) {{2, 5},{8, 10}}
'Load the PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Set an output path.
Const destinationFilePattern As String = "Output" + "{0}.pdf"
'Create the split options object.
Dim splitOptions As New PdfSplitOptions()
'Enable the removal of unused resources property.
splitOptions.RemoveUnusedResources = True
'Split the document by ranges.
loadedDocument.SplitByRanges(destinationFilePattern, values, splitOptions)
'Close the document.
loadedDocument.Close(True)Download a complete working sample from GitHub.
Import Tagged structure when Splitting PDF Documents
The Syncfusion® PDF library enables the splitting of PDF documents and offers the capability to import tagged structure during the process. By enabling the SplitTags property on the PdfSplitOptions class, thereby tagged structure will be imported into the final PDF document. The default value for this property is false.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
//Create the values.
int[,] values = new int[,] { { 0, 1 }, { 1, 2 } };
//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Set an output file pattern.
const string destinationFilePattern = "Output{0}.pdf";
//Create the split options object.
PdfSplitOptions splitOptions = new PdfSplitOptions();
//Enable the Split tags property.
splitOptions.SplitTags = true;
//Split the document by ranges.
loadedDocument.SplitByRanges(destinationFilePattern, values, splitOptions);
//Close the document.
loadedDocument.Close(true);using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
//Create the values.
int[,] values = new int[,] { { 0, 1 }, { 1, 2 } };
//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Set an output file pattern.
const string destinationFilePattern = "Output{0}.pdf";
//Create the split options object.
PdfSplitOptions splitOptions = new PdfSplitOptions();
//Enable the Split tags property.
splitOptions.SplitTags = true;
//Split the document by ranges.
loadedDocument.SplitByRanges(destinationFilePattern, values, splitOptions);
//Close the document.
loadedDocument.Close(true);Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
'Create the values.
Dim values As Integer(,) = New Integer(,) {{0, 1},{1, 2}}
'Load the PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Set an output path.
Const destinationFilePattern As String = "Output" + "{0}.pdf"
'Create the split options object.
Dim splitOptions As New PdfSplitOptions()
'Enable the Split tags property.
splitOptions.SplitTags = True
'Split the document by ranges.
loadedDocument.SplitByRanges(destinationFilePattern, values, splitOptions)
'Close the document.
loadedDocument.Close(True)Download a complete working sample from GitHub.