Tutorial: iText by Example

PdfPTable

Adding a PdfPTable at an absolute position:
You can add a PdfPTable with Document.add() as described in the previous chapter, but you can also choose to put a PdfPTable at an absolute position with the method writeSelectedRows. The mechanisms to add objects at absolute positions will be described in Part IV of the tutorial. All you need to know for now is that you have to pass the following parameters to the method:
  • rowStart - the first row to be written, zero index
  • rowEnd - the last row to be written + 1. If it is -1 all the rows to the end are written.
  • xPos - the x write coodinate of the upper left corner of the table
  • yPos - the y write coodinate of the upper left corner of the table (remark: the 0 is at the bottom of the page as explained in the FAQ)
  • canvas - the PdfContentByte where the rows will be written to (explained in Part IV of the tutorial)
Example: java com.lowagie.examples.objects.tables.pdfptable.WriteSelectedRows
Demonstrates the writeSelectedRows method: see WriteSelectedRows.pdf
Example: java com.lowagie.examples.objects.tables.pdfptable.Tables
Adds a table to an absolute position: see tables.pdf
As you can see in the example, there are some things you need to keep in mind when using writeSelectedRows. If you add a PdfPTable with document.add(), the width of the table is always calculated as a percentage of the available page width at the current pointer (see page 2 of the PDF in the example). But if you add a PdfPTable at an absolute position, iText can't know what you are up to with the rest of the page. What is the available space? That's something iText can't know, so you need to set a total width of the Table with the methods setTotalWidth(float totalWidth) or setTotalWidth(float[] columnWidth).
You are also responsible for checking it the PdfPTable fits on the page. On page 5, rows 100-150 don't fit on the page because we started writing the rows at y = 200 instead of y = 820. Rows 112-149 are written, but you can't see them because they are outside the visible area of your page.
As you can see in the example, you can use writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas) to split your table horizontally over different pages if you have too many rows to fit on one page. If your table is too wide, you can also split the table vertically between columns. You need the method writeSelectedRows(int colStart, int colEnd, int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas) to do this.
Example: java com.lowagie.examples.objects.tables.pdfptable.SplitTable
Demonstrates how to split a Table in two sections of columns: see SplitTable.pdf
Go to top of the page
Memory management for large tables:
You have to be very careful with large tables. When you add objects to a Document, these objects are written to the outputstream 'as soon as possible'. The objects that have been written are made eligible for destruction. However, when you construct a PdfPTable and keep adding new cells without adding the table to the document, the PdfPTable-object keeps on growing, taking more and more memory that isn't being released. For really large tables, this can become a serious problem.
The next example shows how to split a table into different sections. As the tables are glued to eachother (don't use setSpacingBefore and setSpacingAfter here), nobody will see the difference between 1 large table or several small tables.
Example: java com.lowagie.examples.objects.tables.pdfptable.FragmentTable 50
Break one large Table up into different smaller tables with the same header: see FragmentTable.pdf
Go to top of the page
Cell and Table Events:
Remember the class Chunk and how you could add your own functionality in a generic way with a PageEvent? You can do the same with a PdfPCell using a PdfPCellEvent. In the example, we want to strike a line OVER the contents from the lowerleft to the upperright corner. Using methods that are explained in Part IV of the tutorial, we can apply almost every layout that is possible within the PDF syntax. We just have to implement the method cellLayout. You can use the cell itself that is passed as a parameter and you get a Rectangle object that holds to absolute position of the cell on the page. The array of PdfContentByte arrays needs some further explanation:
Example: java com.lowagie.examples.objects.tables.pdfptable.CellEvents
Demonstrates what one can do with cell events: see CellEvents.pdf
External resources for this example: otsoe.jpg
If you want the same kind of control over a complete table, it gets a little bit more complicated. You need to implement the method tableLayout of the PdfPTableEvent interface.
Example: java com.lowagie.examples.objects.tables.pdfptable.TableEvents1
Demonstrates what one can do with table events: see TableEvents1.pdf
Example: java com.lowagie.examples.objects.tables.pdfptable.TableEvents2
Demonstrates what one can do with table events (cells with colspan > 1): see TableEvents2.pdf
In the next example cell and table events are combined to simulate cellspacing.
Example: java com.lowagie.examples.objects.tables.pdfptable.FloatingBoxes
Demonstrates how to simulate cellspacing as in HTML: see FloatingBoxes.pdf
Go to top of the page
Vertical Text:
Finally the answer to a question that pops up on the mailinglist from time to time: is it possible to put vertical text in a Cell. The answer is: yes, but you need to know how to use the class PdfTemplate (which is explained in the direct content of the tutorial). If you can't wait, just take a look at the following example.
Example: java com.lowagie.examples.objects.tables.pdfptable.VerticalTextInCells
Demonstrates how to add vertical text in a cell: see VerticalText.pdf
Go to top of the page



Amazon books:
amazon.co.uk-link