How to Create the Table In Pdf in java


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

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

We will create PdfPTable (com.itextpdf.text.pdf.PdfPTable) and then create cells ( com.itextpdf.text.pdf.PdfPCell) and then add those cells to table in java in pdf in itext.




Download jars required to execute program >

Or, you may download jars from here.


Program/Example to How to Create the Table 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;
/**
*
*  How to Create the Table In Pdf Example Using
*  iText library - core java tutorial
*
*/
public class CreatingTableInPdfExample {
   public static void main(String[] args) {
          try {
                 String pdfFilePath = "e:/Creating Table 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(3); // Create 3 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"));
                 // Add cells in table
                 table.addCell(cell1);
                 table.addCell(cell2);
                 table.addCell(cell3);
                 // 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:/Creating Table In Pdf Example.pdf
*/


PDF formed after executing above java program(How to Create the Table In Pdf in java) will look like this >


Summary -
So in this core java tutorial we learned How to Create the Table In Pdf in java using iText library.
We created PdfPTable (com.itextpdf.text.pdf.PdfPTable) and then created cells ( com.itextpdf.text.pdf.PdfPCell) and then added those cells to table in java in pdf in itext.




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>

iText library tutorial in java - How to Create and work with PDF files in java


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 >

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

How To Set SuperScript And SubScript In Pdf in java - iText java 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


Create new page, set page height and width in java - iText tutorial >

How To Create New Pages In Pdf Using Itext - iText java tutorial example

How to Find Pdf Page Height And Width in java - iText java tutorial program



HEADER and FOOTER in pdf in java - iText tutorial >

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



BAR AND PIE CHARTS in pdf in java - iText tutorial >

How to create Bar Chart In Pdf in java - iText java tutorial

Create Pie Chart In Pdf in java using itext - iText java tutorial



MODIFY/ EDIT pdf in java - iText tutorial >

How to Modify - Add Text To Existing PDF in java - iText java tutorial example

How To Read And Add Image To Existing Pdf in java Example - iText java tutorial



Solve common Exceptions in itext in java - iText tutorial >

How to solve the Document Has No Pages IOException in iText in java


Alternates to itext in java - iText tutorial >

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




eEdit
Must read for you :