Split PDF Pages into Multiple Pages in C#

This guide outlines three basic examples of splitting a PDF file:

Splitting a PDF into Two

To split a PDF file into two files, follow the steps below.

  1. Create three GdPicturePDF objects.

  2. Create three empty output PDF files with the NewPDF method.

  3. Copy the PDF pages with the ClonePage method to the newly created PDF files.

  4. Save the PDF files with the SaveToFile method.

The code below shows how to split a PDF file into two files at a specified page:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
using GdPicturePDF gdpicturePDF1 = new GdPicturePDF();
using GdPicturePDF gdpicturePDF2 = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
gdpicturePDF1.NewPDF();
gdpicturePDF2.NewPDF();
// Specify the first page of the second PDF file.
int splitPage = 3;
for (int i = 1; i <= gdpicturePDF.GetPageCount(); i++)
{
    if (i < splitPage) gdpicturePDF1.ClonePage(gdpicturePDF, i);
    else gdpicturePDF2.ClonePage(gdpicturePDF, i);
    gdpicturePDF1.SaveToFile(@"C:\temp\output1.pdf");
    gdpicturePDF2.SaveToFile(@"C:\temp\output2.pdf");
}
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
Using gdpicturePDF1 As GdPicturePDF = New GdPicturePDF()
Using gdpicturePDF2 As GdPicturePDF = New GdPicturePDF()
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    gdpicturePDF1.NewPDF()
    gdpicturePDF2.NewPDF()
    ' Specify the first page of the second PDF file.
    Dim splitPage = 3
    For i As Integer = 1 To gdpicturePDF.GetPageCount()
        If i < splitPage Then
            gdpicturePDF1.ClonePage(gdpicturePDF, i)
        Else
            gdpicturePDF2.ClonePage(gdpicturePDF, i)
        End If
        gdpicturePDF1.SaveToFile("C:\temp\output1.pdf")
        gdpicturePDF2.SaveToFile("C:\temp\output2.pdf")
    Next
End Using
End Using
End Using
Used Methods

Related Topics

Splitting All PDF Pages

To split all PDF pages into separate files, follow the steps below.

  1. Create two GdPicturePDF objects.

  2. Create an empty output PDF file with the NewPDF method.

  3. For each page:

    • Create an empty PDF.

    • Copy the current PDF page with the ClonePage method to the newly created PDF file.

    • Save the PDF file with the SaveToFile method.

The code below shows how to split all PDF pages into separate files:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
using GdPicturePDF gdpictureSinglePDF = new GdPicturePDF();
    gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
    for (int i = 1; i <=gdpicturePDF.GetPageCount(); i++)
    {
        gdpictureSinglePDF.NewPDF();
        gdpictureSinglePDF.ClonePage(gdpicturePDF, i);
        string page = i.ToString();
        gdpictureSinglePDF.SaveToFile(string.Format(@"C:\temp\output{0}.pdf", page));
    }
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
Using gdpictureSinglePDF As GdPicturePDF = New GdPicturePDF()
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    For i As Integer = 1 To gdpicturePDF.GetPageCount()
        gdpictureSinglePDF.NewPDF()
        gdpictureSinglePDF.ClonePage(gdpicturePDF, i)
        Dim page As String = i.ToString()
        gdpictureSinglePDF.SaveToFile(String.Format("C:\temp\output{0}.pdf", page))
    Next
End Using
End Using
Used Methods

Related Topics

Splitting Odd and Even Pages in a PDF

To split odd and even pages of a PDF file into two PDF files, follow the steps below.

  1. Create three GdPicturePDF objects.

  2. Create two empty output PDF files with the NewPDF method — the first for odd pages, and the second for even pages.

  3. For each page:

    • Check if current PDF page is odd or even.

    • Copy the current PDF page to one of the new PDF files with the ClonePage method.

  4. Save the PDF files with the SaveToFile method.

The code below shows how to split odd and even pages of a PDF file into two PDF files:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
using GdPicturePDF gdpictureOddPDF = new GdPicturePDF();
using GdPicturePDF gdpictureEvenPDF = new GdPicturePDF();
    gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
    gdpictureOddPDF.NewPDF();
    gdpictureEvenPDF.NewPDF();
    for (int i = 1; i <= gdpicturePDF.GetPageCount(); i++)
    {
        if (i % 2 != 0) gdpictureOddPDF.ClonePage(gdpicturePDF, i);
        else gdpictureEvenPDF.ClonePage(gdpicturePDF, i);
    }
    gdpictureOddPDF.SaveToFile(@"C:\temp\outputOdd.pdf");
    gdpictureEvenPDF.SaveToFile(@"C:\temp\outputEven.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
Using gdpictureOddPDF As GdPicturePDF = New GdPicturePDF()
Using gdpictureEvenPDF As GdPicturePDF = New GdPicturePDF()
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    gdpictureOddPDF.NewPDF()
    gdpictureEvenPDF.NewPDF()
    For i As Integer = 1 To gdpicturePDF.GetPageCount()
        If i Mod 2 <> 0 Then
            gdpictureOddPDF.ClonePage(gdpicturePDF, i)
        Else
            gdpictureEvenPDF.ClonePage(gdpicturePDF, i)
        End If
    Next
    gdpictureOddPDF.SaveToFile("C:\temp\outputOdd.pdf")
    gdpictureEvenPDF.SaveToFile("C:\temp\outputEven.pdf")
End Using
End Using
End Using
Used Methods

Related Topics