Organizing content in tables
QR Code ISO/IEC18004 Creation In Java
Using Barcode printer for Java Control to generate, create QR Code 2d barcode image in Java applications.www.OnBarcode.com
QR-Code Reader In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.www.OnBarcode.com
Encoding UCC-128 In Java
Using Barcode generator for Java Control to generate, create EAN / UCC - 13 image in Java applications.www.OnBarcode.com
Making USS Code 39 In Java
Using Barcode creation for Java Control to generate, create Code 39 Extended image in Java applications.www.OnBarcode.com
iText s table functionality has evolved from a very low-level class in the early versions of iText to the twin classes Table and PdfTable in iText 0.30 (2000). These classes were useful, but they had some flaws. It was hard to fine-tune them due to design decisions made by the iText developers; that is, by me. Developers wanted to define how to split the table upon a page break, to control the way borders are drawn, and so on. The PdfPTable class, introduced by the codeveloper of iText, Paulo Soares, solved this problem. The Table and PdfTable classes were removed in iText 5 (2009). So were SimpleTable and SimpleCell, two other table classes discussed in the first edition of iText in Action. Only PdfPTable and PdfPCell remain. They will be discussed in this chapter.
Creating Barcode In Java
Using Barcode drawer for Java Control to generate, create Barcode image in Java applications.www.OnBarcode.com
PDF-417 2d Barcode Printer In Java
Using Barcode maker for Java Control to generate, create PDF 417 image in Java applications.www.OnBarcode.com
Your first PdfPTable
2D Printer In Java
Using Barcode maker for Java Control to generate, create Matrix Barcode image in Java applications.www.OnBarcode.com
Uniform Symbology Specification Codabar Maker In Java
Using Barcode creator for Java Control to generate, create Monarch image in Java applications.www.OnBarcode.com
Suppose that you need to create a simple table that looks like figure 4.1. The code to generate this kind of table is pretty simple.
QR Scanner In .NET Framework
Using Barcode scanner for .NET framework Control to read, scan read, scan image in VS .NET applications.www.OnBarcode.com
Print QR Code ISO/IEC18004 In None
Using Barcode creation for Font Control to generate, create QR Code JIS X 0510 image in Font applications.www.OnBarcode.com
Listing 4.1 MyFirstTable.java
QR Code Maker In None
Using Barcode creator for Software Control to generate, create QR Code ISO/IEC18004 image in Software applications.www.OnBarcode.com
Data Matrix Creator In VB.NET
Using Barcode creator for .NET Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications.www.OnBarcode.com
PdfPTable table = new PdfPTable(3); Creates table with 3 columns PdfPCell cell; cell = new PdfPCell(new Phrase("Cell with colspan 3")); Adds cell with cell.setColspan(3); colspan 3 table.addCell(cell); cell = new PdfPCell(new Phrase("Cell with rowspan 2")); Adds cell with cell.setRowspan(2); rowspan 2 table.addCell(cell); table.addCell("row 1; cell 1"); table.addCell("row 1; cell 2"); Adds remaining table.addCell("row 2; cell 1"); cells table.addCell("row 2; cell 2");
Creating Code 3/9 In Java
Using Barcode creator for Android Control to generate, create Code 39 Full ASCII image in Android applications.www.OnBarcode.com
Generate PDF417 In None
Using Barcode generator for Office Word Control to generate, create PDF-417 2d barcode image in Word applications.www.OnBarcode.com
When you create a PdfPTable, you always need to pass the number of columns to the constructor. Creating a table with zero columns results in a RuntimeException. You can add different objects to a PdfPTable object using the addCell() method.
QR Code ISO/IEC18004 Drawer In .NET
Using Barcode generator for ASP.NET Control to generate, create QR image in ASP.NET applications.www.OnBarcode.com
Painting GTIN - 13 In Objective-C
Using Barcode generator for iPhone Control to generate, create EAN / UCC - 13 image in iPhone applications.www.OnBarcode.com
AUTOMATIC CREATION OF ROWS
Code 39 Full ASCII Scanner In Visual Studio .NET
Using Barcode reader for .NET Control to read, scan read, scan image in .NET applications.www.OnBarcode.com
USS Code 39 Creation In Objective-C
Using Barcode generation for iPad Control to generate, create Code39 image in iPad applications.www.OnBarcode.com
There s a PdfPRow object in the com.itextpdf.text.pdf package, but you aren t supposed to address it directly. iText uses this class internally to store the cells that belong to the same row.
Recognizing Barcode In .NET Framework
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET applications.www.OnBarcode.com
Paint Barcode In VB.NET
Using Barcode creation for Visual Studio .NET Control to generate, create Barcode image in .NET applications.www.OnBarcode.com
Your first PdfPTable
In listing 4.1, the table has three columns B. After adding the first cell with column span 3 C, the first row is full. The next cell is added to a second row that is created automatically by iText. This cell has to span 2 rows D, so a third row is created, of which the first cell is reserved. Four more cells are then added E; the first pair completes the second row; the second pair completes the third row.
When you add a PdfPTable to a Document, only complete rows are added. If you have a table with three columns and your final row has only two cells, the row won t be added unless you use the method PdfPTable. completeRow().
You don t have to worry about creating rows; iText creates them for you. Just make sure you re adding the correct number of cells.
We ll talk about cells and cell properties such as text alignment, spacing, and borders in the next section. First, let s take a look at the properties of the table: the width of the table and its columns, the spacing before and after the table, and the alignment of the table. The default width of a table is 80 percent of the available width. Let s do the math for the table created by listing 4.1. The width of the page is 595 pt minus the margins, which are 36 pt. Eighty percent of this width is (595 (2 * 36)) * 0.80, which amounts to 418.4 pt.
Each PdfPTable keeps two values for the width:
widthPercentage A percentage of the available width totalWidth The absolute width expressed in user space units
When you add a PdfPTable to a Document, iText looks at one of these values, ignoring the other, depending on the value of lockedWidth. If this value is true, the table will have a fixed width, as defined by the totalWidth variable. By default, the value is false, and the exact width of the table will depend on the width available on the page or the width of the ColumnText object to which it s added. The default width of the columns is equal to the width of the table divided by the number of columns.
FAQ Is it possible to have the column width change dynamically based on the content of the cells PDF isn t HTML, and a PdfPTable is completely different from an HTML table rendered in a browser; iText can t calculate column widths based on the content of the columns. The result would depend on too many design decisions and wouldn t always correspond to what a developer expects. It s better to have the developer define the widths.
You could say that each column has a relative width equal to 1. You can change this by defining new relative widths, or by setting absolute widths for the columns.