AVAILABLE NOW: Spring 2025 Release

How to Split, Merge, and Reorder a PDF Using C#

By Garry Klooesterman | 2025 Apr 10

Sanity Image
Read time

4 min

Summary: PDFs are a popular format for storing and sharing information because they are versatile, convenient, and secure, to name a few reasons. However, manipulating PDFs can be challenging, especially for tasks such as adding or cropping pages. This blog discusses using a PDF SDK such as the Apryse SDK to automate these and other PDF manipulation tasks.

Introduction

Copied to clipboard

It’s estimated that there are more than 2.5 trillion PDFs in the world and given the current world population, the PDF to human ratio is roughly 300:1, making them one of the most popular formats for storing and sharing information. Add the fact that PDFs are versatile, convenient, compact, secure, and more, it’s easy to understand why they are so popular.

However, working with PDFs, especially large ones, poses a few key challenges when trying to handle PDF manipulation tasks such as adding or cropping pages. PDF editors such as xodo.com, Xodo PDF Studio, or an app based one like Apryse WebViewer can be used to handle these and many other tasks, but the process would still be partially manual.

To solve for this manual element, we can use a PDF Manipulation library like the one included with the Apryse SDK to automate these tasks. We’ll use C# for these examples, but the SDK is also available for other languages and frameworks including C++, Java, Python and Node.js.

How to Get Started

Copied to clipboard

Follow these instructions to get started.

  1. Download the Apryse Server SDK. In this case, we’ll want to choose the C# .NET PDF library.
  2. Follow these steps to extract the folder from the .zip file and set up .NET.
  3. Get a free trial key.
  4. See our documentation for the full code you’ll need to handle the following example tasks. I’ve included a sample of the specific code for each task based on PDFPageTestCS.

Now that we’ve got everything set up, let’s look at what we can do. 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
  • Reorder pages in a PDF

Split a PDF into Multiple Pages

Copied to clipboard

Imagine this scenario. You work with a lot of reports and most of the time, they’re manageable. But you’ve just received the latest report for your team and for some reason, there’s a bunch of extra pages that have been added to the end by mistake, making the document twice as long as it should be. This calls for splitting the PDF into multiple pages so you have just what you need.

We’ll use the following code.

			// Sample 1 - Split a PDF document into multiple pages
			try
			{
				Console.WriteLine("_______________________________________________");
				Console.WriteLine("Sample 1 - Split a PDF document into multiple pages...");
				Console.WriteLine("Opening the input pdf...");

				using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf"))
				{
					in_doc.InitSecurityHandler();

					int page_num = in_doc.GetPageCount();
					for (int i = 1; i <= page_num; ++i) 
					{
						using (PDFDoc new_doc = new PDFDoc()) 
						{
							new_doc.InsertPages(0, in_doc, i, i, PDFDoc.InsertFlag.e_none);
							new_doc.Save(output_path + "newsletter_split_page_" + i + ".pdf", SDFDoc.SaveOptions.e_remove_unused);
							Console.WriteLine("Done. Result saved in newsletter_split_page_" + i + ".pdf");
						}
					}
				}
			}
			catch (Exception e)
			{
				Console.WriteLine("Exception caught:\n{0}", e);
			}

Merge Several PDFs into One

Copied to clipboard

Now that we’ve split the report into separate pages, we can sort them and discard the ones we don’t need. Using the following code, we can merge the remaining pages into one clean report.

			// Sample 2 - Merge several PDF documents into one
			try
			{
				Console.WriteLine("_______________________________________________");
				Console.WriteLine("Sample 2 - Merge several PDF documents into one...");

				using (PDFDoc new_doc = new PDFDoc()) 
				{
					new_doc.InitSecurityHandler();
					int page_num = 15;
					for (int i = 1; i <= page_num; ++i) 
					{
						Console.WriteLine("Opening newsletter_split_page_" + i + ".pdf");
						using (PDFDoc in_doc = new PDFDoc(output_path + "newsletter_split_page_" + i + ".pdf")) 
						{
							new_doc.InsertPages(i, in_doc, 1, in_doc.GetPageCount(), PDFDoc.InsertFlag.e_none);
						}
					}
					new_doc.Save(output_path + "newsletter_merge_pages.pdf", SDFDoc.SaveOptions.e_remove_unused);
				}
				Console.WriteLine("Done. Result saved in newsletter_merge_pages.pdf");
			}
			catch (Exception e)
			{
				Console.WriteLine("Exception caught:\n{0}", e);
			}

Insert a Page from One PDF to Another PDF

Copied to clipboard

You’ve got your merged report and you’re just about to send it off for review when your manager asks you to add a bio page for each employee. Luckily, you’ve already got all the bio pages in another PDF and you can simply insert those pages throughout the new report.

You’ll need the following code to do this.

			// Sample 4 - Inserts a page from one document at different
			// locations within another document                       
			try   
			{	 
				Console.WriteLine("_______________________________________________");
				Console.WriteLine("Sample 4 - Insert a page at different locations...");
				Console.WriteLine("Opening the input pdf...");

				using (PDFDoc in1_doc = new PDFDoc(input_path +  "newsletter.pdf"))
				using (PDFDoc in2_doc = new PDFDoc(input_path + "fish.pdf"))
				{
					in1_doc.InitSecurityHandler();
					in2_doc.InitSecurityHandler();

					Page src_page = in2_doc.GetPage(1);
					PageIterator dst_page = in1_doc.GetPageIterator(1);
					int page_num = 1;
					while (dst_page.HasNext()) {
						if (page_num++ % 3 == 0) {
							in1_doc.PageInsert(dst_page, src_page);
						}
						dst_page.Next();
					}

					in1_doc.Save(output_path +  "newsletter_page_insert.pdf", 0);
					Console.WriteLine("Done. Result saved in newsletter_page_insert.pdf...");
				}
			}
			catch(Exception e)
			{
				Console.WriteLine("Exception caught:\n{0}", e);
			}

Reorder the Pages in a PDF

Copied to clipboard

Sticking with that report example, you’ve just received the latest report and you’re ready to review it before sending it to your manager. The problem is, whoever scanned it, did it backwards so you’ll need to reorder those pages first. This example uses the copy and delete page operations to re-arrange and sort the pages, with the end result being a new file with the pages reversed.

			// Sample - Reorder the pages of a PDF by reversing the order
			try
			{
				Console.WriteLine("_______________________________________________");
				Console.WriteLine("Sample - Reorder the pages of a PDF  by reversing the order...");
				Console.WriteLine("Opening the input pdf...");

				using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf"))
				{
					in_doc.InitSecurityHandler();
					
					int page_num = in_doc.GetPageCount();
					for (int i = 1; i <= page_num; ++i)
					{		
						PageIterator itr = in_doc.GetPageIterator(i);
						Page page = itr.Current();
						in_doc.PageRemove(itr);
						in_doc.PagePushFront(page);
					}
					in_doc.Save(output_path +  "newsletter_reorder_pages.pdf", 0);
					Console.WriteLine("Done. Result saved in newsletter_reorder_pages.pdf...");
				}
			}
			catch (Exception e)
			{
				Console.WriteLine("Exception caught:\n{0}", e);
			}

Other Examples

Copied to clipboard

Of course, there’s a lot more you can do with this SDK. Check out the documentation for more examples such as removing a page and cropping a page.

Conclusion

Copied to clipboard

When you need to manipulate a PDF, including splitting it into multiple pages or adding a page, the Apryse PDF SDK handles the task easily and efficiently. These examples show the basics of what can be done, and the code can be used as a starting point to create more robust operations able to handle 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