public class Table extends Rectangle implements LargeElement
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:
<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>
border, borderWidth, BOTTOM, BOX, LEFT, llx, lly, NO_BORDER, RIGHT, rotation, TOP, UNDEFINED, urx, uryALIGN_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| Constructor and Description |
|---|
Table(int columns)
Constructs a
Table with a certain number of columns. |
| Modifier and Type | Method and Description |
|---|---|
void |
addCell(Cell cell)
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 |
flushContent()
Flushes the content that has been added.
|
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()
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
|
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.
|
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)
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.
|
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 |
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, setTop, setUseVariableBorders, softCloneNonPositionParameters, toStringpublic 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 boolean process(ElementListener listener)
ElementListener.public int type()
public ArrayList getChunks()
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 getAlignment()
public void setAlignment(int value)
value - the new valuepublic void setAlignment(String alignment)
alignment - the new alignment as a Stringpublic 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 valuesBadElementException - Exception with information about the errorpublic 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 valuesDocumentException - Exception with information about the errorpublic 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 cell)
Cell to the Table.cell - a Cellpublic void addColumns(int aColumns)
aColumns - the number of columns to addpublic void complete()
Cellspublic 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
BadElementException - Exception with information about the errorpublic boolean isNotAddedYet()
public void setNotAddedYet(boolean notAddedYet)
notAddedYet - Indication that will be setpublic 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)@Deprecated public Cell getDefaultLayout()
getDefaultCell(),
scheduled for removal at 2.2.0@Deprecated public void setDefaultLayout(Cell value)
setDefaultCell(Cell),
scheduled for removal at 2.2.0value - a cell with all the defaultsCopyright © 2021. All rights reserved.