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
- JDK 6
- 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();
}
}
}
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

2 Responses to "Itext Table Example"