Splitting a PDF from .NET (C# Code Provided)

by Anil S

Tue, May 22 2012, 1:47 PM (US Eastern Time)

We get a decent amount of people who ask us about dynamically splitting a PDF.  Splitting a PDF document using DynamicPDF Merger for .Net is a very simple task. With the few lines of code the document can be split efficiently at the desired page. Use of PdfDocument object allows for efficient reuse of imported PDF document data. If the same PDF needs to be reused in multiple parts of the application, load the PDF into a PdfDocument object and use it where ever needed. Also the PdfDocument object is designed to be thread safe.
 
Let’s take a look at an example where we want to 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.  Notice that we do not need to know how many pages the original PDF contained by utilizing the PdfDocument’s Pages.Count method.

            //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 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");

We can also look at a case where we are going to take one PDF and “burst” 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");               
            }

The final case we will look at today is taking 50 pages from one PDF (pdfA.pdf) and appending them to the end of another PDF (pdfB.pdf).  First you will create a MergeDocument out of your pdfB.pdf (the one you are appending to) and then call the Append method and specifying pdfA.pdf and the start page of 1 and the total page of 50.  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 first 50 pages and call the Draw method to save the PDF. 
            finalPdf.Append(pdfToSplit, 1, pageNumToSplit);
            finalPdf.Draw("final.pdf");

Those are just three simple examples of doing different tasks around PDF splitting.  Obviously the possibilities of what you can do with them are endless.

Tags: , , , ,

Comments (3) -

Bijuraj
10/10/2012 6:34:36 AM #

I am having pdf file which is having protection mode like password or owner password. any kind of protection.
What I would like to know is if I am having secured pdf, I want to read the same pdf and append some text to that file and save it. How can I do this using Dynamic PDF library.
Can you please help on this, most urgent.
Thanks

Saroj Choudhury
2/14/2014 2:22:18 AM #

Do I need to include any references because it showing error for PdfDocument .

Anil S
2/14/2014 4:33:58 PM #

Hello Saroj,

Yes, you have to reference the DynamicPDF dll which can be downloaded here: www.dynamicpdf.com/.../Download.csp. The PdfDocument class is belongs to DynamicPDF.Merger namespace so you will need to include that in your code with a using or imports statement as shown below:

C#
using ceTe.DynamicPDF.Merger;

VB.NET
Imports ceTe.DynamicPDF.Merger

Add comment

biuquote
  • Comment
  • Preview
Loading

Month List

RecentPosts