C++: Add or Delete Pages in PDF

When you are handling a PDF document with lots of pages, you will most likely need to add new pages to include more information or remove some redundant pages. In this article, you will learn how to programmatically add or delete pages in a PDF document 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

Add a Blank Page at the End of a PDF Document in C++

Spire.PDF for C++ allows you to add an empty page with specific size and margins at the end of the document using PdfDocument->GetPages()->Add(SizeF* size, PdfMargins* margins) method. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a sample PDF document using PdfDocument->LoadFromFile() method.
  • Create an empty page with the specified size and margins and then append it to the end of the document using PdfDocument->GetPages()->Add(SizeF* size, PdfMargins* margins) method.
  • Save the result document using PdfDocument->SaveToFile () method.
  • C++
#include "Spire.Pdf.o.h"

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

int main() {
	//Specify input and output file paths
	wstring inputFile = L"Data\\input.pdf";
	wstring outputFile = L"Output\\InsertEmptyPageAtEnd.pdf";

	//Create a PdfDocument object
	PdfDocument* pdf = new PdfDocument();

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

	//Add an empty page at the end of the document
	PdfMargins tempVar(0, 0);
	pdf->GetPages()->Add(new SizeF(PdfPageSize::A4()), &tempVar);

	//Save the result document
	pdf->SaveToFile(outputFile.c_str());
	pdf->Close();

	delete pdf;
}

C++: Add or Delete Pages in PDF

Insert a Blank Page After a Specific Page in PDF in C++

If you want to insert an empty page into a specific position of a PDF document, you can use the PdfDocument->GetPages()->Insert(int index) method. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a sample PDF document using PdfDocument->LoadFromFile() method.
  • Create a blank page and insert it after a specified PDF page using PdfDocument->GetPages()->Insert(int index) method.
  • Save the result document using PdfDocument->SaveToFile () method.
  • C++
#include "Spire.Pdf.o.h"

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

int main() {
	//Specify input and output file paths
	wstring inputFile = L"Data\\input.pdf";
	wstring outputFile = L"Output\\InsertEmptyPage.pdf";

	//Create a PdfDocument object
	PdfDocument* pdf = new PdfDocument();

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

	//Insert a blank page as the second page
	pdf->GetPages()->Insert(1);

	//Save the result document
	pdf->SaveToFile(outputFile.c_str());
	pdf->Close();

	delete pdf;
}

C++: Add or Delete Pages in PDF

Delete an Existing Page in a PDF Document in C++

For PDF pages containing useless information, you can also remove them using Spire.PDF for C++. The following are the steps to delete a specific page in PDF.

  • Create a PdfDocument object.
  • Load a sample PDF document using PdfDocument->LoadFromFile() method.
  • Remove a specified page from the document using PdfDocument->GetPages()->RemoveAt(int index) method.
  • Save the result document using PdfDocument->SaveToFile () method.
  • C++
#include "Spire.Pdf.o.h"

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

int main() {
	//Specify input and output file paths
	wstring inputFile = L"Data\\input.pdf";
	wstring outputFile = L"Output\\DeletePage.pdf";

	//Create a PdfDocument object
	PdfDocument* pdf = new PdfDocument();

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

	//Delete the first page in the document
	pdf->GetPages()->RemoveAt(0);

	//Save the result document
	pdf->SaveToFile(outputFile.c_str());
	pdf->Close();

	delete pdf;
}

C++: Add or Delete Pages in PDF

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.