C++: Split a PDF File into Multiple PDFs

When dealing with large PDF files, splitting them into multiple separate files is a useful operation to streamline your work. By doing this, you can get the specific parts you need, or get smaller PDF files that are easy to upload to a website, send via email, etc. In this article, you will learn how to split a PDF into multiple files in C++ using Spire.PDF for C++.

Install Spire.PDF for C++

There are two ways to integrate Spire.PDF for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.PDF for C++ in a C++ Application

Split a PDF File into Multiple Single-Page PDFs in C++

Spire.PDF for C++ offers the PdfDocument->Split() method to divide a multipage PDF document into multiple single-page files. The following are the detailed steps.

  • Create a PdfDcoument instance.
  • Load a sample PDF document using PdfDocument->LoadFromFile() method.
  • Split the document into one-page PDFs using PdfDocument->Split() method.
  • C++
#include "Spire.Pdf.o.h";

using namespace Spire::Pdf;
using namespace Spire::Common;


int main()
{
	//Specify the input and output files
	std::wstring inputFile = L"Data\\template.pdf";
	std::wstring outputFile = L"SplitDocument/";
	std::wstring pattern = outputFile + L"SplitDocument-{0}.pdf";

	//Create a PdfDocument instance
	intrusive_ptr<PdfDocument> pdf = new PdfDocument();

	//Load a sample PDF file
	pdf->LoadFromFile(inputFile.c_str());

	//Split the PDF to one-page PDFs
	pdf->Split(pattern.c_str());
	pdf->Close();

}

C++: Split a PDF File into Multiple PDFs

Split a PDF File by Page Ranges in C++

There's no straightforward way to split PDF documents by page ranges. To do so, you can create two or more new PDF documents and then use the PdfPageBase->CreateTemplate()->Draw() method to draw the contents of the specified pages in the input PDF file onto the pages of the new PDFs. The following are the detailed steps.

  • Create a PdfDocument instance and load a sample PDF file.
  • Create a new PDF document, and then Initialize a new instance of PdfPageBase class.
  • Iterate through the first several pages in the sample PDF file.
  • Create a new page with specified size and margins in the new PDF document
  • Get the specified page in the sample PDF using PdfDocument->GetPages()->GetItem() method, and then draw the contents of the specified page onto the new page using PdfPageBase->CreateTemplate()->Draw() method.
  • Save the first new PDF document using PdfDocument->SaveToFile() method.
  • Create another new PDF document and then draw the remaining pages of the sample PDF file into it.
  • Save the second new PDF document.
  • C++
#include "Spire.Pdf.o.h";

using namespace Spire::Pdf;
using namespace Spire::Common;

int main()
{
	//Create a PdfDocument instance and load a sample PDF file
	intrusive_ptr<PdfDocument> oldPdf = new PdfDocument();
	oldPdf->LoadFromFile(L"Data\\template.pdf");

	//Create a new PDF document
	intrusive_ptr<PdfDocument> newPdf1 = new PdfDocument();

	//Initialize a new instance of PdfPageBase class
	intrusive_ptr<PdfPageBase> page;

	//Draw the first three pages of the sample file into the new PDF document
	for (int i = 0; i < 3; i++)
	{
		//Create a new page with specified size and margin in the new PDF document
		intrusive_ptr<PdfMargins> tempVar = new PdfMargins(0);
		page = newPdf1->GetPages()->Add(oldPdf->GetPages()->GetItem(i)->GetSize(), tempVar);

		//Draw the contents of a specified page in the sample file onto the new page
		oldPdf->GetPages()->GetItem(i)->CreateTemplate()->Draw(page, new PointF(0, 0));
	}

	//Save the first PDF document
	newPdf1->SaveToFile(L"SplitByRange1.pdf");
	newPdf1->Close();

	//Create another new PDF document
	intrusive_ptr<PdfDocument> newPdf2 = new PdfDocument();

	//Draw the rest pages of the sample file into the new PDF document
	for (int i = 3; i < oldPdf->GetPages()->GetCount(); i++)
	{
		//Create a new page with specified size and margin in the new PDF document
		intrusive_ptr<PdfMargins> tempVar = new PdfMargins(0);
		page = newPdf2->GetPages()->Add(oldPdf->GetPages()->GetItem(i)->GetSize(), tempVar);

		// Draw the contents of a specified page in the sample file onto the new page
		oldPdf->GetPages()->GetItem(i)->CreateTemplate()->Draw(page, new PointF(0, 0));
	}

	//Save the second PDF document
	newPdf2->SaveToFile(L"SplitByRange2.pdf");
	newPdf2->Close();
}

C++: Split a PDF File into Multiple PDFs

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.