|
![]() |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.lowagie.text.Rectangle
com.lowagie.text.Table
public class Table
A 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 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:
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 |
Rectangle
,
Element
,
Row
,
Cell
Field Summary | |
---|---|
private 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) |
private float |
cellpadding
This is cellpadding. |
(package 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 the Table . |
protected 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 |
defaultLayout
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) |
(package private) float |
offset
This is the offset of the table. |
private java.util.ArrayList |
rows
This is the list of Row s. |
(package 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.lowagie.text.Rectangle |
---|
backgroundColor, border, borderColor, borderColorBottom, borderColorLeft, borderColorRight, borderColorTop, borderWidth, borderWidthBottom, borderWidthLeft, borderWidthRight, borderWidthTop, BOTTOM, BOX, LEFT, llx, lly, NO_BORDER, RIGHT, rotation, TOP, UNDEFINED, urx, ury, useVariableBorders |
Fields inherited from interface com.lowagie.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, JPEG, JPEG2000, KEYWORDS, LIST, LISTITEM, MARKED, MULTI_COLUMN_TEXT, PARAGRAPH, PHRASE, PRODUCER, PTABLE, RECTANGLE, ROW, SECTION, SUBJECT, TABLE, TITLE |
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(java.util.Properties attributes)
Deprecated. |
|
Table(Table t)
Copy constructor (shallow copy). |
Method Summary | |
---|---|
java.lang.String |
absWidth()
Deprecated. use getWidth |
void |
addCell(Cell cell)
Adds a Cell to the Table . |
void |
addCell(Cell aCell,
int row,
int column)
Adds a Cell to the Table at a certain row and column. |
void |
addCell(Cell aCell,
java.awt.Point aLocation)
Adds a Cell to the Table at a certain location. |
void |
addCell(Phrase content)
Adds a Cell to the Table . |
void |
addCell(Phrase content,
java.awt.Point location)
Adds a Cell to the Table . |
void |
addCell(java.lang.String content)
Adds a Cell to the Table . |
void |
addCell(java.lang.String content,
java.awt.Point location)
Adds a Cell to the Table . |
void |
addColumns(int aColumns)
Gives you the posibility to add columns. |
int |
alignment()
Deprecated. Use getAlignment() instead |
private void |
assumeTableDefaults(Cell aCell)
Sets the unset cell properties to be the table defaults. |
float |
cellpadding()
Deprecated. Use getPadding() instead |
float |
cellspacing()
Deprecated. Use getSpacing() instead |
int |
columns()
Deprecated. Use getColumns() instead |
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. |
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. |
private void |
fillEmptyMatrixCells()
adds new Cell 's to empty/null spaces. |
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 |
getDefaultLayout()
Gets the default layout of the Table. |
java.awt.Dimension |
getDimension()
Gets the dimension of this table |
private 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 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,
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 this Table have to fit a page. |
boolean |
isConvert2pdfptable()
Method to check if the Table should be converted to a PdfPTable or not. |
boolean |
isLocked()
|
boolean |
isTableFitsPage()
Checks if this Table has to fit a page. |
private boolean |
isValidLocation(Cell aCell,
java.awt.Point aLocation)
check if Cell 'fits' the table. |
java.util.Iterator |
iterator()
Gets an Iterator of all the Row s. |
int |
lastHeaderRow()
Deprecated. Use getLastHeaderRow() instead |
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 an ElementListener . |
void |
setAbsWidth(java.lang.String width)
Deprecated. setTotalWidth(int width) |
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 |
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/Table |
void |
setDefaultCellBackgroundColor(java.awt.Color color)
Deprecated. use getDefaultCell.setBackgroundColor |
void |
setDefaultCellBorder(int value)
Deprecated. use getDefaultCell.setBorder |
void |
setDefaultCellBorderColor(java.awt.Color color)
Deprecated. getDefaultCell.setBorderColor |
void |
setDefaultCellBorderWidth(float value)
Deprecated. use getDefaultCell.setBorderWidth |
void |
setDefaultCellGrayFill(float value)
Deprecated. use getDefaultCell.setGrayFill |
void |
setDefaultColspan(int value)
Deprecated. use getDefaultCell.setColspan |
void |
setDefaultHorizontalAlignment(int value)
Deprecated. use getDefaultCell.setHorizontalAlignment |
void |
setDefaultLayout(Cell value)
Sets the default layout of the Table to the provided Cell |
void |
setDefaultRowspan(int value)
Deprecated. use getDefaultCell.setRowspan |
void |
setDefaultVerticalAlignment(int value)
Deprecated. use getDefaultCell.setVerticalAlignment |
void |
setLastHeaderRow(int value)
Sets the horizontal alignment. |
void |
setLocked(boolean locked)
|
void |
setOffset(float offset)
Sets the offset of this table. |
void |
setPadding(float value)
Sets the cellpadding. |
void |
setSpaceBetweenCells(float value)
Deprecated. use setSpacing |
void |
setSpaceInsideCell(float value)
Deprecated. use setPadding |
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. |
float |
widthPercentage()
Deprecated. Use getWidth() instead |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private int columns
Table
.
private java.util.ArrayList rows
Row
s.
private java.awt.Point curPosition
private Cell defaultLayout
private int lastHeaderRow
private int alignment
private float cellpadding
private float cellspacing
private float width
private boolean locked
private float[] widths
private boolean mTableInserted
protected boolean autoFillEmptyCells
boolean tableFitsPage
boolean cellsFitPage
float offset
protected boolean convert2pdfptable
Constructor Detail |
---|
public Table(int columns) throws BadElementException
Table
with a certain number of columns.
columns
- The number of columns in the table
BadElementException
- if the creator was called with less than 1 columnpublic Table(int columns, int rows) throws BadElementException
Table
with a certain number of columns
and a certain number of Row
s.
columns
- The number of columns in the tablerows
- The number of rows
BadElementException
- if the creator was called with less than 1 columnpublic Table(Table t)
BadElementException
public Table(java.util.Properties attributes)
Table
that has been constructed taking in account
the value of some attributes.
attributes
- Some attributes
BadElementException
Method Detail |
---|
public boolean process(ElementListener listener)
ElementListener
.
process
in interface Element
process
in class Rectangle
listener
- an ElementListener
true
if the element was processed successfullypublic int type()
type
in interface Element
type
in class Rectangle
public java.util.ArrayList getChunks()
getChunks
in interface Element
getChunks
in class Rectangle
ArrayList
public int getColumns()
public int size()
Table
.
Table
public java.awt.Dimension getDimension()
public Cell getDefaultLayout()
public void setDefaultLayout(Cell value)
value
- a cell with all the defaultspublic int getLastHeaderRow()
public void setLastHeaderRow(int value)
value
- the new valuepublic int endHeaders()
public int getAlignment()
public void setAlignment(int value)
value
- the new valuepublic void setAlignment(java.lang.String alignment)
alignment
- the new alignment as a String
public float getPadding()
public void setPadding(float value)
value
- the new valuepublic float getSpacing()
public void setSpacing(float value)
value
- the new valuepublic void setAutoFillEmptyCells(boolean aDoAutoFill)
aDoAutoFill
- enable/disable autofillpublic float getWidth()
getWidth
in class Rectangle
public void setWidth(float width)
width
- the widthpublic boolean isLocked()
public void setLocked(boolean locked)
locked
- the locked to setpublic float[] getProportionalWidths()
Table
.
Table
public void setWidths(float[] widths) 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.
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)
widths
- an array with values
BadElementException
public void setWidths(int[] widths) throws DocumentException
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.
widths
- an array with values
DocumentException
public boolean isTableFitsPage()
Table
has to fit a page.
public void setTableFitsPage(boolean fitPage)
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.
fitPage
- enter true if you don't want to split cellspublic boolean isCellsFitPage()
Table
have to fit a page.
public void setCellsFitPage(boolean fitPage)
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.
fitPage
- enter true if you don't want to split cellspublic void setOffset(float offset)
offset
- the space between this table and the previous object.public float getOffset()
public boolean isConvert2pdfptable()
public void setConvert2pdfptable(boolean convert2pdfptable)
convert2pdfptable
- true if you want iText to try to convert the Table to a PdfPTablepublic void addCell(Cell aCell, int row, int column) throws BadElementException
Cell
to the Table
at a certain row and column.
aCell
- The Cell
to addrow
- The row where the Cell
will be addedcolumn
- The column where the Cell
will be added
BadElementException
public void addCell(Cell aCell, java.awt.Point aLocation) throws BadElementException
Cell
to the Table
at a certain location.
aCell
- The Cell
to addaLocation
- The location where the Cell
will be added
BadElementException
public void addCell(Cell cell)
Cell
to the Table
.
cell
- a Cell
public void addCell(Phrase content) throws BadElementException
Cell
to the Table
.
This is a shortcut for addCell(Cell cell)
.
The Phrase
will be converted to a Cell
.
content
- a Phrase
BadElementException
- this should never happenpublic void addCell(Phrase content, java.awt.Point location) throws BadElementException
Cell
to the Table
.
This is a shortcut for addCell(Cell cell, Point location)
.
The Phrase
will be converted to a Cell
.
content
- a Phrase
location
- a Point
BadElementException
- this should never happenpublic void addCell(java.lang.String content) throws BadElementException
Cell
to the Table
.
This is a shortcut for addCell(Cell cell)
.
The String
will be converted to a Cell
.
content
- a String
BadElementException
- this should never happenpublic void addCell(java.lang.String content, java.awt.Point location) throws BadElementException
Cell
to the Table
.
This is a shortcut for addCell(Cell cell, Point location)
.
The String
will be converted to a Cell
.
content
- a String
location
- a Point
BadElementException
- this should never happenpublic void insertTable(Table aTable)
aTable
- the table you want to insertpublic void insertTable(Table aTable, int row, int column)
aTable
- The Table
to addrow
- The row where the Cell
will be addedcolumn
- The column where the Cell
will be addedpublic void insertTable(Table aTable, java.awt.Point aLocation)
aTable
- the table you want to insertaLocation
- a Point
public void addColumns(int aColumns)
aColumns
- the number of columns to addpublic void deleteColumn(int column) throws BadElementException
column
- the number of the column that has to be deleted
BadElementException
public boolean deleteRow(int row)
row
- the number of the row to delete
true
if the row was deleted; false
if notpublic void deleteAllRows()
public boolean deleteLastRow()
true
if the row was deleted; false
if notpublic void complete()
Cell
s
private java.lang.Object getElement(int row, int column)
row
- column
-
private void mergeInsertedTables()
private void fillEmptyMatrixCells()
Cell
's to empty/null spaces.
private boolean isValidLocation(Cell aCell, java.awt.Point aLocation)
Cell
'fits' the table.
aCell
- the cell that has to be checkedaLocation
- the location where the cell has to be placed
private void assumeTableDefaults(Cell aCell)
aCell
- The cell to set to table defaults as necessary.private void placeCell(java.util.ArrayList someRows, Cell aCell, java.awt.Point aPosition)
someRows
- some rowsaCell
- the cell that has to be insertedaPosition
- the position where the cell has to be placedprivate void setCurrentLocationToNextValidPosition(java.awt.Point aLocation)
aLocation
- a location in the Tablepublic float[] getWidths(float left, float totalWidth)
This method translates the widths expressed in percentages into the x-coordinate of the borders of the columns on a real document.
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)
public java.util.Iterator iterator()
Iterator
of all the Row
s.
Iterator
public PdfPTable createPdfPTable() throws BadElementException
BadElementException
public int columns()
getColumns()
instead
public int alignment()
getAlignment()
instead
public float cellpadding()
getPadding()
instead
public float cellspacing()
getSpacing()
instead
public void setSpaceInsideCell(float value)
value
- the new valuepublic void setSpaceBetweenCells(float value)
value
- the new valuepublic int lastHeaderRow()
getLastHeaderRow()
instead
public float widthPercentage()
getWidth()
instead
public void setAbsWidth(java.lang.String width)
width
- the widthpublic java.lang.String absWidth()
public void setDefaultCellBorder(int value)
Cell
s
added with method addCell(String content)
.
value
- the new border valuepublic void setDefaultCellBorderWidth(float value)
Cell
s
added with method addCell(String content)
.
value
- the new widthpublic void setDefaultCellBorderColor(java.awt.Color color)
Cell
s
added with method addCell(String content)
.
color
- the new colorpublic void setDefaultCellBackgroundColor(java.awt.Color color)
Cell
s
added with method addCell(String content)
.
color
- the new colorpublic void setDefaultCellGrayFill(float value)
Cell
s
added with method addCell(String content)
.
value
- the new valuepublic void setDefaultHorizontalAlignment(int value)
Cell
s
added with method addCell(String content)
.
value
- the new alignment valuepublic void setDefaultVerticalAlignment(int value)
Cell
s
added with method addCell(String content)
.
value
- the new alignment valuepublic void setDefaultRowspan(int value)
Cell
s
added with method addCell(String content)
.
value
- the new rowspan valuepublic void setDefaultColspan(int value)
Cell
s
added with method addCell(String content)
.
value
- the new colspan value
|
![]() |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |