AVAILABLE NOW: Spring 2025 Release

Split, Merge, and Insert Pages in a PDF Using C++

By Garry Klooesterman | 2025 Apr 24

Sanity Image
Read time

5 min

Summary: PDFs are a great way to share information quickly and easily as they can be opened on practically any device and display the same as the original regardless of the hardware or software used to view them. Manipulating PDFs, on the other hand, can be challenging, especially for more complex tasks like merging PDFs or inserting pages. In this blog, we’ll look at automating these and other PDF manipulation tasks using a PDF SDK such as the Apryse SDK.

Introduction

Copied to clipboard

PDFs are a common and convenient way to share information. They’re easy to use, versatile, secure, compact, and the list goes on. Manipulating PDFs, on the other hand, isn’t always so convenient. For example, large PDFs can be taxing on system resources and difficult to handle, let alone trying to merge PDFs or insert pages.

You could use a PDF editor such as xodo.com or Xodo PDF Studio to handle these and other tasks. App based editors like Apryse WebViewer can also be used, but there would still be some manual element to the process.

If you’re looking for an automated solution, you can use a PDF Manipulation library like the one included with the Apryse SDK. In this blog, we’ll use C++ for the examples, but the SDK is available for other popular languages and frameworks as well including Go, Java, Python and Node.js.

How to Get Started

Copied to clipboard

Follow these instructions to get started.

  1. First, you’ll need to download the Apryse Server SDK. In this case, choose the C++ PDF library.
  2. Next, follow these steps to extract the folder from the .zip file.
  3. Now you’ll want to get a free trial key, if you don’t already have one.
  4. The last step for getting started is to see our documentation for the full code you’ll need to support the following example tasks. I’ve included a sample of the specific code for each task based on our demo.

Now that you’re ready to go, let’s look at some of the ways we can manipulate PDFs using the Apryse SDK. In this blog, we’ll look at how to:

  • Split a PDF into multiple pages
  • Merge several PDFs into one
  • Insert a page from one PDF to another PDF

Split a PDF into Multiple Pages

Copied to clipboard

Let’s look at an example of when you might want to split a PDF into separate pages. You’re working on this month’s stats reports for your team and due to a spacing issue, the system generated a report with a blank page after every employee section. You now have a report with extra blank pages that need to be removed. You can split the PDF report into separate pages using the following code.

	// Sample 1 - Split a PDF document into multiple pages
	try
	{
		cout << "_______________________________________________" << endl;
		cout << "Sample 1 - Split a PDF document into multiple pages..." << endl;
		cout << "Opening the input pdf..." << endl;
		PDFDoc in_doc((input_path +  "newsletter.pdf").c_str());
		in_doc.InitSecurityHandler();
		int page_num = in_doc.GetPageCount();
		for (int i=1; i<=page_num; ++i)
		{
			PDFDoc new_doc;
			char fname[256];
			sprintf(fname, "newsletter_split_page_%d.pdf", i);
			string output_file(output_path + fname);
			new_doc.InsertPages(0, in_doc, i, i, PDFDoc::e_none);
			new_doc.Save(output_file, SDFDoc::e_remove_unused, 0);
			cout << "Done. Result saved in " << fname << endl;
		}
	}
	catch(Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch(...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

Merge Several PDFs into One

Copied to clipboard

You now have the report split into separate pages and you’ve weeded out the blanks. After sorting the pages of employee stats, you’re ready to create the new report. You can use the following code to merge the pages into one report.

	// Sample 2 - Merge several PDF documents into one
	try
	{
		cout << "_______________________________________________" << endl;
		cout << "Sample 2 - Merge several PDF documents into one..." << endl;
		PDFDoc new_doc;
		new_doc.InitSecurityHandler();
		int page_num = 15;
		for (int i=1; i<=page_num; ++i)
		{
			char fname[256];
			sprintf(fname, "newsletter_split_page_%d.pdf", i);
			string input_file(output_path + fname);
			cout << "Opening " << fname << endl;
			PDFDoc in_doc(input_file);
			new_doc.InsertPages(i, in_doc, 1, in_doc.GetPageCount(), PDFDoc::e_none);
		}
		new_doc.Save(output_path + "newsletter_merge_pages.pdf", SDFDoc::e_remove_unused, 0);
		cout << "Done. Result saved in newsletter_merge_pages.pdf" << endl;
	}
	catch(Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch(...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

Insert a Page from One PDF to Another PDF

Copied to clipboard

You’ve got your report ready to go when you remember the latest memo about stat reports needing a bio page added for each employee. Since you already have the bio pages in another PDF, you can use the following code to insert those pages throughout the new report.

	// Sample 4 - Inserts a page from one document at different 
	// locations within another document
	try
	{	 
		cout << "_______________________________________________" << endl;
		cout << "Sample 4 - Insert a page at different locations..." << endl;
		cout << "Opening the input pdf..." << endl;
		
		PDFDoc in1_doc((input_path +  "newsletter.pdf").c_str());
		in1_doc.InitSecurityHandler();
		PDFDoc in2_doc((input_path + "fish.pdf").c_str());
		in2_doc.InitSecurityHandler(); 
		
		PageIterator src_page = in2_doc.GetPageIterator();
		PageIterator dst_page = in1_doc.GetPageIterator();
		int page_num = 1;
		while (dst_page.HasNext()) {
			if (page_num++ % 3 == 0) {
				in1_doc.PageInsert(dst_page, src_page.Current());
			}
			dst_page.Next();
		}
		in1_doc.Save((output_path +  "newsletter_page_insert.pdf").c_str(), 0 , NULL);
		cout << "Done. Result saved in newsletter_page_insert.pdf..." << endl;
	}
	catch(Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch(...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

Other Examples

Copied to clipboard

If you’re looking for more examples of what you can do with the code in this SDK, check out the documentation. You’ll find sample code for removing a page, cropping a page, and more.

Conclusion

Copied to clipboard

As we’ve seen already, there’s many ways to manipulate a PDF using a PDF Manipulation Library like the Apryse PDF SDK. These examples show the basics of what can be done and how easy it is to get started. You can even use the code for inspiration and a base for creating more robust functions for complex PDF manipulation tasks.

Try it out for yourself with our free trial.

Get started now or contact our sales team for any questions. You can also check out our Discord community for support and discussions.

Sanity Image

Garry Klooesterman

Senior Technical Content Creator

Share this post

email
linkedIn
twitter