Images can be added very easily in the tables using the
PdfPTable.addCell(Image image) method.
Lets see example on how to add image in table using iText.
Example – Add image in table using iText – Simple image
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package com.kscodes.examples.itext; import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; public class TableImageExample { public static void main(String args[]) { // Create a Document object Document document = new Document(); try { // Create a PdfWriter instance and use the document to write the pdf // to a specified location PdfWriter.getInstance(document, new FileOutputStream( "K:\\Kscodes\\pdf\\itext-image-tables.pdf")); // open the document document.open(); // Create a Simple table PdfPTable table = new PdfPTable(2); // Set First row as header table.setHeaderRows(1); // Add header details table.addCell("Fruit"); table.addCell("Image"); // Add the data table.addCell("Apple"); Image image1 = Image .getInstance("K:\\Kscodes\\Images\\apple.jpg"); table.addCell(image1); // Add more data table.addCell("Orange"); Image image2 = Image .getInstance("K:\\Kscodes\\Images\\orange.jpg"); table.addCell(image2); document.add(table); // close the document document.close(); System.out.println("PDF created at the location !!!"); } catch (Exception e) { System.out.println("Exception occured :: " + e); } } } |
Output