AVAILABLE NOW: Spring 2025 Release
By Garry Klooesterman | 2025 Apr 24
5 min
Tags
PDF SDK
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.
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.
Follow these instructions to get started.
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:
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;
}
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;
}
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;
}
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.
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.
Tags
PDF SDK
Garry Klooesterman
Senior Technical Content Creator
Share this post
PRODUCTS
Platform Integrations
End User Applications
Popular Content