In our iText Tutorial series, today we will see how to create a simple table in a PDF document using iText. We will be using 5.0.6 version of iText for this tutorial and make sure that you have the JAR file set in your classpath appropriately. We will also discuss how to use ROWSPAN and COLSPAN in Itext library for the table.I would also strongly recommend going through our iText beginner tutorial in Java so that you can be comfortable with the basic objects used for creating a PDF.
There are some basic steps to be followed when you plan to construct a table in iText:
1) We begin our create table example by creating a Document and PdfWriter object. The Document object will create logical elements for us in the PDF and the PdfWriter will realize it by writing it to the output. We have discussed much about these objects in the beginner tutorial and we will not be covering them in this section.
Document iText_Create_Table = new Document();
PdfWriter.getInstance(iText_Create_Table, new FileOutputStream("CreateTableExample.pdf"));
2) To create a table in PDF document using iText, we will make use of PdfPTable object. The PdfPTable object is defined in com.itextpdf.text.pdf.PdfPTable, and it has a constructor that takes the number of columns in the table as an integer. We will create an object for PdfPTable to start with specifying the number of columns in our table as 3.
PdfPTable my_first_table = new PdfPTable(3);
3) When I looked into iText API reference, I found that PdfPTable has a method "addCell", that accepts a Cell object of type PdfPCell. PdfPCell is defined in com.itextpdf.text.pdf.PdfPCell and we will have to create an object for this class, set the Cell features and use addCell method to push it to PdfPTable object. This will start creating the table for us as we keep pushing objects, to build the table. Now, to populate the first Cell (0,0) refer to the code sample below
my_first_table.addCell("0,0 First Cell Data"); // Use AddCell to Add a string data in first Cell
4) Now, as the table has 3 columns, I would like to set a colspan of 2 for the remaining cells and push some data into it. We will use a Cell object for this, the code is provided below
PdfPCell table_cell; // Create a PDFPCell Object
table_cell=new PdfPCell(new Phrase("A Cell with Colspan of 2")); // Create a Phrase data for Colspan 2 First Row
table_cell.setColspan(2); //Define the colspan for Cell object
my_first_table.addCell(table_cell);//push the cell to the table
If you create the PDF at this point, the table would look like the one provided below
Itext Create Table - Table look after adding first ROW |
5) We will now push some more data into the table, this time with ROWSPAN of 2 and COLSPAN of 2. Refer to the code sample below, that accomplishes this task
/* Define a Cell Object with ROWSPAN of 2 and COLSPAN of 2 */ table_cell=new PdfPCell(new Phrase("A Cell with COLSPAN of 2 and ROWSPAN of 2")); table_cell.setColspan(2); //Define the COLSPAN for Cell object table_cell.setRowspan(2);//Define the ROWSPAN for Cell object my_first_table.addCell(table_cell);//push the cell to the table
6) We are left with adding two more CELL data. Row 2 -> column 3 and Row 3 -> Column 3. We will use the "addCell" method to add data into these cells. The cells are automatically filled based on the availability by iText.
/* Add data in left over cells*/ my_first_table.addCell("2,3 Row 2 Column 3"); my_first_table.addCell("3,3 Row 3 Column 3");
7) Finally, we use Document.Add to add the table object we created to the Document and Close the document to write the EOF information.
iText_Create_Table.add(my_first_table);
iText_Create_Table.close();
That completes our first iText table tutorial. We saw how to create a table with ROWSPAN and COLSPAN attributes. The complete Java Code is provided below, and a snapshot of the table we generated is also provided after the code
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class iTextTableTutorial {
public static void main(String[] args){
try {
Document iText_Create_Table = new Document();
PdfWriter.getInstance(iText_Create_Table, new FileOutputStream("CreateTableExample.pdf"));
iText_Create_Table.open();
PdfPTable my_first_table = new PdfPTable(3);
my_first_table.addCell("0,0 First Cell Data"); // Use AddCell to Add a string data in first Cell
PdfPCell table_cell; // Create a PDFPCell Object
table_cell=new PdfPCell(new Phrase("A Cell with Colspan of 2")); // Create a Phrase data for Colspan 2 First Row
table_cell.setColspan(2); //Define the colspan for Cell object
my_first_table.addCell(table_cell);//push the cell to the table
/* Define a Cell Object with ROWSPAN of 2 and COLSPAN of 2 */
table_cell=new PdfPCell(new Phrase("A Cell with COLSPAN of 2 and ROWSPAN of 2"));
table_cell.setColspan(2); //Define the colspan for Cell object
table_cell.setRowspan(2); //Define the ROWSPAN for Cell object
my_first_table.addCell(table_cell);//push the cell to the table
/* Add data in left over cells*/
my_first_table.addCell("2,3 Row 2 Column 3");
my_first_table.addCell("3,3 Row 3 Column 3");
iText_Create_Table.add(my_first_table);
iText_Create_Table.close();
}
catch (Exception i)
{
System.out.println(i);
}
}
}
The complete table created using iText for this example is shown below
iText Table Example - Completed Table |
No comments:
Post a Comment