IT Certificaions

Google Cloud Platform

Thursday, October 13, 2011

Create Nested table inside a table on a pdf document using iText

Nested table in iText to cerate pdf using Java
Suppose you are using iText to create pdf document using java, then this program will help you to create a table inside a table.

------------------------------------------------------------------

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


import java.io.FileOutputStream;


public class NestedTableExample {
  public static void main(String[] args) {
    Document document = new Document();


    try {
      PdfWriter.getInstance(document, 
    new FileOutputStream("NestedTableDemo.pdf"));


      document.open();


      PdfPTable table = new PdfPTable(2); // 2 columns.


      PdfPCell outerTableCell1 = new PdfPCell(new Paragraph("Outer Table :Cell 1"));
      PdfPCell outerTableCell2 = new PdfPCell(new Paragraph("Outer Table :Cell 2"));
      


      PdfPTable nestedTable = new PdfPTable(3);
      nestedTable.addCell(new Paragraph("Inside Nested Table :Cell 1"));
      nestedTable.addCell(new Paragraph("Inside Nested Table :Cell 2"));
 nestedTable.addCell(new Paragraph("Inside Nested Table : Cell 2"));


      outerTableCell2.addElement(nestedTable);


      table.addCell(outerTableCell1);
      table.addCell(outerTableCell2);
    
      document.add(table);


      document.close();


    } catch(Exception e){
      e.printStackTrace();
    }
  }
}
--------------------------------------------------------------------------------