Split PDF pages into multiple pages in C#

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

Splitting a PDF into two files

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");
}

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));
}

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");