Split PDF Documents using .NET PDF Library

4 Dec 202419 minutes to read

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.

//Due to platform limitations, Essential® PDF supports splitting a PDF file into individual pages only in Windows Forms, WPF, ASP.NET, and ASP.NET MVC platforms. However this can be achieved by using the following code snippet. 

//Load the PDF document
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream, true);
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
    //Creates a new document.
    PdfDocument document = new PdfDocument();
    //Imports the pages from the loaded document.
    document.ImportPage(loadedDocument, i);

    //Create a File stream. 
    using (var outputFileStream = new FileStream("Output" + i + ".pdf", FileMode.Create, FileAccess.Write)) {
        //Save the document to stream.
        document.Save(outputFileStream);
    }
    //Close the document.
    document.Close(true);
}
//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);
'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.

//Load the existing PDF file.
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.
    FileStream outputStream = new FileStream(Guid.NewGuid().ToString() + ".pdf", FileMode.CreateNew);
    args.PdfDocumentData.CopyTo(outputStream);
    outputStream.Close();
}
//Spit the document by ranges.
loadDocument.SplitByRanges(new int[,] { { 0, 5 }, { 5, 10 } });

//Close the document.
loadDocument.Close(true);
//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);
'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.

//Load the existing PDF file.
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.
    FileStream outputStream = new FileStream(Guid.NewGuid().ToString() + ".pdf", FileMode.CreateNew);
    args.PdfDocumentData.CopyTo(outputStream);
    outputStream.Close();
}
//Spit the document by a fixed number.
loadDocument.SplitByFixedNumber(2);

//Close the document.
loadDocument.Close(true);
//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);
'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.

// Load the PDF document 
using (FileStream fileStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read)) 
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream)) 
{ 
    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 as stream
                    using (MemoryStream stream = new MemoryStream()) 
                    { 
                        document.Save(stream); 
                    } 
                } 
            } 
        } 
    } 
}
// 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 as stream
                    document.Save("Output.pdf");
                }
            }
        }
    }
}
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 Using

Download 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.

//Load the existing PDF file.
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.
    FileStream outputStream = new FileStream(Guid.NewGuid().ToString() + ".pdf", FileMode.CreateNew);
    args.PdfDocumentData.CopyTo(outputStream);
    outputStream.Close();
}
//Create the split options object.
PdfSplitOptions splitOptions = new PdfSplitOptions();
//Enable the removal of unused resources property.
splitOptions.RemoveUnusedResources = true;
//Split the document by ranges.
loadDocument.SplitByRanges(new int[,] { { 0, 5 }, { 5, 10 } }, splitOptions);
//Close the document.
loadDocument.Close(true);
//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);
'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.

//Load an existing PDF file.
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.
    FileStream outputStream = new FileStream(Guid.NewGuid().ToString() + ".pdf", FileMode.CreateNew);
    args.PdfDocumentData.CopyTo(outputStream);
    outputStream.Close();
}
//Create the split options object.
PdfSplitOptions splitOptions = new PdfSplitOptions();
//Enable the Split tags property.
splitOptions.SplitTags = true;
//Split the document by ranges.
loadDocument.SplitByRanges(new int[,] { { 0, 1 }, { 1, 2 } }, splitOptions);

//Close the document.
loadDocument.Close(true);
//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);
'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.