Friday 4 November 2016

Itext: Add list of images to same cell in table

Following statements are used to add list of images to same cell in pdf table.

PdfPTable table = new PdfPTable(1);

PdfPCell cell = new PdfPCell();
cell.addElement(Image.getInstance("lion.jpeg"));
cell.addElement(Image.getInstance("tiger.jpg"));
cell.addElement(Image.getInstance("rabbit.jpeg"));

table.addCell(cell);

Following is the complete working application.
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TestTable {
 private static final String fileName = "/Users/harikrishna_gurram/Documents/itext/tableEx.pdf";

 public static void main(String args[]) throws DocumentException, MalformedURLException, IOException {

  Document document = new Document();
  PdfWriter.getInstance(document, new FileOutputStream(fileName));
  document.open();

  PdfPTable table = new PdfPTable(1);

  PdfPCell cell = new PdfPCell();
  cell.addElement(Image.getInstance("lion.jpeg"));
  cell.addElement(Image.getInstance("tiger.jpg"));
  cell.addElement(Image.getInstance("rabbit.jpeg"));

  table.addCell(cell);

  document.add(table);

  document.close();
 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment