extracting one page from pdf file using iText

14,019

I have downloaded a single file from your repository: Abdomen.pdf

I have then used the following code to "burst" that PDF:

public static void main(String[] args) throws DocumentException, IOException {
    PdfReader reader = new PdfReader("resources/Abdomen.pdf");
    int n = reader.getNumberOfPages();
    reader.close();
    String path;
    PdfStamper stamper;
    for (int i = 1; i <= n; i++) {
        reader = new PdfReader("resources/abdomen.pdf");
        reader.selectPages(String.valueOf(i));
        path = String.format("results/abdomen/p-%s.pdf", i);
        stamper = new PdfStamper(reader,new FileOutputStream(path));
        stamper.close();
        reader.close();
    }
}

To "burst" means to split in separate pages. While the original file Abdomen.pdf is 72,570 KB (about 70.8 MB), the separate pages are much smaller:

enter image description here

I can not reproduce the problem you describe.

Share:
14,019
Ahmed
Author by

Ahmed

Updated on June 04, 2022

Comments

  • Ahmed
    Ahmed almost 2 years

    I want to return one page from pdf files from java servlet (to reduce file size download), using itext library. using this code

         try {
            PdfReader reader = new PdfReader(input);
            Document document = new Document(reader.getPageSizeWithRotation(page_number) );
    
    
            PdfSmartCopy copy1 = new PdfSmartCopy(document, response.getOutputStream());
            copy1.setFullCompression();
            document.open();
    
            copy1.addPage(copy1.getImportedPage(reader, page_i) );
            copy1.freeReader(reader);
            reader.close();
    
            document.close();
    
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    

    this code returns the page, but the file size is large and some times equals the original file size, even it is just a one page.

  • Ahmed
    Ahmed about 9 years
    but for some files like (Introduction 2013.pdf), the produced files is very large, see this link (2-dot-sheet-s0.appspot.com/…) the file size produced is about 12 MB which is equal to the pdf size.
  • Bruno Lowagie
    Bruno Lowagie about 9 years
    I am no longer at my computer, but on my phone. If you wanted me to test a particular file, you should have said so. This will learn you to be more precise in the future.
  • mkl
    mkl about 9 years
    @Ahmed In the file Introduction 2013.pdf you have the same problem as discussed in the question itextsharp: splitted pages size equals file size: All pages share a single Resources dictionary, 4 0 R in your case. Thus, splitting copies all resources for each page. Before splitting, therefore, you should refine the PDF to have individual Resources dictionaries for each page which do only contain the resources actually used on that page, cf. my answer to that other question.
  • Bruno Lowagie
    Bruno Lowagie about 9 years
    That's a great answer, @mkl and it shows that some PDFs aren't created the way they should be...