|
Geek-Tutorials.com
|
|
Geek Tutorials Home > Java PDF & Reporting - iText Tutorials > Constructing PDF tables
You need iText library to do this tutorial. Please read Setup iText for project & Hello World for a tutorial on download and add iText library to your eclipse project.
Following code snippet show how to create a very simple PDF table with 2 columns and 3 rows.
package com.geek.tutorial.itext.table;
import java.io.FileOutputStream;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
public class SimplePDFTable {
public SimplePDFTable() throws Exception{
Document document = new Document();
PdfWriter.getInstance(document,
new FileOutputStream("SimplePDFTable.pdf"));
document.open();
PdfPTable table = new PdfPTable(2); // Code 1
// Code 2
table.addCell("1");
table.addCell("2");
// Code 3
table.addCell("3");
table.addCell("4");
// Code 4
table.addCell("5");
table.addCell("6");
// Code 5
document.add(table);
document.close();
}
public static void main(String[] args) {
try{
SimplePDFTable pdfTable = new SimplePDFTable();
}catch(Exception e){
System.out.println(e);
}
}
}
Code 1. Create com.lowagie.text.pdf.PdfPTable table and initialize with 2 columns.
Code 2. Insert 2 columns into first row.
Code 3. Insert 2 columns into second row.
Code 4. Insert 2 columns into third row.
Code 5. Add the table to the document and close document.
Run the example above, you will get the following PDF.
You can use PdfPCell to control column span. Following code use PdfPCell to create a table cell and set it to span accross 2 column:
package com.geek.tutorial.itext.table;
import java.io.FileOutputStream;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
public class SimplePDFTableColspan {
public SimplePDFTableColspan() throws Exception{
Document document = new Document();
PdfWriter.getInstance(document,
new FileOutputStream("SimplePDFTableColspan.pdf"));
document.open();
PdfPTable table = new PdfPTable(2);
PdfPCell cell =
new PdfPCell(new Paragraph("column span 2"));
cell.setColspan(2);
table.addCell(cell);
table.addCell("1");
table.addCell("2");
table.addCell("3");
table.addCell("4");
table.addCell("5");
table.addCell("6");
document.add(table);
document.close();
}
public static void main(String[] args) {
try{
SimplePDFTableColspan pdfTable
= new SimplePDFTableColspan();
}catch(Exception e){
System.out.println(e);
}
}
}
Run the example above, you will get the following PDF.
Following code modified from first example SimplePDFTable to show how to change table alignment, table width and column width. New code highlight in bold.
package com.geek.tutorial.itext.table;
import java.io.FileOutputStream;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Element; // import Element class
public class SimplePDFTableAlignAndWidth {
public SimplePDFTableAlignAndWidth() throws Exception{
Document document = new Document();
PdfWriter.getInstance(document,
new FileOutputStream("SimplePDFTableAlignAndWidth.pdf"));
document.open();
float[] colsWidth = {1f, 2f}; // Code 1
PdfPTable table = new PdfPTable(colsWidth);
table.setWidthPercentage(90); // Code 2
table.setHorizontalAlignment(Element.ALIGN_LEFT);//Code 3
table.addCell("1");
table.addCell("2");
table.addCell("3");
table.addCell("4");
table.addCell("5");
table.addCell("6");
document.add(table);
document.close();
}
public static void main(String[] args) {
try{
SimplePDFTableAlignAndWidth pdfTable
= new SimplePDFTableAlignAndWidth();
}catch(Exception e){
System.out.println(e);
}
}
}
Code 1. An array with 2 float values was used to defined a table with 2 columns. The floats in the array define internal table relative widths
Code 2. Set the table width in width percentage of page.
Code 3. Set the horizontal alignment of table. Make sure you import com.lowagie.text.Element class.
Run the example above, you will get the following PDF.
Your donation will be use for this project's site maintainance and further development of the content. Your support can help us provide higher quality of free tutorials and services to everyone in future
. Download latest source code for this project.