How to Create the Table With Multiple Rows In Pdf in java



In this core java tutorial we will learn How to Create the Table With Multiple Rows In Pdf in java - using iText library - core java tutorial in Java with program and examples.


We will create PdfPTable (com.itextpdf.text.pdf.PdfPTable) with 2 columns (new PdfPTable(2)) and then create 6 cells ( com.itextpdf.text.pdf.PdfPCell) and then add all those cells to table and 3 rows will be created in java in pdf in itext in java.


We will generate PDF files in java using iText library. PDF files can be generated dynamically in java using iText library.

Also read :

Program/Example How to Create the Table With Multiple Rows In Pdf in java - Using iText library - core java tutorial


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;
/**
*
*  Creating the Table With Multiple Rows In Pdf Example
*  Using iText library - core java tutorial
*
*/
public class CreateTableWithMultipleRowsInPdfExample {
   public static void main(String[] args) {
          try {
                 String pdfFilePath = "e:/Create Table With Multiple Rows In Pdf Example.pdf";
                 OutputStream fos = new FileOutputStream(new File(pdfFilePath));
                 Document document = new Document();
                 PdfWriter.getInstance(document, fos);
                 document.open();
                 /**
                 * Now, we will create table in PDF file
                 */
                 PdfPTable table = new PdfPTable(2); // Create 2 columns in table.
                 // Create cells
                 PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
                 PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
                 PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
                 PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 4"));
                 PdfPCell cell5 = new PdfPCell(new Paragraph("Cell 5"));
                 PdfPCell cell6 = new PdfPCell(new Paragraph("Cell 6"));
                 // Add cells in table
                 table.addCell(cell1);
                 table.addCell(cell2);
                 table.addCell(cell3);
                 table.addCell(cell4);
                 table.addCell(cell5);
                 table.addCell(cell6);
                 // Add table in document
                 document.add(table);
                 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 Table With Multiple Rows In Pdf Example.pdf
*/

PDF formed after executing above java program will look like this >

Summary -
So in this core java tutorial we learned How to Create the Table With Multiple Rows In Pdf in java - using iText library - core java tutorial in Java with program and examples.
We created PdfPTable (com.itextpdf.text.pdf.PdfPTable) with 2 columns (new PdfPTable(2)) and then created 6 cells ( com.itextpdf.text.pdf.PdfPCell) and then added all those cells to table and 3 rows were created 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>

eEdit
Must read for you :