Tiling images

This is a code example of iText PDF, discover more.

31st May 2016
iText PDF

Switch code for this example

TiledImage.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This code sample was written by Bruno Lowagie in answer to this question:
  10.  * http://stackoverflow.com/questions/26859473/how-to-show-an-image-with-large-dimensions-across-multiple-pages-in-itextpdf
  11.  */
  12. package com.itextpdf.samples.sandbox.images;
  13.  
  14. import com.itextpdf.io.image.ImageData;
  15. import com.itextpdf.io.image.ImageDataFactory;
  16. import com.itextpdf.kernel.geom.PageSize;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26.  
  27. @Category(SampleTest.class)
  28. public class TiledImage extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/images/tiled_image.pdf";
  30.     public static final String IMAGE = "./src/test/resources/img/bruno_ingeborg.jpg";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new TiledImage().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         ImageData image = ImageDataFactory.create(IMAGE);
  41.         float width = image.getWidth();
  42.         float height = image.getHeight();
  43.         PageSize pageSize = new PageSize(width / 2, height / 2);
  44.  
  45.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  46.         pdfDoc.setDefaultPageSize(pageSize);
  47.  
  48.         PdfCanvas canvas;
  49.         canvas = new PdfCanvas(pdfDoc.addNewPage().newContentStreamBefore(),
  50.                 pdfDoc.getLastPage().getResources(), pdfDoc);
  51.         canvas.addImage(image, width, 0, 0, height, 0, -height / 2, false);
  52.         canvas = new PdfCanvas(pdfDoc.addNewPage().newContentStreamBefore(),
  53.                 pdfDoc.getLastPage().getResources(), pdfDoc);
  54.         canvas.addImage(image, width, 0, 0, height, 0, 0, false);
  55.         canvas = new PdfCanvas(pdfDoc.addNewPage().newContentStreamBefore(),
  56.                 pdfDoc.getLastPage().getResources(), pdfDoc);
  57.         canvas.addImage(image, width, 0, 0, height, -width / 2, -height / 2, false);
  58.         canvas = new PdfCanvas(pdfDoc.addNewPage().newContentStreamBefore(),
  59.                 pdfDoc.getLastPage().getResources(), pdfDoc);
  60.         canvas.addImage(image, width, 0, 0, height, -width / 2, 0, false);
  61.  
  62.         pdfDoc.close();
  63.     }
  64. }
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