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.
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.
-
- PdfDocument pdfToSplit = new PdfDocument("pdfToSplit.pdf");
-
-
- int pageNumToSplit = 50;
-
-
- MergeDocument firstPdf = new MergeDocument(pdfToSplit, 1, pageNumToSplit);
- firstPdf.Draw("pdfA.pdf");
-
-
- MergeDocument secondPDF = new MergeDocument(pdfToSplit, pageNumToSplit + 1, pdfToSplit.Pages.Count - pageNumToSplit);
- 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.
-
- PdfDocument pdfToSplit = new PdfDocument("pdfToSplit.pdf");
-
-
- for (int i = 1; i <= pdfToSplit.Pages.Count; i++)
- {
- MergeDocument document = new MergeDocument(pdfToSplit, i, 1);
- document.Draw("pdf_" + i.ToString() + ".pdf");
- }
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.
-
- PdfDocument pdfToSplit = new PdfDocument("pdfA.pdf");
-
-
- int pageNumToSplit = 50;
-
-
- MergeDocument finalPdf = new MergeDocument("pdfB.pdf");
-
- finalPdf.Append(pdfToSplit, 1, pageNumToSplit);
- 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.