Split PDF pages in C# and VB.NET

The following code sample shows how to split PDF pages in C# and VB.NET.

Splitting PDF pages is quite similar to append PDF pages. The only difference is that appending copy pages from PDF documents onto another existing PDF document, while splitting copy pages onto a new PDF document. The following sample code shows how to split a PDF document with multiple pages into several PDF documents each containing a single page.

How to split pdf in C# / VB.NET

using ( FileStream inFile = new FileStream( @"..\..\..\inputDocuments\PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read ) )
{
    // open the source document
    Document documentIn = new Document( inFile );

    // enumerate the pages in the source document
    for ( int i=0; i<documentIn.Pages.Count; i++ )
    {
        // create the target document
        Document documentOut = new Document();

        // append page i to the target document, we don't want to change the documentIn, so Clone the page.
        documentOut.Pages.Add( documentIn.Pages[i].Clone() );

        // write the target document to disk
        using ( FileStream outFile = new FileStream( 
                    string.Format( @"..\..\split_{0}.pdf", i ), 
                    FileMode.Create, FileAccess.Write ) )
        {
            documentOut.Write( outFile );
        }
    }
}
Using inFile As New FileStream("..\..\..\inputDocuments\PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read)
    ' open the source document
    Dim documentIn As New Document(inFile)
    For i As Integer = 0 To documentIn.Pages.Count - 1

        ' enumerate the pages in the source document
        ' create the target document
        Dim documentOut As New Document()

        ' append page i to the target document, we don't want to change the documentIn, so Clone the page.
        documentOut.Pages.Add(documentIn.Pages(i).Clone())

        ' write the target document to disk
        Using outFile As New FileStream(String.Format("..\..\split_{0}.pdf", i), FileMode.Create, FileAccess.Write)
            documentOut.Write(outFile)
        End Using
    Next
End Using