Class Table

All Implemented Interfaces:
IAbstractElement, IBlockElement, IElement, ILargeElement, IPropertyContainer, IAccessibleElement

public class Table extends BlockElement<Table> implements ILargeElement
A Table is a layout element that represents data in a two-dimensional grid. It is filled with cells, ordered in rows and columns.

It is an implementation of ILargeElement, which means it can be flushed to the canvas, in order to reclaim memory that is locked up.

  • Field Details

  • Constructor Details

    • Table

      public Table (float[] columnWidths, boolean largeTable)
      Constructs a Table with the preferable column widths.
      Since 7.0.2 table layout algorithms were introduced. Auto layout is default, except large tables. For large table 100% width and fixed layout set implicitly.
      Note, the eventual columns width depends on selected layout, table width, cell's width, cell's min-widths, and cell's max-widths. Table layout algorithm has the same behaviour as expected for CSS table-layout property, where columnWidths is 's widths. For more information see setAutoLayout() and setFixedLayout().
      Parameters:
      columnWidths - preferable column widths in points. Values must be greater than or equal to zero, otherwise it will be interpreted as undefined.
      largeTable - whether parts of the table will be written before all data is added. Note, large table does not support auto layout, table width shall not be removed.
      See Also:
    • Table

      public Table (UnitValue[] columnWidths, boolean largeTable)
      Constructs a Table with the preferable column widths.
      Since 7.0.2 table layout algorithms were introduced. Auto layout is default, except large tables. For large table 100% width and fixed layout set implicitly.
      Note, the eventual columns width depends on selected layout, table width, cell's width, cell's min-widths, and cell's max-widths. Table layout algorithm has the same behaviour as expected for CSS table-layout property, where columnWidths is 's widths. For more information see setAutoLayout() and setFixedLayout().
      Parameters:
      columnWidths - preferable column widths, points and/or percents. Values must be greater than or equal to zero, otherwise it will be interpreted as undefined.
      largeTable - whether parts of the table will be written before all data is added. Note, large table does not support auto layout, table width shall not be removed.
      See Also:
    • Table

      public Table (UnitValue[] columnWidths)
      Constructs a Table with the preferable column widths.
      Since 7.0.2 table layout algorithms were introduced. Auto layout is default.
      Note, the eventual columns width depends on selected layout, table width, cell's width, cell's min-widths, and cell's max-widths. Table layout algorithm has the same behaviour as expected for CSS table-layout property, where columnWidths is 's widths. For more information see setAutoLayout() and setFixedLayout().
      Parameters:
      columnWidths - preferable column widths, points and/or percents. Values must be greater than or equal to zero, otherwise it will be interpreted as undefined.
      See Also:
    • Table

      public Table (float[] pointColumnWidths)
      Constructs a Table with the preferable column widths.
      Since 7.0.2 table layout algorithms were introduced. Auto layout is default.
      Note, the eventual columns width depends on selected layout, table width, cell's width, cell's min-widths, and cell's max-widths. Table layout algorithm has the same behaviour as expected for CSS table-layout property, where columnWidths is 's widths. For more information see setAutoLayout() and setFixedLayout().
      Parameters:
      pointColumnWidths - preferable column widths in points. Values must be greater than or equal to zero, otherwise it will be interpreted as undefined.
      See Also:
    • Table

      public Table (int numColumns, boolean largeTable)
      Constructs a Table with specified number of columns. The final column widths depend on selected table layout.
      Since 7.0.2 table layout algorithms were introduced. Auto layout is default, except large tables. For large table fixed layout set implicitly.
      Since 7.1 table will have undefined column widths, that will be determined during layout. In oder to set equal percent width as column width, use UnitValue.createPercentArray(int)
      Note, the eventual columns width depends on selected layout, table width, cell's width, cell's min-widths, and cell's max-widths. Table layout algorithm has the same behaviour as expected for CSS table-layout property, where columnWidths is 's widths. For more information see setAutoLayout() and setFixedLayout().
      Parameters:
      numColumns - the number of columns, each column will have equal percent width.
      largeTable - whether parts of the table will be written before all data is added. Note, large table does not support auto layout, table width shall not be removed.
      See Also:
    • Table

      public Table (int numColumns)
      Constructs a Table with specified number of columns. The final column widths depend on selected table layout.
      Since 7.0.2 table layout was introduced. Auto layout is default, except large tables. For large table fixed layout set implicitly.

      Since 7.1 table will have undefined column widths, that will be determined during layout. In oder to set equal percent width as column width, use UnitValue.createPercentArray(int)

      Note, the eventual columns width depends on selected layout, table width, cell's width, cell's min-widths, and cell's max-widths. Table layout algorithm has the same behaviour as expected for CSS table-layout property, where columnWidths is 's widths. For more information see setAutoLayout() and setFixedLayout().

      Parameters:
      numColumns - the number of columns, each column will have equal percent width.
      See Also:
  • Method Details

    • setFixedLayout

      public Table setFixedLayout()
      Set fixed layout. Analog of table-layout:fixed CSS property. Note, the table must have width property, otherwise auto layout will be used.

      Algorithm description
      1. Scan columns for width property and set it. All the rest columns get undefined value. Column width includes borders and paddings. Columns have set in constructor, analog of element in HTML.
      2. Scan the very first row of table for width property and set it to undefined columns. Cell width has lower priority in comparing with column. Cell width doesn't include borders and paddings.
      2.1 If cell has colspan and all columns are undefined, each column will get equal width: width/colspan.
      2.2 If some columns already have width, equal remain (original width minus existed) width will be added remainWidth/colspan to each column.
      3. If sum of columns is less, than table width, there are two options:
      3.1. If undefined columns still exist, they will get the rest remaining width.
      3.2. Otherwise all columns will be expanded proportionally based on its width.
      4. If sum of columns is greater, than table width, nothing to do.

      Returns:
      this element.
    • setAutoLayout

      public Table setAutoLayout()
      Set auto layout. Analog of table-layout:auto CSS property.
      Note, large table does not support auto layout.

      Algorithm principles.
      1. Column width cannot be less, than min-width of any cell in the column (calculated by layout).
      2. Specified table width has higher priority, than sum of column and cell widths.
      3. Percent value of cell and column width has higher priority, than point value.
      4. Cell width has higher priority, than column width.
      5. If column has no width, it will try to reach max-value (calculated by layout).

      Returns:
      this element.
    • useAllAvailableWidth

      public Table useAllAvailableWidth()
      Set Property.WIDTH = 100%.
      Returns:
      this element
    • getColumnWidth

      public UnitValue getColumnWidth (int column)
      Returns the column width for the specified column.
      Parameters:
      column - index of the column
      Returns:
      the width of the column
    • getNumberOfColumns

      public int getNumberOfColumns()
      Returns the number of columns.
      Returns:
      the number of columns.
    • getNumberOfRows

      public int getNumberOfRows()
      Returns the number of rows.
      Returns:
      the number of rows.
    • addHeaderCell

      public Table addHeaderCell (Cell headerCell)
      Adds a new cell to the header of the table. The header will be displayed in the top of every area of this table. See also setSkipFirstHeader(boolean).
      Parameters:
      headerCell - a header cell to be added
      Returns:
      this element
    • addHeaderCell

      public IElement> Table addHeaderCell (BlockElement blockElement)
      Adds a new cell with received blockElement as a content to the header of the table. The header will be displayed in the top of every area of this table. See also setSkipFirstHeader(boolean).
      Type Parameters:
      T - any IElement
      Parameters:
      blockElement - an element to be added to a header cell
      Returns:
      this element
    • addHeaderCell

      public Table addHeaderCell (Image image)
      Adds a new cell with received image to the header of the table. The header will be displayed in the top of every area of this table. See also setSkipFirstHeader(boolean).
      Parameters:
      image - an element to be added to a header cell
      Returns:
      this element
    • addHeaderCell

      public Table addHeaderCell (String content)
      Adds a new cell with received string as a content to the header of the table. The header will be displayed in the top of every area of this table. See also setSkipFirstHeader(boolean).
      Parameters:
      content - a string to be added to a header cell
      Returns:
      this element
    • getHeader

      public Table getHeader()
      Gets the header of the table. The header is represented as a distinct table and might have its own properties.
      Returns:
      table header or null, if addHeaderCell(Cell) hasn't been called.
    • addFooterCell

      public Table addFooterCell (Cell footerCell)
      Adds a new cell to the footer of the table. The footer will be displayed in the bottom of every area of this table. See also setSkipLastFooter(boolean).
      Parameters:
      footerCell - a footer cell
      Returns:
      this element
    • addFooterCell

      public IElement> Table addFooterCell (BlockElement blockElement)
      Adds a new cell with received blockElement as a content to the footer of the table. The footer will be displayed in the bottom of every area of this table. See also setSkipLastFooter(boolean).
      Type Parameters:
      T - IElement instance
      Parameters:
      blockElement - an element to be added to a footer cell
      Returns:
      this element
    • addFooterCell

      public Table addFooterCell (Image image)
      Adds a new cell with received image as a content to the footer of the table. The footer will be displayed in the bottom of every area of this table. See also setSkipLastFooter(boolean).
      Parameters:
      image - an image to be added to a footer cell
      Returns:
      this element
    • addFooterCell

      public Table addFooterCell (String content)
      Adds a new cell with received string as a content to the footer of the table. The footer will be displayed in the bottom of every area of this table. See also setSkipLastFooter(boolean).
      Parameters:
      content - a content string to be added to a footer cell
      Returns:
      this element
    • getFooter

      public Table getFooter()
      Gets the footer of the table. The footer is represented as a distinct table and might have its own properties.
      Returns:
      table footer or null, if addFooterCell(Cell) hasn't been called.
    • isSkipFirstHeader

      public boolean isSkipFirstHeader()
      Tells you if the first header needs to be skipped (for instance if the header says "continued from the previous page").
      Returns:
      Value of property skipFirstHeader.
    • setSkipFirstHeader

      public Table setSkipFirstHeader (boolean skipFirstHeader)
      Skips the printing of the first header. Used when printing tables in succession belonging to the same printed table aspect.
      Parameters:
      skipFirstHeader - New value of property skipFirstHeader.
      Returns:
      this element
    • isSkipLastFooter

      public boolean isSkipLastFooter()
      Tells you if the last footer needs to be skipped (for instance if the footer says "continued on the next page")
      Returns:
      Value of property skipLastFooter.
    • setSkipLastFooter

      public Table setSkipLastFooter (boolean skipLastFooter)
      Skips the printing of the last footer. Used when printing tables in succession belonging to the same printed table aspect.
      Parameters:
      skipLastFooter - New value of property skipLastFooter.
      Returns:
      this element
    • setCaption

      public Table setCaption (Div caption)
      Sets the table's caption. If there is no Property.CAPTION_SIDE set (note that it's an inheritable property), CaptionSide.TOP will be used. Also the StandardRoles.CAPTION will be set on the element.
      Parameters:
      caption - The element to be set as a caption.
      Returns:
      this element
    • setCaption

      public Table setCaption (Div caption, CaptionSide side)
      Sets the table's caption and its caption side. Also the StandardRoles.CAPTION will be set on the element.
      Parameters:
      caption - The element to be set as a caption.
      side - The caption side to be set on the caption.
      Returns:
      this element
    • getCaption

      public Div getCaption()
      Gets the table's caption.
      Returns:
      the table's caption.
    • startNewRow

      public Table startNewRow()
      Starts new row. This mean that next cell will be added at the beginning of next line.
      Returns:
      this element
    • addCell

      public Table addCell (Cell cell)
      Adds a new cell to the table. The implementation decides for itself which row the cell will be placed on.
      Parameters:
      cell - Cell to add.
      Returns:
      this element
    • addCell

      public IElement> Table addCell (BlockElement blockElement)
      Adds a new cell with received blockElement as a content.
      Type Parameters:
      T - IElement instance
      Parameters:
      blockElement - a blockElement to add to the cell and then to the table
      Returns:
      this element
    • addCell

      public Table addCell (Image image)
      Adds a new cell with received image as a content.
      Parameters:
      image - an image to add to the cell and then to the table
      Returns:
      this element
    • addCell

      public Table addCell (String content)
      Adds a new cell with received string as a content.
      Parameters:
      content - a string to add to the cell and then to the table
      Returns:
      this element
    • getCell

      public Cell getCell (int row, int column)
      Returns a cell as specified by its location. If the cell is in a col-span or row-span and is not the top left cell, then null is returned.
      Parameters:
      row - the row of the cell. indexes are zero-based
      column - the column of the cell. indexes are zero-based
      Returns:
      the cell at the specified position.
    • createRendererSubTree

      public IRenderer createRendererSubTree()
      Creates a renderer subtree with root in the current table element. Compared to getRenderer(), the renderer returned by this method should contain all the child renderers for children of the current element.
      Specified by:
      createRendererSubTree in interface IElement
      Overrides:
      createRendererSubTree in class AbstractElement<Table>
      Returns:
      a TableRenderer subtree for this element
    • getRenderer

      public IRenderer getRenderer()
      Gets a table renderer for this element. Note that this method can be called more than once. By default each element should define its own renderer, but the renderer can be overridden by AbstractElement.setNextRenderer(IRenderer) method call.
      Specified by:
      getRenderer in interface IElement
      Overrides:
      getRenderer in class AbstractElement<Table>
      Returns:
      a table renderer for this element
    • isComplete

      public boolean isComplete()
      Description copied from interface: ILargeElement
      Checks whether an element has already been marked as complete.
      Specified by:
      isComplete in interface ILargeElement
      Returns:
      the completion marker boolean
    • complete

      public void complete()
      Indicates that all the desired content has been added to this large element and no more content will be added. After this method is called, more precise rendering is activated. For instance, a table may have a setSkipLastFooter(boolean) method set to true, and in case of large table on flush() we do not know if any more content will be added, so we might not place the content in the bottom of the page where it would fit, but instead add a footer, and place that content in the start of the page. Technically such result would look all right, but it would be more concise if we placed the content in the bottom and did not start new page. For such cases to be renderered more accurately, one can call complete() when some content is still there and not flushed.
      Specified by:
      complete in interface ILargeElement
    • flush

      public void flush()
      Writes the newly added content to the document.
      Specified by:
      flush in interface ILargeElement
    • flushContent

      public void flushContent()
      Flushes the content which has just been added to the document. This is a method for internal usage and is called automatically by the document.
      Specified by:
      flushContent in interface ILargeElement
    • setDocument

      public void setDocument (Document document)
      Description copied from interface: ILargeElement
      Sets the document this element is bound to. We cannot write a large element into several documents simultaneously because we would need more bulky interfaces for this feature. For now we went for simplicity.
      Specified by:
      setDocument in interface ILargeElement
      Parameters:
      document - the document
    • getLastRowBottomBorder

      public List<Border> getLastRowBottomBorder()
      Gets the markup properties of the bottom border of the (current) last row.
      Returns:
      an array of Border objects
    • setExtendBottomRow

      public Table setExtendBottomRow (boolean isExtended)
      Defines whether the Table should be extended to occupy all the space left in the available area in case it is the last element in this area.
      Parameters:
      isExtended - defines whether the Table should be extended
      Returns:
      this Table
    • setExtendBottomRowOnSplit

      public Table setExtendBottomRowOnSplit (boolean isExtended)
      Defines whether the Table should be extended to occupy all the space left in the available area in case the area has been split and it is the last element in the split part of this area.
      Parameters:
      isExtended - defines whether the Table should be extended
      Returns:
      this Table
    • setBorderCollapse

      public Table setBorderCollapse (BorderCollapsePropertyValue collapsePropertyValue)
      Sets the type of border collapse.
      Parameters:
      collapsePropertyValue - BorderCollapsePropertyValue to be set as the border collapse type
      Returns:
      this Table
    • setHorizontalBorderSpacing

      public Table setHorizontalBorderSpacing (float spacing)
      Sets the horizontal spacing between this table's cells.
      Parameters:
      spacing - a horizontal spacing between this table's cells
      Returns:
      this Table
    • setVerticalBorderSpacing

      public Table setVerticalBorderSpacing (float spacing)
      Sets the vertical spacing between this table's cells.
      Parameters:
      spacing - a vertical spacing between this table's cells
      Returns:
      this Table
    • getAccessibilityProperties

      public AccessibilityProperties getAccessibilityProperties()
      Description copied from interface: IAccessibleElement
      Specified by:
      getAccessibilityProperties in interface IAccessibleElement
      Returns:
      an interface that allows to specify properties of a tagged element in Tagged PDF.
    • makeNewRenderer

      protected IRenderer makeNewRenderer()
      Description copied from class: AbstractElement
      Creates new renderer instance.
      Specified by:
      makeNewRenderer in class AbstractElement<Table>
      Returns:
      new IRenderer
    • getRowGroups

      protected List<Table.RowRange> getRowGroups()
      Returns the list of all row groups.
      Returns:
      a list of a Table.RowRange which holds the row numbers of a section of a table