locked
extracting pdf pages using itextsharp RRS feed

  • Question

  • User-2027253991 posted
    Hai,
    How to extract specific pages in pdf using itextsharp?
    Suppose i have 16 pages of pdf i want to extract page number 2,8,16 from that 16pages.Can any suggest me.
    Thanks
    basanth
    Hai,

    How to extract specific pages in pdf using itextsharp?

    Suppose i have 16 pages of pdf i want to extract page number 2,8,16 from that 16pages.Can any suggest me.


    Thanks
    basanth
    Monday, December 6, 2010 2:14 AM

All replies

  • User-1446685904 posted

    http://jamesewelch.wordpress.com/2008/11/14/how-to-extract-pages-from-a-pdf-document/

    http://www.vbforums.com/showthread.php?t=490456

    private static void ExtractPages(string inputFile, string outputFile,
        int start, int end)
    {
        // get input document
        PdfReader inputPdf = new PdfReader(inputFile);
    
        // retrieve the total number of pages
        int pageCount = inputPdf.NumberOfPages;
    
        if (end < start || end > pageCount)
        {
            end = pageCount;
        }
    
        // load the input document
        Document inputDoc =
            new Document(inputPdf.GetPageSizeWithRotation(1));
    
        // create the filestream
        using (FileStream fs = new FileStream(outputFile, FileMode.Create))
        {
            // create the output writer
            PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc, fs);
            inputDoc.Open();
            PdfContentByte cb1 = outputWriter.DirectContent;
    
            // copy pages from input to output document
            for (int i = start; i <= end; i++)
            {
                inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(i));
                inputDoc.NewPage();
    
                PdfImportedPage page =
                    outputWriter.GetImportedPage(inputPdf, i);
                int rotation = inputPdf.GetPageRotation(i);
    
                if (rotation == 90 || rotation == 270)
                {
                    cb1.AddTemplate(page, 0, -1f, 1f, 0, 0,
                        inputPdf.GetPageSizeWithRotation(i).Height);
                }
                else
                {
                    cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }
            }
    
            inputDoc.Close();
        }
    }


    Monday, December 6, 2010 3:50 AM