public class Table extends TableRectangle implements LargeElement, WithHorizontalAlignment
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 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:
// 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 | ||
| Modifier and Type | Field and Description |
|---|---|
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)
|
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.
|
protected boolean |
notAddedYet
Indicates if this is the first time the section was added.
|
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, useVariableBordersALIGN_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, FOOTNOTE, HEADER, IMGRAW, IMGTEMPLATE, JBIG2, JPEG, JPEG2000, KEYWORDS, LIST, LISTITEM, MARKED, MULTI_COLUMN_TEXT, PARAGRAPH, PHRASE, PRODUCER, PTABLE, RECTANGLE, ROW, SECTION, SUBJECT, TABLE, TITLE, YMARK| Constructor and Description |
|---|
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).
|
| Modifier and Type | Method and Description |
|---|---|
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,
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,
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 |
complete()
Will fill empty cells with valid blank
Cells |
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.
|
void |
flushContent()
Flushes the content that has been added.
|
int |
getAlignment()
Gets the horizontal alignment.
|
ArrayList<Element> |
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 by
getDefaultCell(),
scheduled for removal at 2.2.0 |
Dimension |
getDimension()
Gets the dimension of this table
|
TableRectangle |
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()
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 this
Table has to fit a page. |
Iterator |
iterator()
Gets an
Iterator of all the Rows. |
boolean |
process(ElementListener listener)
Processes the element by adding it (or the different parts) to an
ElementListener. |
void |
setAlignment(int value)
Deprecated.
Setting alignment through unconstrained types is non-obvious and error-prone,
use
setHorizontalAlignment(HorizontalAlignment) instead |
void |
setAlignment(String alignment)
Deprecated.
Setting alignment through unconstrained types is non-obvious and error-prone,
use
setHorizontalAlignment(HorizontalAlignment) instead |
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.
|
void |
setDefaultCell(Cell value)
Sets the default layout of the Table to
the provided Cell
|
void |
setDefaultLayout(Cell value)
Deprecated.
As of iText 2.0.7, replaced by
setDefaultCell(Cell),
scheduled for removal at 2.2.0 |
void |
setHorizontalAlignment(HorizontalAlignment alignment)
Sets horizontal alignment mode.
|
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.
|
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, setRotation, setTop, setUseVariableBorders, softCloneNonPositionParameters, toStringprotected boolean autoFillEmptyCells
protected boolean convert2pdfptable
protected boolean notAddedYet
protected boolean complete
public Table(int columns)
throws BadElementException
Table with a certain number of columns.columns - The number of columns in the tableBadElementException - 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 Rows.columns - The number of columns in the tablerows - The number of rowsBadElementException - if the creator was called with less than 1 columnpublic Table(Table t)
public boolean process(ElementListener listener)
ElementListener.public int type()
public boolean isNestable()
ElementisNestable in interface ElementisNestable in class RectangleElement.isNestable()public int getColumns()
public int size()
Table.Tablepublic Dimension getDimension()
public Cell getDefaultCell()
public void setDefaultCell(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)
setHorizontalAlignment(HorizontalAlignment) insteadvalue - the new valuepublic void setAlignment(String alignment)
setHorizontalAlignment(HorizontalAlignment) insteadalignment - the new alignment as a Stringpublic void setHorizontalAlignment(HorizontalAlignment alignment)
WithHorizontalAlignmentsetHorizontalAlignment in interface WithHorizontalAlignmentalignment - New alignment mode. If null, current alignment must be left unchangedpublic 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()
public void setWidth(float width)
width - the widthpublic boolean isLocked()
public void setLocked(boolean locked)
locked - the locked to setpublic float[] getProportionalWidths()
Table.Tablepublic 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:
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.widths - an array with valuesBadElementExceptionpublic 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 valuesDocumentExceptionpublic 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 addedBadElementExceptionpublic void addCell(Cell aCell, Point aLocation) throws BadElementException
Cell to the Table at a certain location.aCell - The Cell to addaLocation - The location where the Cell will be addedBadElementExceptionpublic void addCell(Cell cell)
Cell to the Table.cell - a Cellpublic 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 PhraseBadElementException - this should never happenpublic void addCell(Phrase content, 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 Phraselocation - a PointBadElementException - this should never happenpublic void addCell(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 StringBadElementException - this should never happenpublic void addCell(String content, 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 Stringlocation - a PointBadElementException - 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, Point aLocation)
aTable - the table you want to insertaLocation - a Pointpublic 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 deletedBadElementExceptionpublic boolean deleteRow(int row)
row - the number of the row to deletetrue if the row was deleted; false if notpublic void deleteAllRows()
public boolean deleteLastRow()
true if the row was deleted; false if notpublic void complete()
Cellspublic TableRectangle getElement(int row, int column)
row - column - public 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 Iterator iterator()
Iterator of all the Rows.Iteratorpublic PdfPTable createPdfPTable() throws BadElementException
BadElementExceptionpublic boolean isNotAddedYet()
public void setNotAddedYet(boolean notAddedYet)
notAddedYet - public void flushContent()
LargeElementflushContent in interface LargeElementLargeElement.flushContent()public boolean isComplete()
LargeElementisComplete in interface LargeElementLargeElement.isComplete()public void setComplete(boolean complete)
LargeElementsetComplete in interface LargeElementcomplete - false if you'll be adding more data after
adding the object to the document.LargeElement.setComplete(boolean)public Cell getDefaultLayout()
getDefaultCell(),
scheduled for removal at 2.2.0public void setDefaultLayout(Cell value)
setDefaultCell(Cell),
scheduled for removal at 2.2.0value - a cell with all the defaultsCopyright © 2019. All rights reserved.