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 their .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.

NET 4.0 dll

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

DynamicPDF.Merger

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.

// Create a PdfDocument object to load the PDF.
PdfDocument pdfToSplit = new PdfDocument("pdfToSplit.pdf");

// Page number to split the document at.
int pageNumToSplit = 50;

// Add the first 50 pages and call the Draw method to save the PDF.
MergeDocument firstPdf = new MergeDocument(pdfToSplit, 1, pageNumToSplit);
firstPdf.Draw("pdfA.pdf");

// Add the remaining pages to a second document and save the 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.

// Create a PdfDocument object to load the PDF.
PdfDocument pdfToSplit = new PdfDocument("pdfToSplit.pdf");

// Loop through the page count and create an individual PDF for each page.
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.

// Create a PdfDocument object to load the.
PdfDocument pdfToSplit = new PdfDocument("pdfA.pdf");

// Page number to split the document at.
int pageNumToSplit = 50;

// Read the PDF into memory to which the split pages need to be appended.
MergeDocument finalPdf = new MergeDocument("pdfB.pdf");
// Add the first 50 pages and call the Draw method to save the 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.


Similar Articles