com.lowagie.text
public class Table extends Rectangle implements LargeElement
Table
is a Rectangle
that contains Cell
s,
ordered in some kind of matrix.
Tables that span multiple pages are cut into different parts automatically.
If you want a table header to be repeated on every page, you may not forget to
mark the end of the header section by using the method endHeaders()
.
The matrix of a table is not necessarily an m x n-matrix. It can contain holes
or cells that are bigger than the unit. Believe me or not, but it took some serious
thinking to make this as user friendly as possible. I hope you will find the result
quite simple (I love simple solutions, especially for complex problems).
I didn't want it to be something as complex as the Java GridBagLayout
.
Example:
The result of this code is a table:// Remark: You MUST know the number of columns when constructing a Table. // The number of rows is not important. Table table = new Table(3); table.setBorderWidth(1); table.setBorderColor(new Color(0, 0, 255)); table.setPadding(5); table.setSpacing(5); Cell cell = new Cell("header"); cell.setHeader(true); cell.setColspan(3); table.addCell(cell); table.endHeaders(); cell = new Cell("example cell with colspan 1 and rowspan 2"); cell.setRowspan(2); cell.setBorderColor(new Color(255, 0, 0)); table.addCell(cell); table.addCell("1.1"); table.addCell("2.1"); table.addCell("1.2"); table.addCell("2.2"); table.addCell("cell test1"); cell = new Cell("big cell"); cell.setRowspan(2); cell.setColspan(2); table.addCell(cell); table.addCell("cell test2");
header | ||
---|---|---|
example cell with colspan 1 and rowspan 2 | 1.1 | 2.1 |
1.2 | 2.2 | |
cell test1 | big cell | |
cell test2 |
Field Summary | |
---|---|
int | alignment This is the horizontal alignment. |
protected boolean | autoFillEmptyCells
Boolean to automatically fill empty cells before a table is rendered
(takes CPU so may be set to false in case of certainty) |
float | cellpadding This is cellpadding. |
boolean | cellsFitPage If true cells may not be split over two pages. |
float | cellspacing This is cellspacing. |
int | columns This is the number of columns in the Table . |
protected boolean | complete
Indicates if the PdfPTable is complete once added to the document. |
protected boolean | convert2pdfptable if you want to generate tables the old way, set this value to false. |
Point | curPosition The current Position in the table. |
Cell | defaultCell This Empty Cell contains the DEFAULT layout of each Cell added with the method addCell(String content). |
int | lastHeaderRow This is the number of the last row of the table headers. |
boolean | locked Is the width a percentage (false) or an absolute width (true)? |
boolean | mTableInserted Boolean to track if a table was inserted (to avoid unnecessary computations afterwards) |
protected boolean | notAddedYet
Indicates if this is the first time the section was added. |
float | offset This is the offset of the table. |
ArrayList | rows This is the list of Row s. |
boolean | tableFitsPage If true this table may not be split over two pages. |
float | width This is the width of the table (in percent of the available space). |
float[] | widths This is an array containing the widths (in percentages) of every column. |
Constructor Summary | |
---|---|
Table(int columns)
Constructs a Table with a certain number of columns.
| |
Table(int columns, int rows)
Constructs a Table with a certain number of columns
and a certain number of Row s.
| |
Table(Table t)
Copy constructor (shallow copy). |
Method Summary | |
---|---|
void | addCell(Cell aCell, int row, int column)
Adds a Cell to the Table at a certain row and column.
|
void | addCell(Cell aCell, Point aLocation)
Adds a Cell to the Table at a certain location.
|
void | addCell(Cell cell)
Adds a Cell to the Table .
|
void | addCell(Phrase content)
Adds a Cell to the Table .
|
void | addCell(Phrase content, Point location)
Adds a Cell to the Table .
|
void | addCell(String content)
Adds a Cell to the Table .
|
void | addCell(String content, Point location)
Adds a Cell to the Table .
|
void | addColumns(int aColumns)
Gives you the possibility to add columns.
|
void | assumeTableDefaults(Cell aCell)
Sets the unset cell properties to be the table defaults.
|
void | complete()
Will fill empty cells with valid blank Cell s |
PdfPTable | createPdfPTable()
Create a PdfPTable based on this Table object. |
void | deleteAllRows()
Deletes all rows in this table.
(contributed by dperezcar@fcc.es) |
void | deleteColumn(int column)
Deletes a column in this table.
|
boolean | deleteLastRow()
Deletes the last row in this table.
|
boolean | deleteRow(int row)
Deletes a row.
|
int | endHeaders()
Marks the last row of the table headers.
|
void | fillEmptyMatrixCells()
adds new Cell 's to empty/null spaces. |
void | flushContent() |
int | getAlignment()
Gets the horizontal alignment.
|
ArrayList | getChunks()
Gets all the chunks in this element.
|
int | getColumns()
Gets the number of columns.
|
Cell | getDefaultCell()
Gets the default layout of the Table. |
Cell | getDefaultLayout()
Gets the default layout of the Table. |
Dimension | getDimension()
Gets the dimension of this table
|
Object | getElement(int row, int column)
returns the element at the position row, column
(Cast to Cell or Table)
|
int | getLastHeaderRow()
Gets the last number of the rows that contain headers.
|
float | getOffset()
Gets the offset of this table.
|
float | getPadding()
Gets the cellpadding.
|
float[] | getProportionalWidths()
Gets the proportional widths of the columns in this Table .
|
float | getSpacing()
Gets the cellspacing.
|
float | getWidth()
Gets the table width (a percentage).
|
float[] | getWidths(float left, float totalWidth)
Gets an array with the positions of the borders between every column.
|
void | insertTable(Table aTable)
To put a table within the existing table at the current position
generateTable will of course re-arrange the widths of the columns.
|
void | insertTable(Table aTable, int row, int column)
To put a table within the existing table at the given position
generateTable will of course re-arrange the widths of the columns.
|
void | insertTable(Table aTable, Point aLocation)
To put a table within the existing table at the given position
generateTable will of course re-arrange the widths of the columns.
|
boolean | isCellsFitPage()
Checks if the cells of this Table have to fit a page.
|
boolean | isComplete() |
boolean | isConvert2pdfptable()
Method to check if the Table should be converted to a PdfPTable or not. |
boolean | isLocked() |
boolean | isNestable() |
boolean | isNotAddedYet()
Indicates if this is the first time the section is added. |
boolean | isTableFitsPage()
Checks if this Table has to fit a page.
|
boolean | isValidLocation(Cell aCell, Point aLocation)
check if Cell 'fits' the table.
|
Iterator | iterator()
Gets an Iterator of all the Row s.
|
void | mergeInsertedTables()
Integrates all added tables and recalculates column widths. |
void | placeCell(ArrayList someRows, Cell aCell, Point aPosition)
Inserts a Cell in a cell-array and reserves cells defined by row-/colspan.
|
boolean | process(ElementListener listener)
Processes the element by adding it (or the different parts) to an
ElementListener .
|
void | setAlignment(int value)
Sets the horizontal alignment.
|
void | setAlignment(String alignment)
Sets the alignment of this paragraph.
|
void | setAutoFillEmptyCells(boolean aDoAutoFill)
Enables/disables automatic insertion of empty cells before table is rendered. (default = false)
As some people may want to create a table, fill only a couple of the cells and don't bother with
investigating which empty ones need to be added, this default behavior may be very welcome.
|
void | setCellsFitPage(boolean fitPage)
Allows you to control when a page break occurs.
|
void | setComplete(boolean complete) |
void | setConvert2pdfptable(boolean convert2pdfptable)
If set to true, iText will try to convert the Table to a PdfPTable. |
void | setCurrentLocationToNextValidPosition(Point aLocation)
Sets current col/row to valid(empty) pos after addCell/Table |
void | setDefaultCell(Cell value)
Sets the default layout of the Table to
the provided Cell |
void | setDefaultLayout(Cell value)
Sets the default layout of the Table to
the provided Cell |
void | setLastHeaderRow(int value)
Sets the horizontal alignment.
|
void | setLocked(boolean locked) |
void | setNotAddedYet(boolean notAddedYet)
Sets the indication if the section was already added to
the document. |
void | setOffset(float offset)
Sets the offset of this table.
|
void | setPadding(float value)
Sets the cellpadding.
|
void | setSpacing(float value)
Sets the cellspacing.
|
void | setTableFitsPage(boolean fitPage)
Allows you to control when a page break occurs.
|
void | setWidth(float width)
Sets the width of this table (in percentage of the available space).
|
void | setWidths(float[] widths)
Sets the widths of the different columns (percentages).
|
void | setWidths(int[] widths)
Sets the widths of the different columns (percentages).
|
int | size()
Gets the number of rows in this Table .
|
int | type()
Gets the type of the text element.
|
Table
.Since: iText 2.0.8
Since: iText 2.0.8
Row
s.Table
with a certain number of columns.
Parameters: columns The number of columns in the table
Throws: BadElementException if the creator was called with less than 1 column
Table
with a certain number of columns
and a certain number of Row
s.
Parameters: columns The number of columns in the table rows The number of rows
Throws: BadElementException if the creator was called with less than 1 column
Throws: BadElementException
Cell
to the Table
at a certain row and column.
Parameters: aCell The Cell
to add row The row where the Cell
will be added column The column where the Cell
will be added
Throws: BadElementException
Cell
to the Table
at a certain location.
Parameters: aCell The Cell
to add aLocation The location where the Cell
will be added
Throws: BadElementException
Cell
to the Table
.
Parameters: cell a Cell
Cell
to the Table
.
This is a shortcut for addCell(Cell cell)
.
The Phrase
will be converted to a Cell
.
Parameters: content a Phrase
Throws: BadElementException this should never happen
Cell
to the Table
.
This is a shortcut for addCell(Cell cell, Point location)
.
The Phrase
will be converted to a Cell
.
Parameters: content a Phrase
location a Point
Throws: BadElementException this should never happen
Cell
to the Table
.
This is a shortcut for addCell(Cell cell)
.
The String
will be converted to a Cell
.
Parameters: content a String
Throws: BadElementException this should never happen
Cell
to the Table
.
This is a shortcut for addCell(Cell cell, Point location)
.
The String
will be converted to a Cell
.
Parameters: content a String
location a Point
Throws: BadElementException this should never happen
Parameters: aColumns the number of columns to add
Parameters: aCell The cell to set to table defaults as necessary.
Cell
sReturns: a PdfPTable object
Throws: BadElementException
Parameters: column the number of the column that has to be deleted
Throws: BadElementException
Returns: boolean true
if the row was deleted; false
if not
Parameters: row the number of the row to delete
Returns: boolean true
if the row was deleted; false
if not
Returns: the number of the last row of the table headers
Cell
's to empty/null spaces.Since: iText 2.0.8
See Also: flushContent
Returns: a value
Returns: an ArrayList
Returns: a value
Returns: a cell with all the defaults
Since: 2.0.7
Deprecated: As of iText 2.0.7, replaced by getDefaultCell, scheduled for removal at 2.2.0
Gets the default layout of the Table.Returns: a cell with all the defaults
Returns: dimension
Parameters: row column
Returns: dimension
Since: 2.1.0 (was made private in 2.0.3)
Returns: a rownumber
Returns: the space between this table and the previous element.
Returns: a value
Table
.
Returns: the proportional widths of the columns in this Table
Returns: a value
Returns: the table width
This method translates the widths expressed in percentages into the x-coordinate of the borders of the columns on a real document.
Parameters: left this is the position of the first border at the left (cellpadding not included) totalWidth this is the space between the first border at the left and the last border at the right (cellpadding not included)
Returns: an array with border positions
Parameters: aTable the table you want to insert
Parameters: aTable The Table
to add row The row where the Cell
will be added column The column where the Cell
will be added
Parameters: aTable the table you want to insert aLocation a Point
Table
have to fit a page.
Returns: true if the cells may not be split
Since: iText 2.0.8
See Also: isComplete
Returns: false if the table should be handled the old fashioned way.
Returns: the locked
Since: iText 2.0.8
See Also: isNestable
Returns: true if the section wasn't added yet
Since: iText2.0.8
Table
has to fit a page.
Returns: true if the table may not be split
Cell
'fits' the table.
Parameters: aCell the cell that has to be checked aLocation the location where the cell has to be placed
Returns: true if the location was valid
Iterator
of all the Row
s.
Returns: an Iterator
Parameters: someRows some rows aCell the cell that has to be inserted aPosition the position where the cell has to be placed
ElementListener
.
Parameters: listener an ElementListener
Returns: true
if the element was processed successfully
Parameters: value the new value
Parameters: alignment the new alignment as a String
Parameters: aDoAutoFill enable/disable autofill
When a cell doesn't fit a page, it is split in two parts. If you want to avoid this, you should set the cellsFitPage value to true.
Parameters: fitPage enter true if you don't want to split cells
Since: iText 2.0.8
See Also: LargeElement
Parameters: convert2pdfptable true if you want iText to try to convert the Table to a PdfPTable
Parameters: aLocation a location in the Table
Parameters: value a cell with all the defaults
Since: 2.0.7
Deprecated: As of iText 2.0.7, replaced by setDefaultCell, scheduled for removal at 2.2.0
Sets the default layout of the Table to the provided CellParameters: value a cell with all the defaults
Parameters: value the new value
Parameters: locked the locked to set
Parameters: notAddedYet
Since: iText2.0.8
Parameters: offset the space between this table and the previous object.
Parameters: value the new value
Parameters: value the new value
When a table doesn't fit a page, it is split in two parts. If you want to avoid this, you should set the tableFitsPage value to true.
Parameters: fitPage enter true if you don't want to split cells
Parameters: width the width
You can give up relative values of borderwidths. The sum of these values will be considered 100%. The values will be recalculated as percentages of this sum.
example:
The widths will be: a width of 50% for the first column, 25% for the second and third column.float[] widths = {2, 1, 1}; table.setWidths(widths)
Parameters: widths an array with values
Throws: BadElementException
You can give up relative values of borderwidths. The sum of these values will be considered 100%. The values will be recalculated as percentages of this sum.
Parameters: widths an array with values
Throws: DocumentException
Table
.
Returns: the number of rows in this Table
Returns: a type