Multiple images

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

1st November 2015
admin-marketing

Switch code for this example

MultipleImages.java
  1. /**
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/29331838/add-multiple-images-into-a-single-pdf-file-with-itext-using-java
  4.  */
  5. package sandbox.images;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Image;
  10. import com.itextpdf.text.pdf.PdfWriter;
  11.  
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import sandbox.WrapToTest;
  16.  
  17. @WrapToTest
  18. public class MultipleImages {
  19.     public static final String[] IMAGES = {
  20.         "resources/images/berlin2013.jpg",
  21.         "resources/images/javaone2013.jpg",
  22.         "resources/images/map_cic.png"
  23.     };
  24.     public static final String DEST = "results/images/multiple_images.pdf";
  25.    
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new MultipleImages().createPdf(DEST);
  30.     }
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         Image img = Image.getInstance(IMAGES[0]);
  33.         Document document = new Document(img);
  34.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         for (String image : IMAGES) {
  37.             img = Image.getInstance(image);
  38.             document.setPageSize(img);
  39.             document.newPage();
  40.             img.setAbsolutePosition(0, 0);
  41.             document.add(img);
  42.         }
  43.         document.close();
  44.     }
  45. }
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