Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split PDF into multiple PDFs using iTextsharp

public int SplitAndSave(string inputPath, string outputPath)
    {
        FileInfo file = new FileInfo(inputPath);
        string name = file.Name.Substring(0, file.Name.LastIndexOf("."));

        using (PdfReader reader = new PdfReader(inputPath))
        {

            for (int pagenumber = 1; pagenumber <= reader.NumberOfPages; pagenumber++)
            {
                string filename = pagenumber.ToString() + ".pdf";

                Document document = new Document();
                PdfCopy copy = new PdfCopy(document, new FileStream(outputPath + "\\" + filename, FileMode.Create));

                document.Open();

                copy.AddPage(copy.GetImportedPage(reader, pagenumber));

                document.Close();
            }
            return reader.NumberOfPages;
        }

    }

I want to split the Pdf in to multiple PDFs with 50 pages interval.(Suppoose If there are 400 pages PDF, I want 8 pdfs). The above code is splitting every page into a pdf. Please help me...I'm using asp.net with iTextSharp.

like image 233
Billy Avatar asked Sep 12 '13 10:09

Billy


People also ask

How to extract pages from one PDF file using iTextSharp?

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. Now, add three namespaces in top of .cs page, which are given below. Write the code in the Program class to extract the pages from one PDF and save into multiple PDF files.

How to split pages from one PDF file and save?

We have to follow some simple steps to split the pages from one PDF file and save into multiple PDF files. 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. Now, add three namespaces in top of .cs page, which are given below.

What is the PDF Split and merge tool?

This tool makes use of the iTextSharp library, which allows you to create and modify documents in the Portable Document Format (PDF). The source of the PDF split and merge tool can be found on top of this article. The two screenshots below show how the UI of the application looks like, simple and easy to use.

How to extract pages from one PDF and save into multiple PDFs?

We can install, using Package Manager Console with the the command given below. Now, add three namespaces in top of .cs page, which are given below. Write the code in the Program class to extract the pages from one PDF and save into multiple PDF files. In the code given above, we are using the PdfReader, FileInfo, Document and PdfCopy classes.


1 Answers

You're looping through the pdf and creating a new document every time you advance a page. You'll need to keep track of your pages so that you perform split only every 50 pages. Personally I would put that in a separate method and call it from your loop. Something like this:

private void ExtractPages(string sourcePDFpath, string outputPDFpath, int startpage,  int endpage)
{
    PdfReader reader = null;
    Document sourceDocument = null;
    PdfCopy pdfCopyProvider = null;
    PdfImportedPage importedPage = null;

    reader = new PdfReader(sourcePDFpath);
    sourceDocument = new Document(reader.GetPageSizeWithRotation(startpage));
    pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPDFpath, System.IO.FileMode.Create));

    sourceDocument.Open();

    for (int i = startpage; i <= endpage; i++)
    {
        importedPage = pdfCopyProvider.GetImportedPage(reader, i);
        pdfCopyProvider.AddPage(importedPage);
    }
    sourceDocument.Close();
    reader.Close();
}

So in your original code loop through your pdf and every 50 pages call the above method. You'll just need to add variables in your block to keep track of the start/end pages.

like image 163
ovaltein Avatar answered Sep 29 '22 12:09

ovaltein