How to Create Nested Tables in Pdf in java


In this core java tutorial we will learn How to Create Nested Tables in Pdf - Example using iText library - core java tutorial in Java with program and examples.


We will create top level PdfPTable (com.itextpdf.text.pdf.PdfPTable) and then create top level table cells ( com.itextpdf.text.pdf.PdfPCell) and then create another table i.e. nested table and add that in top level table cells in java in pdf in itext in java.


How to Set the Font Name, Size, Style and Colour In Pdf using itext in java

How To Set HyperLink (Anchor - a tag) In Pdf in java - iText java tutorial

Convert Html To Pdf in java using iText - iText java tutorial

How To Set Header and Footer in pdf in java using Itext Example - iText java tutorial




Download jars required to execute program >

Or, you may download jars from here.

Program/Example - How to Create Nested Tables in Pdf in java - Example  - Using iText library - core java tutorial in Java


import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
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;
/**
*
*  How to Create Nested Tables in Pdf - Example
*  Using iText library - core java tutorial
*
*/
public class CreateNestedTablesPdfExample {
   public static void main(String[] args) {
          try {
                 String pdfFilePath = "e:/Create Nested Tables Pdf Example.pdf";
                 OutputStream fos = new FileOutputStream(new File(pdfFilePath));
                 Document document = new Document();
                 PdfWriter.getInstance(document, fos);
                 document.open();
                 PdfPTable topLevelTable = new PdfPTable(2); // Create 2 columns in
                                                                                            // TOP LEVEL TABLE
                 PdfPCell topLevelTablecell1 = new PdfPCell(new Paragraph("Cell 1"));
                 PdfPCell topLevelTablecell2 = new PdfPCell(new Paragraph("Cell 2"));
                 PdfPTable nestedTable = new PdfPTable(2); // Create 2 columns in
                                                                                            // NESTED TABLE
                 PdfPCell nestedTableCell1 = new PdfPCell(new Paragraph(
                              "Nested table - Cell 1"));
                 PdfPCell nestedTableCell2 = new PdfPCell(new Paragraph(
                              "Nested table - Cell 2"));
                 nestedTable.addCell(nestedTableCell1);
                 nestedTable.addCell(nestedTableCell2);
             topLevelTablecell2.addElement(nestedTable); // add nested table in
                                                                                            // top level table cell
                 // Now, add both cells in top level table.
                 topLevelTable.addCell(topLevelTablecell1);
                 topLevelTable.addCell(topLevelTablecell2);
                 document.add(topLevelTable);
                 document.close();
                 fos.close();
                 System.out.println("PDF created in >> " + pdfFilePath);
          } catch (Exception e) {
                 e.printStackTrace();
          }
   }
}
/* Output of above program
PDF created in >> e:/Create Nested Tables Pdf Example.pdf
*/



PDF formed after executing above java program (How to Create Nested Tables in Pdf in java) will look like this >


Summary -
So in this core java tutorial we learned How to Create Nested Tables in Pdf - Example using iText library. We created top level PdfPTable (com.itextpdf.text.pdf.PdfPTable) and then created top level table cells ( com.itextpdf.text.pdf.PdfPCell) and then created another table i.e. nested table and added that in top level table cells in java in pdf in itext in java.




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

References >



RELATED LINKS>

Create the first Pdf Example Using iText library in java - iText java tutorial example

Setting Pdf Attributes like Title, Author, Creator, subject, Keywords, Header and CreationDate in java



Creating TABLES in pdf in java - iText tutorial >

How to Create the Table In Pdf in java - iText java tutorial

Rotate cells, cell padding, horizontal and vertical alignment of Cell in table in Pdf in java itext - iText java tutorial example




Creating LIST in pdf in java - iText tutorial >

Create Greek List In pdf in java using iText - iText java tutorial

How to Create Roman List In pdf in java - iText java tutorial

Create ZapfDingbats List In pdf in java - iText java tutorial program

How to Create Ordered List In pdf in java using iText - iText java tutorial


Creating CHAPTER, SECTION, CHUNK, PARAGRAPH AND PHRASE in pdf in java - iText tutorial >

Create Chapter And Section In Pdf in java - iText java tutorial

Create Left, center and right aligned Paragraph In Pdf - iText java tutorial

Create Phrase In Pdf (with lead) Using Itext - iText java tutorial example



Set FONT NAME, SIZE, STYLE, COLOUR in pdf in java - iText tutorial >

How to Set the Font Name, Size, Style and Colour In Pdf using itext in java


Create ANCHOR (HYPERLINK), SUPERSCRIPT, SUBSCRIPT, UNDERLINE AND STRIKETHROUGH in pdf in java - iText tutorial >

UnderLine Text In Pdf in java - iText java tutorial example

Strikethrough Text In Pdf in java - iText java tutorial program


HTML to PDF in java - iText tutorial >

Convert Html To Pdf in java using iText - iText java tutorial


Alternates to itext in java - iText tutorial >

What are alternatives to iText library for creating Pdf in java?




eEdit
Must read for you :