Splite PDF document using Csharp(pdf Sharp dll)

Some times we have requirement to Splite PDF document into several PDF Documents.
In this Article i am explaining how to Splite Large pdf document.
For this i used pdfSharp dll which is free available into internet.
i am describing this example step by step.

Step 1(Create new Project)
Open Visual Studio
Go to File–>New–>Project

Go to Window Form Application and Give the “SplitPdfDoc” name to the project.

a new Form is showing right Click on Window Form and select View Code.

Step 2
dowonload PdfSharp.dll from internet.
now add the reference of this dll to your project.
Right click on your solution Explorer–> Add Reference –> Browse(browse the downloaded dll path)–> ok
now add the button control on your page.
Drag and Drop the Button Control from the Toolbar and name it “btnsplitepdf”.
double click on button and generate the OnClick Event.

Step 3
put your input.pdf(you can choose another name) into debug folder of your project.
output file will also created in debug folder of your project.

Go to CodeBehind and copy paste the following code within button Onclick Event.

        private void btnsplitepdfdoc_Click(object sender, EventArgs e)
        {
            // input pdf file name
            string inputfile = "input.pdf";
            // read the pdf document
            PdfDocument inputPDFDocument = PdfReader.Open(inputfile, PdfDocumentOpenMode.Import);
           // get pdf file name without .pdf extension
            string filenamewoext = Path.GetFileNameWithoutExtension(inputfile);
            // loop for spliting the pdf document on the bases of pageIndex
            for (int pageIndex = 0; pageIndex < inputPDFDocument.PageCount; pageIndex++)
            {
                // create new output pdf document
                PdfDocument outpdfdoc = new PdfDocument();
                outpdfdoc.Version = inputPDFDocument.Version;
                outpdfdoc.Info.Title =
                  String.Format("Page {0} of {1}", pageIndex + 1, inputPDFDocument.Info.Title);
                outpdfdoc.Info.Creator = inputPDFDocument.Info.Creator;
 
                // Add the page
                outpdfdoc.AddPage(inputPDFDocument.Pages[pageIndex]);
                // save the new output page
                outpdfdoc.Save(String.Format("{0}-{1}.pdf", filenamewoext, pageIndex + 1));

        }
    }
Advertisements
This entry was posted in Asp.Net. Bookmark the permalink.

One Response to Splite PDF document using Csharp(pdf Sharp dll)

  1. Pingback: Merge PDF pages into One PDF Document in Csharp(Pdf Sharp dll) | Asp.Net , Sql queries Code Example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s