0

I am using following code to create new document from existing pdf.

I want to shrink all pages(re-size) of existing pdf in new generated pdf.

Code i am using:

public void resize() throws IOException, DocumentException {

        PdfReader reader = new PdfReader("D:/test/scaned4.pdf");
        Document document = new Document(PageSize.LEGAL, 0, 0, 0, 0);

        System.out.println(reader.isTampered());

        PdfCopy copy = new PdfCopy(document, new FileOutputStream(
                "D:/test/result.pdf"));
        document.open();
        PdfImportedPage page;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            page = copy.getImportedPage(reader, i);
            copy.addPage(page);

        }
        reader.close();
        document.close();

}

There is any way to shrink imported page before add to new document.

Butani Vijay
  • 4,181
  • 2
  • 29
  • 61
  • 1
    The `Pdf*Copy*` family of classes are the classes of choice if you want to copy existing material as is with as completely as possible. If you want to manipulate the page, importing it into a `PdfWriter` is appropriate, and adding an imported page template to a `PdfWriter` allows you to scale it, rotate it, etc. – mkl Mar 20 '14 at 08:54
  • Thanks for suggestion. can you provide sample code that help me to scale page. – Butani Vijay Mar 20 '14 at 10:43
  • 1
    The [NUpTool](http://itextpdf.com/examples/iia.php?id=115) (a sample putting *n* scaled down pages of some document onto one page of the target document) shows how to do that. The scaling by `factor` is done in `cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY);`. – mkl Mar 20 '14 at 12:32
  • Thanks once again. can i use addTemplate(...) if i use PdfCopy as you have suggested and how?. – Butani Vijay Mar 20 '14 at 13:08
  • 2
    *can i use addTemplate(...) if i use PdfCopy* - I doubt that works as intended. `PdfCopy` is designed for copying pages unchanged, but you want to change it (you want to scale it down). Just use a `PdfWriter` as the [NUpTool](http://itextpdf.com/examples/iia.php?id=115) does. – mkl Mar 20 '14 at 13:18
  • i also want page unchanged except scale it down top and bottom for adding header and footer. – Butani Vijay Mar 21 '14 at 04:04

0 Answers0