How to show an image with large dimensions across multiple pages?

I have an image with large dimensions which I need to display in a PDF file. I cant scale to fit the image as this would make text on the node illegible. How could I split the image into multiple pa

I have an image with large dimensions which I need to display in a PDF file. I cant scale to fit the image as this would make text on the node illegible. How could I split the image into multiple pages while retaining its original dimensions?

Posted on StackOverflow on Nov 11, 2014 by Raja

Please take a look at the TiledImage example. It takes an image at its original size and it tiles it over 4 pages: tiled_image.pdf

Screen shot
Screen shot

To make this work, I first asked the image for its size:

ImageData image = ImageDataFactory.create(IMAGE);
float width = image.getWidth();
float height = image.getHeight();

To make sure each page is as big as one fourth of the page, I define this special PageSize:

PageSize pageSize = new PageSize(width / 2, height / 2);

I use this pageSize when creating the PdfDocument instance and I add the same image 4 times using different coordinates:

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
pdfDoc.setDefaultPageSize(pageSize);
PdfCanvas canvas;
canvas = new PdfCanvas(pdfDoc.addNewPage().newContentStreamBefore(),
        pdfDoc.getLastPage().getResources(), pdfDoc);
canvas.addImage(image, width, 0, 0, height, 0, -height / 2, false);
canvas = new PdfCanvas(pdfDoc.addNewPage().newContentStreamBefore(),
        pdfDoc.getLastPage().getResources(), pdfDoc);
canvas.addImage(image, width, 0, 0, height, 0, 0, false);
canvas = new PdfCanvas(pdfDoc.addNewPage().newContentStreamBefore(),
        pdfDoc.getLastPage().getResources(), pdfDoc);
canvas.addImage(image, width, 0, 0, height, -width / 2, -height / 2, false);
canvas = new PdfCanvas(pdfDoc.addNewPage().newContentStreamBefore(),
        pdfDoc.getLastPage().getResources(), pdfDoc);
canvas.addImage(image, width, 0, 0, height, -width / 2, 0, false);
pdfDoc.close();

Now I have distributed the image over different pages, which is exactly what you are trying to achieve ;-)

Click this link if you want to see how to answer this question in iText 5.


Share this article

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now