Changing the properties of a cell Part 2 (iText 5)

PdfPCell in composite mode

Text mode is meant for Chunk and Phrase objects. As soon as you need Paragraphs, Lists, or Images, you have to work in composite mode. There’s a huge difference between

tmp17C-174_thumb_thumb[1]

In the first code line, the Paragraph is treated in text mode: Paragraph-specific properties, such as the leading and the alignment, are ignored. Instead, the corresponding properties of the PdfPCell are used.

In the last two lines, you switch to composite mode by using addElement(). All the content that was previously inside the cell in text mode is discarded. Now the leading, alignment, and indentation set for the cell are ignored in favor of the properties of the elements that are added. This is exactly the same mechanism we discussed in the previous topic when we talked about the ColumnText object..

MOVIE LIST

You can now create a table of movie information and introduce Paragraph, List, and Image objects. See figure 4.8.


Cells in composite mode

Figure 4.8 Cells in composite mode

Each movie in the next listing takes only two cells: one with the movie poster O, another one with information about the movie ©.

Listing 4.14 MovieCompositeMode.java

Listing 4.14 MovieCompositeMode.java

Cell 2 consists of Paragraph and List objects with different alignments, leading, spacing, and indentation values ©. Because you’re using the addElement() method, you’re working in composite mode, and all the properties that are set for these different Elements are preserved. For Images, you can specify whether or not they have to be scaled.

ADDING IMAGES TO A TABLE

Figure 4.9 shows four movie posters added in four different ways. Listing 4.15 shows that the posters of the first two X-Men movies (directed by Bryan Singer) were added using a special PdfPCell constructor. The poster of the final part in the X-Men trilogy (directed by Brett Ratner) was added straight to the table with addCell(). A fourth poster was added to a cell with addElement(). (FYI: Bryan Singer stepped down as director of X-Men 3 in favor of Superman Returns’; he has regretted his mistake ever since.)

Cells and images

Figure 4.9 Cells and images

Listing 4.15 XMen.java

Listing 4.15 XMen.java

When you create a PdfPCell with an Image as a parameter, the default padding is 0 pt instead of 2 pt. With an extra parameter of type boolean, you can ask iText to scale the image so that it fits the width of the cell ©. By default, the value of this boolean is false and the image isn’t scaled O. This is a risk; if the image doesn’t fit within the borders of the cell, it will exceed them and overlap other cells.

Adding an Image with addCell() will scale it, but the properties of the default cell will be used ©: the third poster in image 4.9 has a padding of 2 pt, and it’s bottom-aligned.

Finally, you can add an image as an element ©. The Image is scaled so that it fills 100 percent of the cell width, unless you change the width percentage with the setWidth-Percentage() method.

Another special object that can be added to a cell is PdfPTable: tables can be nested! NESTED TABLES

There was a time when rowspan wasn’t supported for PdfPCells. The only way to work around this was to use nested tables. Cells 1.1 and 1.2 in figure 4.10 are part of a nested table. So are cells 12.1 and 12.2. Because of this, cells 13, 14, and 15 look as if they have their rowspan set to 2.

Nested tables

Figure 4.10 Nested tables

Looking at next listing, you’ll immediately see the difference between the nested table in cell 1 and the nested table in cell 12.

Listing 4.16 NestedTable.java

Listing 4.16 NestedTable.java

Just like with the Image object, the padding is 2 pt when the PdfPTable is added with addCell() directly. The padding is 0 pt when you wrap the table in a PdfPCell first. COMPLEX TABLE LAYOUTS

You can use nested tables to create layouts that are tabular, but that don’t fit in a traditional grid. Figure 4.11 is an example of such a layout.

Nesting tables for complex layouts

Figure 4.11 Nesting tables for complex layouts

This layout is created using the next listing, which is an example of deep nesting. A table is nested inside a nested table.

Listing 4.17 NestedTables.java

Listing 4.17 NestedTables.java

The table created with the fullTitle() method was added with the addElement() method. The effect is different from adding a PdfPTable as a parameter of the addCell() method or the PdfPCell constructor. With addElement(), the table is added to the ColumnText object of the cell, and you can add other elements to the same cell.

You’ve now worked with some small, almost academic, table examples to demonstrate the properties of tables and cells, but once you start working with real-world examples, tables can get really large. In the next section, we’ll discuss tips and tricks that are important as soon as a table spans multiple pages.

Next post:

Previous post: