Hello All,

I am going to demonstrate you how to split a single multipage Pdf file into multiple single page pdf files in c#. There are many commercial tool available for it on the web today but there is an open source library named PDFSharp which can do the same task and absolutely free.

In one of my project, i had the requirement to split a PDF file into multiple single page file. After googling for hours, i found this excellent library and successfully spilted the PDF file using PDFSharp, i thought it would be nice to post it on my blog so that it might help others seeking the same feature in .net.

Following were the steps i followed to do this task:

1) Downloaded latest code of PDFSharp from Codeplex.

2) Compiled the source code and copied PDFSharp.dll into the bin directory of the application.

3) Added reference to this dll in the project.

 

To spilt the PDF file, i used the following code:

 

private static void SplitPDFIntoSinglePagePDFs()
        {
            string inputFilename = "Silverlight-Straight-Talk.pdf"; 
            PdfDocument inputPDFDocument = PdfReader.Open(inputFilename, PdfDocumentOpenMode.Import);

            string name = Path.GetFileNameWithoutExtension(inputFilename);
            Console.WriteLine("Splitting started.....");
            for (int pageIndex = 0; pageIndex < inputPDFDocument.PageCount; pageIndex++)
            {
                // Create new document
                PdfDocument outputPDFDocument = new PdfDocument();
                outputPDFDocument.Version = inputPDFDocument.Version;
                outputPDFDocument.Info.Title =
                  String.Format("Page {0} of {1}", pageIndex + 1, inputPDFDocument.Info.Title);
                outputPDFDocument.Info.Creator = inputPDFDocument.Info.Creator;

                // Add the page and save it
                outputPDFDocument.AddPage(inputPDFDocument.Pages[pageIndex]);
                outputPDFDocument.Save(String.Format("output/{0} - Page {1}.pdf", name, pageIndex + 1));
                Console.WriteLine("Splitted Page {0}", pageIndex + 1);
            }
        }

 

The above code split a pdf located in the bin/debug directory of the application, splits it into single page pdf's and saves all the files in bin/debug/output folder.

You can test it yourself by downloading the complete source code from here.

SpiltPdfDemo.zip (2.02 mb)