Adding images to a table

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

24th October 2015
admin-marketing

Switch code for this example

ImagesNextToEachOther.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/19700549/itextsharp-images-are-not-coming-next-to-one-another
  4.  *
  5.  * We create a table with two columns and two cells.
  6.  * This way, we can add two images next to each other.
  7.  */
  8. package sandbox.tables;
  9.  
  10. import com.itextpdf.text.Document;
  11. import com.itextpdf.text.DocumentException;
  12. import com.itextpdf.text.Image;
  13. import com.itextpdf.text.pdf.PdfPCell;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20.  
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class ImagesNextToEachOther {
  25.     public static final String DEST = "results/tables/images_next_to_each_other.pdf";
  26.     public static final String IMG1 = "resources/images/javaone2013.jpg";
  27.     public static final String IMG2 = "resources/images/berlin2013.jpg";
  28.  
  29.     public static void main(String[] args) throws IOException,
  30.             DocumentException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new ImagesNextToEachOther().createPdf(DEST);
  34.     }
  35.    
  36.     public void createPdf(String dest) throws IOException, DocumentException {
  37.         Document document = new Document();
  38.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  39.         document.open();
  40.         PdfPTable table = new PdfPTable(2);
  41.         table.setWidthPercentage(100);
  42.         table.addCell(createImageCell(IMG1));
  43.         table.addCell(createImageCell(IMG2));
  44.         document.add(table);
  45.         document.close();
  46.     }
  47.    
  48.     public static PdfPCell createImageCell(String path) throws DocumentException, IOException {
  49.         Image img = Image.getInstance(path);
  50.         PdfPCell cell = new PdfPCell(img, true);
  51.         return cell;
  52.     }
  53. }
IconDescriptionTable.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/23375618/how-to-add-an-icon-to-an-itext-pdfpcell
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Image;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class IconDescriptionTable {
  22.     public static final String DEST = "results/tables/icon_description.pdf";
  23.     public static final String IMG = "resources/images/bulb.gif";
  24.  
  25.     public static void main(String[] args) throws IOException,
  26.             DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new IconDescriptionTable().createPdf(DEST);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         Document document = new Document();
  34.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         PdfPTable table = new PdfPTable(2);
  37.         table.setWidths(new int[]{ 1, 9 });
  38.         Image img = Image.getInstance(IMG);
  39.         table.addCell(new PdfPCell(img, true));
  40.         table.addCell(new Phrase("A light bulb icon"));
  41.         document.add(table);
  42.         document.close();
  43.     }
  44. }
ImageNextToText.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/25780258/how-to-display-image-and-text-beside-each-other-itext
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.Rectangle;
  13. import com.itextpdf.text.pdf.PdfPCell;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class ImageNextToText {
  24.     public static final String DEST = "results/tables/image_next_to_text.pdf";
  25.     public static final String IMG1 = "resources/images/javaone2013.jpg";
  26.  
  27.     public static void main(String[] args) throws IOException,
  28.             DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new ImageNextToText().createPdf(DEST);
  32.     }
  33.    
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document();
  36.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         document.open();
  38.         PdfPTable table = new PdfPTable(2);
  39.         table.setWidthPercentage(100);
  40.         table.setWidths(new int[]{1, 2});
  41.         table.addCell(createImageCell(IMG1));
  42.         table.addCell(createTextCell("This picture was taken at Java One.\nIt shows the iText crew at Java One in 2013."));
  43.         document.add(table);
  44.         document.close();
  45.     }
  46.    
  47.     public static PdfPCell createImageCell(String path) throws DocumentException, IOException {
  48.         Image img = Image.getInstance(path);
  49.         PdfPCell cell = new PdfPCell(img, true);
  50.         return cell;
  51.     }
  52.    
  53.     public static PdfPCell createTextCell(String text) throws DocumentException, IOException {
  54.         PdfPCell cell = new PdfPCell();
  55.         Paragraph p = new Paragraph(text);
  56.         p.setAlignment(Element.ALIGN_RIGHT);
  57.         cell.addElement(p);
  58.         cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
  59.         cell.setBorder(Rectangle.NO_BORDER);
  60.         return cell;
  61.     }
  62. }
ImagesInChunkInCell.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/32130219/itext-list-of-images-in-a-cell
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. /**
  21.  * @author Bruno Lowagie
  22.  */
  23. @WrapToTest
  24. public class ImagesInChunkInCell {
  25.     public static final String IMG = "resources/images/bulb.gif";
  26.     public static final String DEST = "results/tables/list_with_images.pdf";
  27.    
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new ImagesInChunkInCell().createPdf(DEST);
  32.     }
  33.        
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document();
  36.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         document.open();
  38.         Image image = Image.getInstance(IMG);
  39.         image.setScaleToFitHeight(false);
  40.         PdfPTable table = new PdfPTable(1);
  41.         table.setTotalWidth(new float[]{120});
  42.         table.setLockedWidth(true);
  43.         Phrase listOfDots = new Phrase();
  44.         for (int i = 0; i < 40; i++) {
  45.             listOfDots.add(new Chunk(image, 0, 0));
  46.             listOfDots.add(new Chunk(" "));
  47.         }
  48.         table.addCell(listOfDots);
  49.         document.add(table);
  50.         document.close();
  51.     }
  52. }
SimpleTable8.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/29752875/generate-pdf-from-java-code-that-dynamically-continue-file-template-with-itext
  4.  */
  5. package sandbox.tables;
  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.PdfImportedPage;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class SimpleTable8 {
  23.     public static final String DEST = "results/tables/simple_table8.pdf";
  24.    
  25.     public static void main(String[] args) throws IOException,
  26.             DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new SimpleTable8().createPdf(DEST);
  30.     }
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         Document document = new Document();
  33.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  34.         document.open();
  35.         PdfPTable table = new PdfPTable(3);
  36.         table.setWidthPercentage(100);
  37.         PdfReader reader = new PdfReader("resources/pdfs/header.pdf");
  38.         PdfImportedPage header = writer.getImportedPage(reader, 1);
  39.         PdfPCell cell = new PdfPCell(Image.getInstance(header));
  40.         cell.setColspan(3);
  41.         table.addCell(cell);
  42.         for (int row = 1; row
PositionContentInCell.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/31169268/cell-background-image-with-text-itextsharp
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.Rectangle;
  13. import com.itextpdf.text.pdf.ColumnText;
  14. import com.itextpdf.text.pdf.PdfContentByte;
  15. import com.itextpdf.text.pdf.PdfPCell;
  16. import com.itextpdf.text.pdf.PdfPCellEvent;
  17. import com.itextpdf.text.pdf.PdfPTable;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import sandbox.WrapToTest;
  24.  
  25. /**
  26.  *
  27.  * @author Bruno Lowagie (iText Software)
  28.  */
  29. @WrapToTest
  30. public class PositionContentInCell {
  31.     public static final String DEST = "results/tables/position_content_in_cell.pdf";
  32.     public static final String IMG = "resources/images/info.png";
  33.     public enum POSITION { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };
  34.    
  35.     class ImageEvent implements PdfPCellEvent {
  36.         protected Image img;
  37.         public ImageEvent(Image img) {
  38.             this.img = img;
  39.         }
  40.         public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
  41.             img.scaleToFit(position.getWidth(), position.getHeight());
  42.             img.setAbsolutePosition(position.getLeft() + (position.getWidth() - img.getScaledWidth()) / 2,
  43.                     position.getBottom() + (position.getHeight() - img.getScaledHeight()) / 2);
  44.             PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
  45.             try {
  46.                 canvas.addImage(img);
  47.             } catch (DocumentException ex) {
  48.                 // do nothing
  49.             }
  50.         }
  51.     }
  52.    
  53.     class PositionEvent implements PdfPCellEvent {
  54.         protected Phrase content;
  55.         protected POSITION pos;
  56.        
  57.         public PositionEvent(Phrase content, POSITION pos) {
  58.             this.content = content;
  59.             this.pos = pos;
  60.         }
  61.        
  62.         public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
  63.             PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
  64.             float x = 0;
  65.             float y = 0;
  66.             int alignment = 0;
  67.             switch (pos) {
  68.                 case TOP_LEFT:
  69.                     x = position.getLeft(3);
  70.                     y = position.getTop(content.getLeading());
  71.                     alignment = Element.ALIGN_LEFT;
  72.                     break;
  73.                 case TOP_RIGHT:
  74.                     x = position.getRight(3);
  75.                     y = position.getTop(content.getLeading());
  76.                     alignment = Element.ALIGN_RIGHT;
  77.                     break;
  78.                 case BOTTOM_LEFT:
  79.                     x = position.getLeft(3);
  80.                     y = position.getBottom(3);
  81.                     alignment = Element.ALIGN_LEFT;
  82.                     break;
  83.                 case BOTTOM_RIGHT:
  84.                     x = position.getRight(3);
  85.                     y = position.getBottom(3);
  86.                     alignment = Element.ALIGN_RIGHT;
  87.                     break;
  88.             }
  89.             ColumnText.showTextAligned(canvas, alignment, content, x, y, 0);
  90.         }
  91.     }
  92.    
  93.     public static void main(String[] args) throws IOException, DocumentException {
  94.         File file = new File(DEST);
  95.         file.getParentFile().mkdirs();
  96.         new PositionContentInCell().createPdf(DEST);
  97.     }
  98.    
  99.     public void createPdf(String dest) throws IOException, DocumentException {
  100.         // 1. Create a Document which contains a table:
  101.         Document document = new Document();
  102.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  103.         document.open();
  104.         PdfPTable table = new PdfPTable(2);
  105.         PdfPCell cell1 = new PdfPCell();
  106.         PdfPCell cell2 = new PdfPCell();
  107.         PdfPCell cell3 = new PdfPCell();
  108.         PdfPCell cell4 = new PdfPCell();
  109.         // 2. Inside that table, make each cell with specific height:
  110.         cell1.setFixedHeight(50);
  111.         cell2.setFixedHeight(50);
  112.         cell3.setFixedHeight(50);
  113.         cell4.setFixedHeight(50);
  114.         // 3. Each cell has the same background image
  115.         ImageEvent imgEvent = new ImageEvent(Image.getInstance(IMG));
  116.         cell1.setCellEvent(imgEvent);
  117.         cell2.setCellEvent(imgEvent);
  118.         cell3.setCellEvent(imgEvent);
  119.         cell4.setCellEvent(imgEvent);
  120.         // 4. Add text in front of the image at specific position
  121.         cell1.setCellEvent(new PositionEvent(new Phrase("Top left"), POSITION.TOP_LEFT));
  122.         cell2.setCellEvent(new PositionEvent(new Phrase("Top right"), POSITION.TOP_RIGHT));
  123.         cell3.setCellEvent(new PositionEvent(new Phrase("Bottom left"), POSITION.BOTTOM_LEFT));
  124.         cell4.setCellEvent(new PositionEvent(new Phrase("Bottom right"), POSITION.BOTTOM_RIGHT));
  125.         // Wrap it all up!
  126.         table.addCell(cell1);
  127.         table.addCell(cell2);
  128.         table.addCell(cell3);
  129.         table.addCell(cell4);
  130.         document.add(table);
  131.         document.close();
  132.     }
  133. }
PositionContentInCell2.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/31169268/cell-background-image-with-text-itextsharp
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.Rectangle;
  13. import com.itextpdf.text.pdf.ColumnText;
  14. import com.itextpdf.text.pdf.PdfContentByte;
  15. import com.itextpdf.text.pdf.PdfPCell;
  16. import com.itextpdf.text.pdf.PdfPCellEvent;
  17. import com.itextpdf.text.pdf.PdfPTable;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import sandbox.WrapToTest;
  24.  
  25. /**
  26.  *
  27.  * @author Bruno Lowagie (iText Software)
  28.  */
  29. @WrapToTest
  30. public class PositionContentInCell2 {
  31.     public static final String DEST = "results/tables/position_content_in_cell2.pdf";
  32.     public static final String IMG = "resources/images/info.png";
  33.    
  34.     class ImageEvent implements PdfPCellEvent {
  35.         protected Image img;
  36.         public ImageEvent(Image img) {
  37.             this.img = img;
  38.         }
  39.         public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
  40.             img.scaleAbsolute(position.getWidth(), position.getHeight());
  41.             img.setAbsolutePosition(position.getLeft(), position.getBottom());
  42.             PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
  43.             try {
  44.                 canvas.addImage(img);
  45.             } catch (DocumentException ex) {
  46.                 // do nothing
  47.             }
  48.         }
  49.     }
  50.    
  51.     class PositionEvent implements PdfPCellEvent {
  52.         protected Phrase content;
  53.         protected float wPct;
  54.         protected float hPct;
  55.         protected int alignment;
  56.        
  57.         public PositionEvent(Phrase content, float wPct, float hPct, int alignment) {
  58.             this.content = content;
  59.             this.wPct = wPct;
  60.             this.hPct = hPct;
  61.             this.alignment = alignment;
  62.         }
  63.        
  64.         public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
  65.             PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
  66.             float x = position.getLeft() + wPct * position.getWidth();
  67.             float y = position.getBottom() + hPct * (position.getHeight() - content.getLeading());
  68.             ColumnText.showTextAligned(canvas, alignment, content, x, y, 0);
  69.         }
  70.     }
  71.    
  72.     public static void main(String[] args) throws IOException, DocumentException {
  73.         File file = new File(DEST);
  74.         file.getParentFile().mkdirs();
  75.         new PositionContentInCell2().createPdf(DEST);
  76.     }
  77.    
  78.     public void createPdf(String dest) throws IOException, DocumentException {
  79.         // 1. Create a Document which contains a table:
  80.         Document document = new Document();
  81.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  82.         document.open();
  83.         PdfPTable table = new PdfPTable(2);
  84.         PdfPCell cell1 = new PdfPCell();
  85.         PdfPCell cell2 = new PdfPCell();
  86.         PdfPCell cell3 = new PdfPCell();
  87.         PdfPCell cell4 = new PdfPCell();
  88.         PdfPCell cell5 = new PdfPCell();
  89.         PdfPCell cell6 = new PdfPCell();
  90.         PdfPCell cell7 = new PdfPCell();
  91.         PdfPCell cell8 = new PdfPCell();
  92.         // 2. Inside that table, make each cell with specific height:
  93.         cell1.setFixedHeight(50);
  94.         cell2.setFixedHeight(50);
  95.         cell3.setFixedHeight(50);
  96.         cell4.setFixedHeight(50);
  97.         cell5.setFixedHeight(50);
  98.         cell6.setFixedHeight(50);
  99.         cell7.setFixedHeight(50);
  100.         cell8.setFixedHeight(50);
  101.         // 3. Each cell has the same background image
  102.         ImageEvent imgEvent = new ImageEvent(Image.getInstance(IMG));
  103.         cell1.setCellEvent(imgEvent);
  104.         cell2.setCellEvent(imgEvent);
  105.         cell3.setCellEvent(imgEvent);
  106.         cell4.setCellEvent(imgEvent);
  107.         cell5.setCellEvent(imgEvent);
  108.         cell6.setCellEvent(imgEvent);
  109.         cell7.setCellEvent(imgEvent);
  110.         cell8.setCellEvent(imgEvent);
  111.         // 4. Add text in front of the image at specific position
  112.         cell1.setCellEvent(new PositionEvent(new Phrase(14, "Top left"), 0, 1, Element.ALIGN_LEFT));
  113.         cell2.setCellEvent(new PositionEvent(new Phrase(14, "Top right"), 1, 1, Element.ALIGN_RIGHT));
  114.         cell3.setCellEvent(new PositionEvent(new Phrase(14, "Top center"), 0.5f, 1, Element.ALIGN_CENTER));
  115.         cell4.setCellEvent(new PositionEvent(new Phrase(14, "Bottom center"), 0.5f, 0, Element.ALIGN_CENTER));
  116.         cell5.setCellEvent(new PositionEvent(new Phrase(14, "Middle center"), 0.5f, 0.5f, Element.ALIGN_CENTER));
  117.         cell6.setCellEvent(new PositionEvent(new Phrase(14, "Middle center"), 0.5f, 0.5f, Element.ALIGN_CENTER));
  118.         cell7.setCellEvent(new PositionEvent(new Phrase(14, "Bottom left"), 0, 0, Element.ALIGN_LEFT));
  119.         cell8.setCellEvent(new PositionEvent(new Phrase(14, "Bottom right"), 1, 0, Element.ALIGN_RIGHT));
  120.         // Wrap it all up!
  121.         table.addCell(cell1);
  122.         table.addCell(cell2);
  123.         table.addCell(cell3);
  124.         table.addCell(cell4);
  125.         table.addCell(cell5);
  126.         table.addCell(cell6);
  127.         table.addCell(cell7);
  128.         table.addCell(cell8);
  129.         document.add(table);
  130.         document.close();
  131.     }
  132. }
AddOverlappingImage.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/22094289/itext-precisely-position-an-image-on-top-of-a-pdfptable
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.ExceptionConverter;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfPCell;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfPTableEvent;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21.  
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class AddOverlappingImage {
  26.    
  27.     public static final String DEST = "results/tables/add_overlapping_image.pdf";
  28.  
  29.     public class ImageContent implements PdfPTableEvent {
  30.         protected Image content;
  31.         public ImageContent(Image content) {
  32.             this.content = content;
  33.         }
  34.         public void tableLayout(PdfPTable table, float[][] widths,
  35.                 float[] heights, int headerRows, int rowStart,
  36.                 PdfContentByte[] canvases) {
  37.             try {
  38.                 PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
  39.                 float x = widths[3][1] + 10;
  40.                 float y = heights[3] - 10 - content.getScaledHeight();
  41.                 content.setAbsolutePosition(x, y);
  42.                 canvas.addImage(content);
  43.             } catch (DocumentException e) {
  44.                 throw new ExceptionConverter(e);
  45.             }
  46.         }
  47.     }
  48.    
  49.     public static void main(String[] args) throws IOException, DocumentException {
  50.         File file = new File(DEST);
  51.         file.getParentFile().mkdirs();
  52.         new AddOverlappingImage().createPdf(DEST);
  53.     }
  54.    
  55.     public void createPdf(String dest) throws IOException, DocumentException {
  56.         Document document = new Document();
  57.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  58.         document.open();
  59.         PdfPTable table = new PdfPTable(5);
  60.         table.setTableEvent(new ImageContent(Image.getInstance("resources/images/hero.jpg")));
  61.         table.setWidthPercentage(100);
  62.         PdfPCell cell;
  63.         for (int r = 'A'; r
MultipleImagesInCell.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/33603296/how-to-add-two-images-in-one-cell-in-itext
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. /**
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class MultipleImagesInCell {
  25.    
  26.     public static final String DEST = "results/tables/images_in_cell.pdf";
  27.     public static final String IMG1 = "resources/images/brasil.png";
  28.     public static final String IMG2 = "resources/images/dog.bmp";
  29.     public static final String IMG3 = "resources/images/fox.bmp";
  30.  
  31.     public static void main(String[] args) throws IOException,
  32.             DocumentException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new MultipleImagesInCell().createPdf(DEST);
  36.     }
  37.  
  38.     public void createPdf(String dest) throws IOException, DocumentException {
  39.         Image img1 = Image.getInstance(IMG1);
  40.         Image img2 = Image.getInstance(IMG2);
  41.         Image img3 = Image.getInstance(IMG3);
  42.         Document document = new Document();
  43.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  44.         document.open();
  45.         PdfPTable table = new PdfPTable(1);
  46.         table.setWidthPercentage(50);
  47.         table.addCell("Different images, one after the other vertically:");
  48.         PdfPCell cell = new PdfPCell();
  49.         cell.addElement(img1);
  50.         cell.addElement(img2);
  51.         cell.addElement(img3);
  52.         table.addCell(cell);
  53.         document.add(table);
  54.         document.newPage();
  55.         table = new PdfPTable(1);
  56.         table.addCell("Different images, one after the other vertically, but scaled:");
  57.         cell = new PdfPCell();
  58.         img1.setWidthPercentage(20);
  59.         cell.addElement(img1);
  60.         img2.setWidthPercentage(20);
  61.         cell.addElement(img2);
  62.         img3.setWidthPercentage(20);
  63.         cell.addElement(img3);
  64.         table.addCell(cell);
  65.         table.addCell("Different images, one after the other horizontally:");
  66.         Paragraph p = new Paragraph();
  67.         img1.scalePercent(30);
  68.         p.add(new Chunk(img1, 0, 0, true));
  69.         p.add(new Chunk(img2, 0, 0, true));
  70.         p.add(new Chunk(img3, 0, 0, true));
  71.         cell = new PdfPCell();
  72.         cell.addElement(p);
  73.         table.addCell(cell);
  74.         table.addCell("Text and images (mixed):");
  75.         p = new Paragraph("The quick brown ");
  76.         p.add(new Chunk(img3, 0, 0, true));
  77.         p.add(" jumps over the lazy ");
  78.         p.add(new Chunk(img2, 0, 0, true));
  79.         cell = new PdfPCell();
  80.         cell.addElement(p);
  81.         table.addCell(cell);
  82.         document.add(table);
  83.         document.close();
  84.     }
  85. }
MultipleImagesInTable.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/34303448
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. /**
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class MultipleImagesInTable {
  25.    
  26.     public static final String DEST = "results/tables/images_in_table_sequence.pdf";
  27.     public static final String IMG1 = "resources/images/brasil.png";
  28.     public static final String IMG2 = "resources/images/dog.bmp";
  29.     public static final String IMG3 = "resources/images/fox.bmp";
  30.  
  31.     public static void main(String[] args) throws IOException,
  32.             DocumentException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new MultipleImagesInTable().createPdf(DEST);
  36.     }
  37.  
  38.     public void createPdf(String dest) throws IOException, DocumentException {
  39.         Image img1 = Image.getInstance(IMG1);
  40.         Image img2 = Image.getInstance(IMG2);
  41.         Image img3 = Image.getInstance(IMG3);
  42.         Document document = new Document();
  43.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  44.         document.open();
  45.         PdfPTable table = new PdfPTable(1);
  46.         table.setWidthPercentage(20);
  47.         table.addCell(img1);
  48.         table.addCell("Brazil");
  49.         table.addCell(img2);
  50.         table.addCell("Dog");
  51.         table.addCell(img3);
  52.         table.addCell("Fox");
  53.         document.add(table);
  54.         document.close();
  55.     }
  56. }
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