Itext Table Example

Itext Table Example describes about creating pdf documents using Java and iText.

Creating PDF with java in enterprise applications is quite common these days.

iText is a free and open-source tool for manipulating and creating PDF files in Java.

It had been written by Paulo Soares, Bruno Lowagie and others. It allows developers looking to boost web applications with dynamic PDF content manipulation.

You can see the below example, which is demonstrating Itext Table Example

Required Libraries

You need to download

  1. JDK 6
  2. itext-5.1.3.jar in classpath

Itext Table Example

import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

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

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

      PdfPTable table =
new PdfPTable(3);
     
      PdfPCell c1 =
new PdfPCell(new Phrase("Header 1"));
      c1.setHorizontalAlignment
(Element.ALIGN_LEFT);
      table.addCell
(c1);

      c1 =
new PdfPCell(new Phrase("Header 2"));
      c1.setHorizontalAlignment
(Element.ALIGN_LEFT);
      table.addCell
(c1);

      c1 =
new PdfPCell(new Phrase("Header 3"));
      c1.setHorizontalAlignment
(Element.ALIGN_LEFT);
      table.addCell
(c1);
     
      table.addCell
("1.0");
      table.addCell
("1.1");
      table.addCell
("1.2");
      table.addCell
("2.1");
      table.addCell
("2.2");
      table.addCell
("2.3");
     
     
      document.add
(table);
      document.close
();
   
} catch (Exception e) {
     
e.printStackTrace();
   
}
  }
}
Output
Itext Table Example










2 Responses to "Itext Table Example"
  1. francis 2011-12-15 08:11:43.0
  1. Rose 2011-12-16 08:11:43.0

Your email address will not be published. Required fields are marked *