C# tutorial: split PDF file


Split PDF file

In the previous tutorial, you learnt how to write C# code to combine or merge many PDF files to become a single-page PDF file. It is a straight forward process. In some situations the reversing task can occur. For example, once a PDF file is too large, it is not easy to send this file via e-mail to your classmates or co-workers. So it must be split.

By using iTextSharp library, you can easily split a large PDF file into many single-page PDF files. You will have a PdfReader object to read the large file. Then create a new PdfCopy object for every page to split the document
into individual pages. You can reuse the same PdfReader object for different PdfCopy objects. The example C# code below will split the jmf_tutorial.pdf file to many single-page PDF files. The original file has 25 pages and its size is about 185KB. After spliting you will get 25 PDF files. Each PDF file has one page with a file size of about 101KB. By multiplying 25 by 101KB, you will get 2525KB which is greater than the original 185KB. This is because resources that were shared among pages in the original PDF file are now copied into each separate PDF file.

//variables
String source_file="D:/jmf_tutorial.pdf";
String result = "d:/result";
PdfCopy copy;
//create PdfReader object
PdfReader reader = new PdfReader(source_file);

for (int i = 1; i <=reader.NumberOfPages ; i++)
{
//create Document object
Document document = new Document();
copy = new PdfCopy(document, new FileStream(result+i+".pdf", FileMode.Create));
//open the document
document.Open();
//add page to PdfCopy
copy.AddPage(copy.GetImportedPage(reader,i));
//close the document object
document.Close();
}

split pdf file


Comments

Carlos Henriquez comment

 Carlos Henriquez

Gracias, desde Chile


2017-01-23
siva comment

 siva

excellent
its working fine


2017-01-10
C# pdf split and merge comment

 C# pdf split and merge

http://www.rasteredge.com/how-to/csharp-imaging/pdf-split/


2016-04-12



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.