com.lowagie.text

Class Table

public class Table extends Rectangle

A Table is a Rectangle that contains Cells, 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 userfriendly as possible. I hope you wil 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:

 // 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");
 
The result of this code is a table:
header
example cell with colspan 1 and rowspan 2 1.1 2.1
1.2 2.2
cell test1 big cell
cell test2

See Also: Rectangle Element Row Cell

Field Summary
intalignment
This is the horizontal alignment.
protected booleanautoFillEmptyCells
Boolean to automatically fill empty cells before a table is rendered (takes CPU so may be set to false in case of certainty)
floatcellpadding
This is cellpadding.
booleancellsFitPage
If true cells may not be split over two pages.
floatcellspacing
This is cellspacing.
intcolumns
This is the number of columns in the Table.
protected booleanconvert2pdfptable
if you want to generate tables the old way, set this value to false.
PointcurPosition
The current Position in the table.
CelldefaultLayout
This Empty Cell contains the DEFAULT layout of each Cell added with the method addCell(String content).
intlastHeaderRow
This is the number of the last row of the table headers.
booleanlocked
Is the width a percentage (false) or an absolute width (true)?
booleanmTableInserted
Boolean to track if a table was inserted (to avoid unnecessary computations afterwards)
floatoffset
This is the offset of the table.
ArrayListrows
This is the list of Rows.
booleantableFitsPage
If true this table may not be split over two pages.
floatwidth
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 Rows.
Table(Table t)
Copy constructor (shallow copy).
Table(Properties attributes)
Returns a Table that has been constructed taking in account the value of some attributes.
Method Summary
StringabsWidth()
Gets the table width (in pixels).
voidaddCell(Cell aCell, int row, int column)
Adds a Cell to the Table at a certain row and column.
voidaddCell(Cell aCell, Point aLocation)
Adds a Cell to the Table at a certain location.
voidaddCell(Cell cell)
Adds a Cell to the Table.
voidaddCell(Phrase content)
Adds a Cell to the Table.
voidaddCell(Phrase content, Point location)
Adds a Cell to the Table.
voidaddCell(String content)
Adds a Cell to the Table.
voidaddCell(String content, Point location)
Adds a Cell to the Table.
voidaddColumns(int aColumns)
Gives you the posibility to add columns.
intalignment()
Gets the horizontal alignment.
voidassumeTableDefaults(Cell aCell)
Sets the unset cell properties to be the table defaults.
floatcellpadding()
Gets the cellpadding.
floatcellspacing()
Gets the cellspacing.
intcolumns()
Gets the number of columns.
voidcomplete()
Will fill empty cells with valid blank Cells
PdfPTablecreatePdfPTable()
Create a PdfPTable based on this Table object.
voiddeleteAllRows()
Deletes all rows in this table. (contributed by dperezcar@fcc.es)
voiddeleteColumn(int column)
Deletes a column in this table.
booleandeleteLastRow()
Deletes the last row in this table.
booleandeleteRow(int row)
Deletes a row.
intendHeaders()
Marks the last row of the table headers.
voidfillEmptyMatrixCells()
adds newCell's to empty/null spaces.
intgetAlignment()
Gets the horizontal alignment.
ArrayListgetChunks()
Gets all the chunks in this element.
intgetColumns()
Gets the number of columns.
CellgetDefaultLayout()
Gets the default layout of the Table.
DimensiongetDimension()
Gets the dimension of this table
ObjectgetElement(int row, int column)
returns the element at the position row, column (Cast to Cell or Table)
intgetLastHeaderRow()
Gets the last number of the rows that contain headers.
floatgetOffset()
Gets the offset of this table.
floatgetPadding()
Gets the cellpadding.
float[]getProportionalWidths()
Gets the proportional widths of the columns in this Table.
floatgetSpacing()
Gets the cellspacing.
floatgetWidth()
Gets the table width (a percentage).
float[]getWidths(float left, float totalWidth)
Gets an array with the positions of the borders between every column.
voidinsertTable(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.
voidinsertTable(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.
voidinsertTable(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.
booleanisCellsFitPage()
Checks if the cells of this Table have to fit a page.
booleanisConvert2pdfptable()
Method to check if the Table should be converted to a PdfPTable or not.
booleanisLocked()
booleanisTableFitsPage()
Checks if this Table has to fit a page.
booleanisValidLocation(Cell aCell, Point aLocation)
check if Cell 'fits' the table.
Iteratoriterator()
Gets an Iterator of all the Rows.
intlastHeaderRow()
Gets the last number of the rows that contain headers.
voidmergeInsertedTables()
Integrates all added tables and recalculates column widths.
voidplaceCell(ArrayList someRows, Cell aCell, Point aPosition)
Inserts a Cell in a cell-array and reserves cells defined by row-/colspan.
booleanprocess(ElementListener listener)
Processes the element by adding it (or the different parts) to an ElementListener.
voidsetAbsWidth(String width)
Sets the width of this table (in percentage of the available space).
voidsetAlignment(int value)
Sets the horizontal alignment.
voidsetAlignment(String alignment)
Sets the alignment of this paragraph.
voidsetAutoFillEmptyCells(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 behaviour may be very welcome.
voidsetCellsFitPage(boolean fitPage)
Allows you to control when a page break occurs.
voidsetConvert2pdfptable(boolean convert2pdfptable)
If set to true, iText will try to convert the Table to a PdfPTable.
voidsetCurrentLocationToNextValidPosition(Point aLocation)
Sets current col/row to valid(empty) pos after addCell/Table
voidsetDefaultCellBackgroundColor(Color color)
Changes the backgroundcolor in the default layout of the Cells added with method addCell(String content).
voidsetDefaultCellBorder(int value)
Changes the border in the default layout of the Cells added with method addCell(String content).
voidsetDefaultCellBorderColor(Color color)
Changes the bordercolor in the default layout of the Cells added with method addCell(String content).
voidsetDefaultCellBorderWidth(float value)
Changes the width of the borders in the default layout of the Cells added with method addCell(String content).
voidsetDefaultCellGrayFill(float value)
Changes the grayfill in the default layout of the Cells added with method addCell(String content).
voidsetDefaultColspan(int value)
Changes the colspan in the default layout of the Cells added with method addCell(String content).
voidsetDefaultHorizontalAlignment(int value)
Changes the horizontalAlignment in the default layout of the Cells added with method addCell(String content).
voidsetDefaultLayout(Cell value)
Sets the default layout of the Table to the provided Cell
voidsetDefaultRowspan(int value)
Changes the rowspan in the default layout of the Cells added with method addCell(String content).
voidsetDefaultVerticalAlignment(int value)
Changes the verticalAlignment in the default layout of the Cells added with method addCell(String content).
voidsetLastHeaderRow(int value)
Sets the horizontal alignment.
voidsetLocked(boolean locked)
voidsetOffset(float offset)
Sets the offset of this table.
voidsetPadding(float value)
Sets the cellpadding.
voidsetSpaceBetweenCells(float value)
Sets the cellspacing.
voidsetSpaceInsideCell(float value)
Sets the cellpadding.
voidsetSpacing(float value)
Sets the cellspacing.
voidsetTableFitsPage(boolean fitPage)
Allows you to control when a page break occurs.
voidsetWidth(float width)
Sets the width of this table (in percentage of the available space).
voidsetWidths(float[] widths)
Sets the widths of the different columns (percentages).
voidsetWidths(int[] widths)
Sets the widths of the different columns (percentages).
intsize()
Gets the number of rows in this Table.
inttype()
Gets the type of the text element.
floatwidthPercentage()
Gets the table width (a percentage).

Field Detail

alignment

private int alignment
This is the horizontal alignment.

autoFillEmptyCells

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)

cellpadding

private float cellpadding
This is cellpadding.

cellsFitPage

boolean cellsFitPage
If true cells may not be split over two pages.

cellspacing

private float cellspacing
This is cellspacing.

columns

private int columns
This is the number of columns in the Table.

convert2pdfptable

protected boolean convert2pdfptable
if you want to generate tables the old way, set this value to false.

curPosition

private Point curPosition
The current Position in the table.

defaultLayout

private Cell defaultLayout
This Empty Cell contains the DEFAULT layout of each Cell added with the method addCell(String content).

lastHeaderRow

private int lastHeaderRow
This is the number of the last row of the table headers.

locked

private boolean locked
Is the width a percentage (false) or an absolute width (true)?

mTableInserted

private boolean mTableInserted
Boolean to track if a table was inserted (to avoid unnecessary computations afterwards)

offset

float offset
This is the offset of the table.

rows

private ArrayList rows
This is the list of Rows.

tableFitsPage

boolean tableFitsPage
If true this table may not be split over two pages.

width

private float width
This is the width of the table (in percent of the available space).

widths

private float[] widths
This is an array containing the widths (in percentages) of every column.

Constructor Detail

Table

public Table(int columns)
Constructs a 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

public Table(int columns, int rows)
Constructs a Table with a certain number of columns and a certain number of Rows.

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

Table

public Table(Table t)
Copy constructor (shallow copy).

Throws: BadElementException

Table

public Table(Properties attributes)

Deprecated:

Returns a Table that has been constructed taking in account the value of some attributes.

Parameters: attributes Some attributes

Throws: BadElementException

Method Detail

absWidth

public String absWidth()

Deprecated: use getWidth

Gets the table width (in pixels).

Returns: the table width

addCell

public void addCell(Cell aCell, int row, int column)
Adds a 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

addCell

public void addCell(Cell aCell, Point aLocation)
Adds a 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

addCell

public void addCell(Cell cell)
Adds a Cell to the Table.

Parameters: cell a Cell

addCell

public void addCell(Phrase content)
Adds a 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

addCell

public void addCell(Phrase content, Point location)
Adds a 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

addCell

public void addCell(String content)
Adds a 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

addCell

public void addCell(String content, Point location)
Adds a 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

addColumns

public void addColumns(int aColumns)
Gives you the posibility to add columns.

Parameters: aColumns the number of columns to add

alignment

public int alignment()

Deprecated: Use getAlignment instead

Gets the horizontal alignment.

Returns: a value

assumeTableDefaults

private void assumeTableDefaults(Cell aCell)
Sets the unset cell properties to be the table defaults.

Parameters: aCell The cell to set to table defaults as necessary.

cellpadding

public float cellpadding()

Deprecated: Use getPadding instead

Gets the cellpadding.

Returns: a value

cellspacing

public float cellspacing()

Deprecated: Use getSpacing instead

Gets the cellspacing.

Returns: a value

columns

public int columns()

Deprecated: Use getColumns instead

Gets the number of columns.

Returns: a value

complete

public void complete()
Will fill empty cells with valid blank Cells

createPdfPTable

public PdfPTable createPdfPTable()
Create a PdfPTable based on this Table object.

Returns: a PdfPTable object

Throws: BadElementException

deleteAllRows

public void deleteAllRows()
Deletes all rows in this table. (contributed by dperezcar@fcc.es)

deleteColumn

public void deleteColumn(int column)
Deletes a column in this table.

Parameters: column the number of the column that has to be deleted

Throws: BadElementException

deleteLastRow

public boolean deleteLastRow()
Deletes the last row in this table.

Returns: boolean true if the row was deleted; false if not

deleteRow

public boolean deleteRow(int row)
Deletes a row.

Parameters: row the number of the row to delete

Returns: boolean true if the row was deleted; false if not

endHeaders

public int endHeaders()
Marks the last row of the table headers.

Returns: the number of the last row of the table headers

fillEmptyMatrixCells

private void fillEmptyMatrixCells()
adds newCell's to empty/null spaces.

getAlignment

public int getAlignment()
Gets the horizontal alignment.

Returns: a value

getChunks

public ArrayList getChunks()
Gets all the chunks in this element.

Returns: an ArrayList

getColumns

public int getColumns()
Gets the number of columns.

Returns: a value

getDefaultLayout

public Cell getDefaultLayout()
Gets the default layout of the Table.

Returns: a cell with all the defaults

getDimension

public Dimension getDimension()
Gets the dimension of this table

Returns: dimension

getElement

private Object getElement(int row, int column)
returns the element at the position row, column (Cast to Cell or Table)

Parameters: row column

Returns: dimension

getLastHeaderRow

public int getLastHeaderRow()
Gets the last number of the rows that contain headers.

Returns: a rownumber

getOffset

public float getOffset()
Gets the offset of this table.

Returns: the space between this table and the previous element.

getPadding

public float getPadding()
Gets the cellpadding.

Returns: a value

getProportionalWidths

public float[] getProportionalWidths()
Gets the proportional widths of the columns in this Table.

Returns: the proportional widths of the columns in this Table

getSpacing

public float getSpacing()
Gets the cellspacing.

Returns: a value

getWidth

public float getWidth()
Gets the table width (a percentage).

Returns: the table width

getWidths

public float[] getWidths(float left, float totalWidth)
Gets an array with the positions of the borders between every column.

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 borderpositions

insertTable

public 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.

Parameters: aTable the table you want to insert

insertTable

public 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.

Parameters: aTable The Table to add row The row where the Cell will be added column The column where the Cell will be added

insertTable

public 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.

Parameters: aTable the table you want to insert aLocation a Point

isCellsFitPage

public boolean isCellsFitPage()
Checks if the cells of this Table have to fit a page.

Returns: true if the cells may not be split

isConvert2pdfptable

public boolean isConvert2pdfptable()
Method to check if the Table should be converted to a PdfPTable or not.

Returns: false if the table should be handled the oldfashioned way.

isLocked

public boolean isLocked()

Returns: the locked

isTableFitsPage

public boolean isTableFitsPage()
Checks if this Table has to fit a page.

Returns: true if the table may not be split

isValidLocation

private boolean isValidLocation(Cell aCell, Point aLocation)
check if 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

public Iterator iterator()
Gets an Iterator of all the Rows.

Returns: an Iterator

lastHeaderRow

public int lastHeaderRow()

Deprecated: Use getLastHeaderRow instead

Gets the last number of the rows that contain headers.

Returns: a rownumber

mergeInsertedTables

private void mergeInsertedTables()
Integrates all added tables and recalculates column widths.

placeCell

private void placeCell(ArrayList someRows, Cell aCell, Point aPosition)
Inserts a Cell in a cell-array and reserves cells defined by row-/colspan.

Parameters: someRows some rows aCell the cell that has to be inserted aPosition the position where the cell has to be placed

process

public boolean process(ElementListener listener)
Processes the element by adding it (or the different parts) to an ElementListener.

Parameters: listener an ElementListener

Returns: true if the element was processed successfully

setAbsWidth

public void setAbsWidth(String width)

Deprecated: setTotalWidth(int width)

Sets the width of this table (in percentage of the available space).

Parameters: width the width

setAlignment

public void setAlignment(int value)
Sets the horizontal alignment.

Parameters: value the new value

setAlignment

public void setAlignment(String alignment)
Sets the alignment of this paragraph.

Parameters: alignment the new alignment as a String

setAutoFillEmptyCells

public 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 behaviour may be very welcome. Disabling is recommended to increase speed. (empty cells should be added through extra code then)

Parameters: aDoAutoFill enable/disable autofill

setCellsFitPage

public void setCellsFitPage(boolean fitPage)
Allows you to control when a page break occurs.

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

setConvert2pdfptable

public void setConvert2pdfptable(boolean convert2pdfptable)
If set to true, iText will try to convert the Table to a PdfPTable.

Parameters: convert2pdfptable true if you want iText to try to convert the Table to a PdfPTable

setCurrentLocationToNextValidPosition

private void setCurrentLocationToNextValidPosition(Point aLocation)
Sets current col/row to valid(empty) pos after addCell/Table

Parameters: aLocation a location in the Table

setDefaultCellBackgroundColor

public void setDefaultCellBackgroundColor(Color color)

Deprecated: use getDefaultCell.setBackgroundColor

Changes the backgroundcolor in the default layout of the Cells added with method addCell(String content).

Parameters: color the new color

setDefaultCellBorder

public void setDefaultCellBorder(int value)

Deprecated: use getDefaultCell.setBorder

Changes the border in the default layout of the Cells added with method addCell(String content).

Parameters: value the new border value

setDefaultCellBorderColor

public void setDefaultCellBorderColor(Color color)

Deprecated: getDefaultCell.setBorderColor

Changes the bordercolor in the default layout of the Cells added with method addCell(String content).

Parameters: color the new color

setDefaultCellBorderWidth

public void setDefaultCellBorderWidth(float value)

Deprecated: use getDefaultCell.setBorderWidth

Changes the width of the borders in the default layout of the Cells added with method addCell(String content).

Parameters: value the new width

setDefaultCellGrayFill

public void setDefaultCellGrayFill(float value)

Deprecated: use getDefaultCell.setGrayFill

Changes the grayfill in the default layout of the Cells added with method addCell(String content).

Parameters: value the new value

setDefaultColspan

public void setDefaultColspan(int value)

Deprecated: use getDefaultCell.setColspan

Changes the colspan in the default layout of the Cells added with method addCell(String content).

Parameters: value the new colspan value

setDefaultHorizontalAlignment

public void setDefaultHorizontalAlignment(int value)

Deprecated: use getDefaultCell.setHorizontalAlignment

Changes the horizontalAlignment in the default layout of the Cells added with method addCell(String content).

Parameters: value the new alignment value

setDefaultLayout

public void setDefaultLayout(Cell value)
Sets the default layout of the Table to the provided Cell

Parameters: value a cell with all the defaults

setDefaultRowspan

public void setDefaultRowspan(int value)

Deprecated: use getDefaultCell.setRowspan

Changes the rowspan in the default layout of the Cells added with method addCell(String content).

Parameters: value the new rowspan value

setDefaultVerticalAlignment

public void setDefaultVerticalAlignment(int value)

Deprecated: use getDefaultCell.setVerticalAlignment

Changes the verticalAlignment in the default layout of the Cells added with method addCell(String content).

Parameters: value the new alignment value

setLastHeaderRow

public void setLastHeaderRow(int value)
Sets the horizontal alignment.

Parameters: value the new value

setLocked

public void setLocked(boolean locked)

Parameters: locked the locked to set

setOffset

public void setOffset(float offset)
Sets the offset of this table. Normally a newline is added before you add a Table object. This newline uses the current leading. If you want to control the space between the table and the previous element yourself, you have to set the offset of this table.

Parameters: offset the space between this table and the previous object.

setPadding

public void setPadding(float value)
Sets the cellpadding.

Parameters: value the new value

setSpaceBetweenCells

public void setSpaceBetweenCells(float value)

Deprecated: use setSpacing

Sets the cellspacing.

Parameters: value the new value

setSpaceInsideCell

public void setSpaceInsideCell(float value)

Deprecated: use setPadding

Sets the cellpadding.

Parameters: value the new value

setSpacing

public void setSpacing(float value)
Sets the cellspacing.

Parameters: value the new value

setTableFitsPage

public void setTableFitsPage(boolean fitPage)
Allows you to control when a page break occurs.

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

setWidth

public void setWidth(float width)
Sets the width of this table (in percentage of the available space).

Parameters: width the width

setWidths

public void setWidths(float[] widths)
Sets the widths of the different columns (percentages).

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:

 float[] widths = {2, 1, 1};
 table.setWidths(widths)
 
The widths will be: a width of 50% for the first column, 25% for the second and third column.

Parameters: widths an array with values

Throws: BadElementException

setWidths

public void setWidths(int[] widths)
Sets the widths of the different columns (percentages).

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

size

public int size()
Gets the number of rows in this Table.

Returns: the number of rows in this Table

type

public int type()
Gets the type of the text element.

Returns: a type

widthPercentage

public float widthPercentage()

Deprecated: Use getWidth instead

Gets the table width (a percentage).

Returns: the table width