Splitting a PDF in .NET

One of the common functionalities needed in applications that handle PDF documents is to split a PDF. The product I have used to achieve this task is called DynamicPDF Merger for .NET

DynamicPDF Merger is a .NET API that allows developers to manipulate PDFs in there .NET applications. An evaluation edition of this product can be downloaded here. The evaluation is fully functional and there is no time limit and the output PDFs are watermarked with the company logo during evaluation.

Let’s get started. Download and install the product on your development machine. In your project add a reference to the DynamicPDF Merger dll. The product dlls are located in the bin folder within the folder where you have installed the product. Separate dlls are provided for each .NET Framework version; you just need to pick one dll based on your project’s .Net Framework setting.

Assembly Name .NET Framework Version
ceTe.DynamicPDF.20.dll 2.0 and 3.0
ceTe.DynamicPDF.35.dll 3.5
ceTe.DynamicPDF.40.dll 4.x

In the screenshot shown below, I have referenced the .NET 4.0 dll.

solution explorer
Next, we will add a using statement to import the ceTe.DynamicPDF.Merger namespace to our code.

Let’s take a look at some sample code. Here is an example where we take a PDF file (pdfToSplit.pdf) and split it into two files (pdfA.pdf and pdfB.pdf).

The first PDF split (pdfA.pdf) will contain the first 50 pages of the file while the second PDF (pdfB.pdf) will contain all remaining pages.

  1. //Create a PdfDocument object to load the PDF.   
  2. PdfDocument pdfToSplit = new PdfDocument("pdfToSplit.pdf");  
  4. //Page number to split the document at.   
  5. int pageNumToSplit = 50;  
  7. //Add first 50 pages and call the Draw method to save the PDF.   
  8. MergeDocument firstPdf = new MergeDocument(pdfToSplit, 1, pageNumToSplit);  
  9. firstPdf.Draw("pdfA.pdf");  
  11. //Add the remaining pages to a second document and save the PDF.   
  12. MergeDocument secondPDF = new MergeDocument(pdfToSplit, pageNumToSplit + 1, pdfToSplit.Pages.Count - pageNumToSplit);  
  13. secondPDF.Draw("pdfB.pdf");  
Here is a case where we take one PDF and split it into a bunch of smaller PDF files. In this example we will take the original PDF and create a new PDF file for each page of the original PDF. The original PDF document is not modified.
  1. //Create a PdfDocument object to load the PDF.   
  2. PdfDocument pdfToSplit = new PdfDocument("pdfToSplit.pdf");  
  4. //Loop through the page count and create an individual PDF for each page.   
  5. for (int i = 1; i <= pdfToSplit.Pages.Count; i++)   
  6. {  
  7.     MergeDocument document = new MergeDocument(pdfToSplit, i, 1);  
  8.     document.Draw("pdf_" + i.ToString() + ".pdf");  
  9. }  
Finally we will take 50 pages from one PDF (pdfA.pdf) and append them to the end of another PDF (pdfB.pdf). First we will create a MergeDocument out of the pdfB.pdf (the one we are appending to) and then call the Append method and specify pdfA.pdf, start page of 1 and the number of pages to split and append. It’s that simple.
  1. //Create a PdfDocument object to load the.   
  2. PdfDocument pdfToSplit = new PdfDocument("pdfA.pdf");  
  4. //Page number to split the document at.   
  5. int pageNumToSplit = 50;  
  7. //Read the PDF into memory to which the split pages need to be appended.   
  8. MergeDocument finalPdf = new MergeDocument("pdfB.pdf");  
  9. //Add first 50 pages and call the Draw method to save the PDF.   
  10. finalPdf.Append(pdfToSplit, 1, pageNumToSplit);  
  11. finalPdf.Draw("final.pdf");  
These are just three simple examples of doing different tasks around PDF splitting. Obviously the possibilities of what you can do with them are endless.