Class Table
- java.lang.Object
-
- com.aowagie.text.Rectangle
-
- com.aowagie.text.Table
-
- All Implemented Interfaces:
Element
,LargeElement
public class Table extends Rectangle implements LargeElement
ATable
is aRectangle
that containsCell
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");
<TABLE ALIGN="Center" BORDER="1" BORDERCOLOR="#0000ff" CELLPADDING="5" CELLSPACING="5"> <TR ALIGN="Left" VALIGN="Left"> <TH ALIGN="Left" COLSPAN="3" VALIGN="Left"> header </TH> </TR> <TR ALIGN="Left" VALIGN="Left"> <TD ALIGN="Left" BORDERCOLOR="#ff0000" ROWSPAN="2" VALIGN="Left"> example cell with colspan 1 and rowspan 2 </TD> <TD ALIGN="Left" VALIGN="Left"> 1.1 </TD> <TD ALIGN="Left" VALIGN="Left"> 2.1 </TD> </TR> <TR ALIGN="Left" VALIGN="Left"> <TD ALIGN="Left" VALIGN="Left"> 1.2 </TD> <TD ALIGN="Left" VALIGN="Left"> 2.2 </TD> </TR> <TR ALIGN="Left" VALIGN="Left"> <TD ALIGN="Left" VALIGN="Left"> cell test1 </TD> <TD ALIGN="Left" COLSPAN="2" ROWSPAN="2" VALIGN="Left"> big cell </TD> </TR> <TR ALIGN="Left" VALIGN="Left"> <TD ALIGN="Left" VALIGN="Left"> cell test2 </TD> </TR> </TABLE>
-
-
Field Summary
Fields Modifier and Type Field Description private int
alignment
This is the horizontal alignment.private 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)private float
cellpadding
This is cellpadding.private boolean
cellsFitPage
If true cells may not be split over two pages.private float
cellspacing
This is cellspacing.private int
columns
This is the number of columns in theTable
.private boolean
complete
Indicates if the PdfPTable is complete once added to the document.private boolean
convert2pdfptable
if you want to generate tables the old way, set this value to false.private java.awt.Point
curPosition
The current Position in the table.private Cell
defaultCell
This Empty Cell contains the DEFAULT layout of each Cell added with the method addCell(String content).private int
lastHeaderRow
This is the number of the last row of the table headers.private boolean
locked
Is the width a percentage (false) or an absolute width (true)?private boolean
mTableInserted
Boolean to track if a table was inserted (to avoid unnecessary computations afterwards)private boolean
notAddedYet
Indicates if this is the first time the section was added.private float
offset
This is the offset of the table.private java.util.ArrayList
rows
This is the list ofRow
s.private boolean
tableFitsPage
If true this table may not be split over two pages.private float
width
This is the width of the table (in percent of the available space).private float[]
widths
This is an array containing the widths (in percentages) of every column.-
Fields inherited from class com.aowagie.text.Rectangle
border, borderWidth, BOTTOM, BOX, LEFT, llx, lly, NO_BORDER, RIGHT, rotation, TOP, UNDEFINED, urx, ury
-
Fields inherited from interface com.aowagie.text.Element
ALIGN_BASELINE, ALIGN_BOTTOM, ALIGN_CENTER, ALIGN_JUSTIFIED, ALIGN_JUSTIFIED_ALL, ALIGN_LEFT, ALIGN_MIDDLE, ALIGN_RIGHT, ALIGN_TOP, ALIGN_UNDEFINED, ANCHOR, ANNOTATION, AUTHOR, CCITT_BLACKIS1, CCITT_ENCODEDBYTEALIGN, CCITT_ENDOFBLOCK, CCITT_ENDOFLINE, CCITTG3_1D, CCITTG3_2D, CCITTG4, CELL, CHAPTER, CHUNK, CREATIONDATE, CREATOR, HEADER, IMGRAW, IMGTEMPLATE, JBIG2, JPEG, JPEG2000, KEYWORDS, LIST, LISTITEM, MARKED, MULTI_COLUMN_TEXT, PARAGRAPH, PHRASE, PRODUCER, PTABLE, RECTANGLE, ROW, SECTION, SUBJECT, TABLE, TITLE, YMARK
-
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addCell(Cell cell)
Adds aCell
to theTable
.private void
addCell(Cell aCell, java.awt.Point aLocation)
Adds aCell
to theTable
at a certain location.private void
addCell(Phrase content, java.awt.Point location)
Adds aCell
to theTable
.void
addColumns(int aColumns)
Gives you the possibility to add columns.private void
assumeTableDefaults(Cell aCell)
Sets the unset cell properties to be the table defaults.void
complete()
Will fill empty cells with valid blankCell
sPdfPTable
createPdfPTable()
Create a PdfPTable based on this Table object.private void
fillEmptyMatrixCells()
adds newCell
's to empty/null spaces.void
flushContent()
Flushes the content that has been added.int
getAlignment()
Gets the horizontal alignment.java.util.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()
Deprecated.As of iText 2.0.7, replaced bygetDefaultCell()
, scheduled for removal at 2.2.0java.awt.Dimension
getDimension()
Gets the dimension of this tableprivate java.lang.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 thisTable
.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.(package private) 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.private void
insertTable(Table aTable, java.awt.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 thisTable
have to fit a page.boolean
isComplete()
Indicates if the element is complete or not.boolean
isConvert2pdfptable()
Method to check if the Table should be converted to a PdfPTable or not.boolean
isLocked()
boolean
isNestable()
Checks if this element is nestable.boolean
isNotAddedYet()
Indicates if this is the first time the section is added.boolean
isTableFitsPage()
Checks if thisTable
has to fit a page.private boolean
isValidLocation(Cell aCell, java.awt.Point aLocation)
check ifCell
'fits' the table.java.util.Iterator
iterator()
Gets anIterator
of all theRow
s.private void
mergeInsertedTables()
Integrates all added tables and recalculates column widths.private void
placeCell(java.util.ArrayList someRows, Cell aCell, java.awt.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 anElementListener
.void
setAlignment(int value)
Sets the horizontal alignment.void
setAlignment(java.lang.String alignment)
Sets the alignment of this paragraph.void
setAutoFillEmptyCells(boolean aDoAutoFill)
Enables/disables automatic insertion of empty cells before table is rendered.void
setCellsFitPage(boolean fitPage)
Allows you to control when a page break occurs.void
setComplete(boolean complete)
If you invoke setComplete(false), you indicate that the content of the object isn't complete yet; it can be added to the document partially, but more will follow.void
setConvert2pdfptable(boolean convert2pdfptable)
If set to true, iText will try to convert the Table to a PdfPTable.private void
setCurrentLocationToNextValidPosition(java.awt.Point aLocation)
Sets current col/row to valid(empty) pos after addCell/Tablevoid
setDefaultCell(Cell value)
Sets the default layout of the Table to the provided Cellvoid
setDefaultLayout(Cell value)
Deprecated.As of iText 2.0.7, replaced bysetDefaultCell(Cell)
, scheduled for removal at 2.2.0void
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 thisTable
.int
type()
Gets the type of the text element.-
Methods inherited from class com.aowagie.text.Rectangle
cloneNonPositionParameters, disableBorderSide, enableBorderSide, getBackgroundColor, getBorder, getBorderColor, getBorderColorBottom, getBorderColorLeft, getBorderColorRight, getBorderColorTop, getBorderWidth, getBorderWidthBottom, getBorderWidthLeft, getBorderWidthRight, getBorderWidthTop, getBottom, getBottom, getGrayFill, getHeight, getLeft, getLeft, getRight, getRight, getRotation, getTop, getTop, hasBorder, hasBorders, isContent, isUseVariableBorders, normalize, rectangle, rotate, setBackgroundColor, setBorder, setBorderColor, setBorderColorBottom, setBorderColorLeft, setBorderColorRight, setBorderColorTop, setBorderWidth, setBorderWidthBottom, setBorderWidthLeft, setBorderWidthRight, setBorderWidthTop, setBottom, setGrayFill, setLeft, setRight, setTop, setUseVariableBorders, softCloneNonPositionParameters, toString
-
-
-
-
Field Detail
-
columns
private int columns
This is the number of columns in theTable
.
-
rows
private java.util.ArrayList rows
This is the list ofRow
s.
-
curPosition
private java.awt.Point curPosition
The current Position in the table.
-
defaultCell
private Cell defaultCell
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.
-
alignment
private int alignment
This is the horizontal alignment.
-
cellpadding
private float cellpadding
This is cellpadding.
-
cellspacing
private float cellspacing
This is cellspacing.
-
width
private float width
This is the width of the table (in percent of the available space).
-
locked
private boolean locked
Is the width a percentage (false) or an absolute width (true)?
-
widths
private float[] widths
This is an array containing the widths (in percentages) of every column.
-
mTableInserted
private boolean mTableInserted
Boolean to track if a table was inserted (to avoid unnecessary computations afterwards)
-
autoFillEmptyCells
private 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)
-
tableFitsPage
private boolean tableFitsPage
If true this table may not be split over two pages.
-
cellsFitPage
private boolean cellsFitPage
If true cells may not be split over two pages.
-
offset
private float offset
This is the offset of the table.
-
convert2pdfptable
private boolean convert2pdfptable
if you want to generate tables the old way, set this value to false.
-
notAddedYet
private boolean notAddedYet
Indicates if this is the first time the section was added.- Since:
- iText 2.0.8
-
complete
private boolean complete
Indicates if the PdfPTable is complete once added to the document.- Since:
- iText 2.0.8
-
-
Constructor Detail
-
Table
public Table(int columns) throws BadElementException
Constructs aTable
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
private Table(int columns, int rows) throws BadElementException
Constructs aTable
with a certain number of columns and a certain number ofRow
s.- Parameters:
columns
- The number of columns in the tablerows
- The number of rows- Throws:
BadElementException
- if the creator was called with less than 1 column
-
-
Method Detail
-
process
public boolean process(ElementListener listener)
Processes the element by adding it (or the different parts) to anElementListener
.
-
type
public int type()
Gets the type of the text element.
-
getChunks
public java.util.ArrayList getChunks()
Gets all the chunks in this element.
-
isNestable
public boolean isNestable()
Description copied from interface:Element
Checks if this element is nestable.- Specified by:
isNestable
in interfaceElement
- Overrides:
isNestable
in classRectangle
- Returns:
- true if this element can be nested inside other elements.
- Since:
- iText 2.0.8
- See Also:
Element.isNestable()
-
getColumns
public int getColumns()
Gets the number of columns.- Returns:
- a value
-
size
public int size()
Gets the number of rows in thisTable
.- Returns:
- the number of rows in this
Table
-
getDimension
public java.awt.Dimension getDimension()
Gets the dimension of this table- Returns:
- dimension
-
getDefaultCell
public Cell getDefaultCell()
Gets the default layout of the Table.- Returns:
- a cell with all the defaults
- Since:
- 2.0.7
-
setDefaultCell
public void setDefaultCell(Cell value)
Sets the default layout of the Table to the provided Cell- Parameters:
value
- a cell with all the defaults- Since:
- 2.0.7
-
getLastHeaderRow
public int getLastHeaderRow()
Gets the last number of the rows that contain headers.- Returns:
- a rownumber
-
setLastHeaderRow
public void setLastHeaderRow(int value)
Sets the horizontal alignment.- Parameters:
value
- the new value
-
getAlignment
public int getAlignment()
Gets the horizontal alignment.- Returns:
- a value
-
setAlignment
public void setAlignment(int value)
Sets the horizontal alignment.- Parameters:
value
- the new value
-
setAlignment
public void setAlignment(java.lang.String alignment)
Sets the alignment of this paragraph.- Parameters:
alignment
- the new alignment as aString
-
getPadding
public float getPadding()
Gets the cellpadding.- Returns:
- a value
-
setPadding
public void setPadding(float value)
Sets the cellpadding.- Parameters:
value
- the new value
-
getSpacing
public float getSpacing()
Gets the cellspacing.- Returns:
- a value
-
setSpacing
public void setSpacing(float value)
Sets the cellspacing.- Parameters:
value
- the new value
-
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 behavior may be very welcome. Disabling is recommended to increase speed. (empty cells should be added through extra code then)- Parameters:
aDoAutoFill
- enable/disable autofill
-
getWidth
public float getWidth()
Gets the table width (a percentage).
-
setWidth
public void setWidth(float width)
Sets the width of this table (in percentage of the available space).- Parameters:
width
- the width
-
isLocked
public boolean isLocked()
- Returns:
- the locked
-
setLocked
public void setLocked(boolean locked)
- Parameters:
locked
- the locked to set
-
getProportionalWidths
public float[] getProportionalWidths()
Gets the proportional widths of the columns in thisTable
.- Returns:
- the proportional widths of the columns in this
Table
-
setWidths
public void setWidths(float[] widths) throws BadElementException
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:
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
- Exception with information about the error
-
setWidths
public void setWidths(int[] widths) throws DocumentException
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
- Exception with information about the error
-
isTableFitsPage
public boolean isTableFitsPage()
Checks if thisTable
has to fit a page.- Returns:
- true if the table may not be split
-
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
-
isCellsFitPage
public boolean isCellsFitPage()
Checks if the cells of thisTable
have to fit a page.- Returns:
- true if the cells may not be split
-
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
-
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.
-
getOffset
public float getOffset()
Gets the offset of this table.- Returns:
- the space between this table and the previous element.
-
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 old fashioned way.
-
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
-
addCell
private void addCell(Cell aCell, java.awt.Point aLocation) throws BadElementException
Adds aCell
to theTable
at a certain location.- Parameters:
aCell
- TheCell
to addaLocation
- The location where theCell
will be added- Throws:
BadElementException
-
addCell
public void addCell(Cell cell)
Adds aCell
to theTable
.- Parameters:
cell
- aCell
-
addCell
private void addCell(Phrase content, java.awt.Point location) throws BadElementException
Adds aCell
to theTable
.This is a shortcut for
addCell(Cell cell, Point location)
. ThePhrase
will be converted to aCell
.- Parameters:
content
- aPhrase
location
- aPoint
- Throws:
BadElementException
- this should never happen
-
insertTable
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
private void insertTable(Table aTable, java.awt.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 insertaLocation
- aPoint
-
addColumns
public void addColumns(int aColumns)
Gives you the possibility to add columns.- Parameters:
aColumns
- the number of columns to add
-
complete
public void complete()
Will fill empty cells with valid blankCell
s
-
getElement
private java.lang.Object getElement(int row, int column)
returns the element at the position row, column (Cast to Cell or Table)- Parameters:
row
-column
-- Returns:
- dimension
- Since:
- 2.1.0 (was made private in 2.0.3)
-
mergeInsertedTables
private void mergeInsertedTables()
Integrates all added tables and recalculates column widths.
-
fillEmptyMatrixCells
private void fillEmptyMatrixCells()
adds newCell
's to empty/null spaces.
-
isValidLocation
private boolean isValidLocation(Cell aCell, java.awt.Point aLocation)
check ifCell
'fits' the table.- rowspan/colspan not beyond borders
- spanned cell don't overlap existing cells
- Parameters:
aCell
- the cell that has to be checkedaLocation
- the location where the cell has to be placed- Returns:
- true if the location was valid
-
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.
-
placeCell
private void placeCell(java.util.ArrayList someRows, Cell aCell, java.awt.Point aPosition)
Inserts a Cell in a cell-array and reserves cells defined by row-/colspan.- Parameters:
someRows
- some rowsaCell
- the cell that has to be insertedaPosition
- the position where the cell has to be placed
-
setCurrentLocationToNextValidPosition
private void setCurrentLocationToNextValidPosition(java.awt.Point aLocation)
Sets current col/row to valid(empty) pos after addCell/Table- Parameters:
aLocation
- a location in the Table
-
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 border positions
-
iterator
public java.util.Iterator iterator()
Gets anIterator
of all theRow
s.- Returns:
- an
Iterator
-
createPdfPTable
public PdfPTable createPdfPTable() throws BadElementException
Create a PdfPTable based on this Table object.- Returns:
- a PdfPTable object
- Throws:
BadElementException
- Exception with information about the error
-
isNotAddedYet
public boolean isNotAddedYet()
Indicates if this is the first time the section is added.- Returns:
- true if the section wasn't added yet
- Since:
- iText2.0.8
-
setNotAddedYet
public void setNotAddedYet(boolean notAddedYet)
Sets the indication if the section was already added to the document.- Parameters:
notAddedYet
- Indication that will be set- Since:
- iText2.0.8
-
flushContent
public void flushContent()
Description copied from interface:LargeElement
Flushes the content that has been added.- Specified by:
flushContent
in interfaceLargeElement
- Since:
- iText 2.0.8
- See Also:
LargeElement.flushContent()
-
isComplete
public boolean isComplete()
Description copied from interface:LargeElement
Indicates if the element is complete or not.- Specified by:
isComplete
in interfaceLargeElement
- Returns:
- indicates if the element is complete according to the user.
- Since:
- iText 2.0.8
- See Also:
LargeElement.isComplete()
-
setComplete
public void setComplete(boolean complete)
Description copied from interface:LargeElement
If you invoke setComplete(false), you indicate that the content of the object isn't complete yet; it can be added to the document partially, but more will follow. If you invoke setComplete(true), you indicate that you won't add any more data to the object.- Specified by:
setComplete
in interfaceLargeElement
- Parameters:
complete
- false if you'll be adding more data after adding the object to the document.- Since:
- iText 2.0.8
- See Also:
LargeElement.setComplete(boolean)
-
getDefaultLayout
@Deprecated public Cell getDefaultLayout()
Deprecated.As of iText 2.0.7, replaced bygetDefaultCell()
, scheduled for removal at 2.2.0Gets the default layout of the Table.- Returns:
- a cell with all the defaults
-
setDefaultLayout
@Deprecated public void setDefaultLayout(Cell value)
Deprecated.As of iText 2.0.7, replaced bysetDefaultCell(Cell)
, scheduled for removal at 2.2.0Sets the default layout of the Table to the provided Cell- Parameters:
value
- a cell with all the defaults
-
-