Splitting PDF File In C# Using iTextSharp

We are going to use iTextSharp library in this article. It is an open source library and very useful to CREATE, ADAPT, INSPECT and MAINTAIN documents in the Portable Document Format (PDF).

Please refer to the link given below for PDF, using iTextSharp library.

Sometimes we need to split the pages from one PDF file into multiple PDF files.

Here, in this article, we are going to take a sample example for splitting a PDF file. Sample example is in console applications but in real time, we can use ASP.NET, Web API etc., as per our requirement.

We have to follow some simple steps to split the pages from one PDF file and save into multiple PDF files.

Step 1

We have to install iTextSharp through manage NuGet packages, as shown below.


We can install, using Package Manager Console with the the command given below.

Install-Package iTextSharp

Step 2

Now, add three namespaces in top of .cs page, which are given below. 

  1. using iTextSharp.text;  
  2. using iTextSharp.text.pdf;  
  3. using System.IO;   

Step 3

Write the code in the Program class to extract the pages from one PDF and save into multiple PDF files. 

  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             string pdfFilePath = @"C:\PdfFiles\sample.pdf";  
  6.             string outputPath = @"C:\SplitedPdfFiles";  
  7.             int interval = 10;  
  8.             int pageNameSuffix = 0;  
  9.   
  10.  // Intialize a new PdfReader instance with the contents of the source Pdf file:  
  11.             PdfReader reader = new PdfReader(pdfFilePath);              
  12.   
  13.             FileInfo file = new FileInfo(pdfFilePath);  
  14.             string pdfFileName = file.Name.Substring(0, file.Name.LastIndexOf(".")) + "-";  
  15.   
  16.             Program obj = new Program();  
  17.   
  18.             for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber += interval)  
  19.             {  
  20.                 pageNameSuffix++;  
  21.                 string newPdfFileName = string.Format(pdfFileName + "{0}", pageNameSuffix);  
  22.                 obj.SplitAndSaveInterval(pdfFilePath, outputPath, pageNumber, interval, newPdfFileName);  
  23.             }  
  24.         }  
  25.   
  26.   
  27.         private void SplitAndSaveInterval(string pdfFilePath, string outputPath, int startPage, int interval, string pdfFileName)  
  28.         {            
  29.             using (PdfReader reader = new PdfReader(pdfFilePath))  
  30.             {  
  31.                 Document document = new Document();  
  32.                 PdfCopy copy = new PdfCopy(document, new FileStream(outputPath + "\\" + pdfFileName + ".pdf", FileMode.Create));  
  33.                 document.Open();  
  34.   
  35.                 for (int pagenumber = startPage; pagenumber < (startPage + interval); pagenumber++)  
  36.                 {  
  37.                     if (reader.NumberOfPages >= pagenumber)  
  38.                     {  
  39.                         copy.AddPage(copy.GetImportedPage(reader, pagenumber));  
  40.                     }  
  41.                     else  
  42.                     {  
  43.                         break;  
  44.                     }  
  45.   
  46.                 }  
  47.   
  48.                 document.Close();  
  49.             }  
  50.         }  
  51.     }   

In the code given above, we are using the PdfReader, FileInfo, Document and PdfCopy classes.

Now, I am going to explain the code written above.

Here, pdfFilePath variable is the old PDF location and outputPath variable is the location of new PDF files. 

  1. string pdfFilePath = @"C:\PdfFiles\sample.pdf";  
  2. string outputPath = @"C:\SplitedPdfFiles";   

The interval is the page(s) number of the PDF file from where we want to split the original PDF and divide into each new PDF files. It will have the same number of pages.

In my example, sample.pdf has 102 pages and the interval variable is 10, so each PDF file will contain 10 pages and the last PDF file will contain 2 pages.

We are using pageNameSuffix variable for giving the sequence number of each file with the PDF original name as sample-1.pdf, sample-2.pdf and so on. 

  1. int interval = 10;  
  2. int pageNameSuffix = 0;   

Now, The PdfReader instance contains the content of the source PDF file and we can get the number of pages of the PDF file, using the instance (reader) of PdfReader. We can increment pageNumber, as per interval value, using for loop, as given below. 

  1. // Intialize a new PdfReader instance with the contents of the source Pdf file:  
  2. PdfReader reader = new PdfReader(pdfFilePath);              
  3.   
  4. FileInfo file = new FileInfo(pdfFilePath);  
  5. string pdfFileName = file.Name.Substring(0, file.Name.LastIndexOf(".")) + "-";  
  6.   
  7. Program obj = new Program();  
  8.   
  9. for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber += interval)  
  10. {  
  11. pageNameSuffix++;  
  12. string newPdfFileName = string.Format(pdfFileName + "{0}", pageNameSuffix);  
  13. obj.SplitAndSaveInterval(pdfFilePath, outputPath, pageNumber, interval,             newPdfFileName);  
  14. }   

Here, we are using splitAndSaveInterval Method for all the operations of PDF, as per our requirement. Its always better to keep separate methods aside for the separation of concern.

Copy PDF page(s) from the original PDF file into new PDF, using parameterized constructor of PdfCopy class and add the page into the new PDF file, using AddPage Method. 

  1. private void SplitAndSaveInterval(string pdfFilePath, string outputPath, int    startPage, int interval, string pdfFileName)  
  2.         {            
  3.             using (PdfReader reader = new PdfReader(pdfFilePath))  
  4.             {  
  5.                 Document document = new Document();  
  6.                 PdfCopy copy = new PdfCopy(document, new FileStream(outputPath + "\\" + pdfFileName + ".pdf", FileMode.Create));  
  7.                 document.Open();  
  8.   
  9.                 for (int pagenumber = startPage; pagenumber < (startPage + interval); pagenumber++)  
  10.                 {  
  11.                     if (reader.NumberOfPages >= pagenumber)  
  12.                     {  
  13.                         copy.AddPage(copy.GetImportedPage(reader, pagenumber));  
  14.                     }  
  15.                     else  
  16.                     {  
  17.                         break;  
  18.                     }  
  19.   
  20.                 }  
  21.   
  22.                 document.Close();  
  23.             }  
  24.         }   

The screenshot is given below for newly created PDF files from the sample.pdf file.


In this example, I explained how to split a PDF file and save it into multiple PDF files, as per the requirement in C#, using iTextSharp .

Download the attachment for the source code of the sample Application.

Up Next
    Ebook Download
    View all
    Learn
    View all