com.lowagie.text.pdf
Class PdfPTable

java.lang.Object
  |
  +--com.lowagie.text.pdf.PdfPTable
All Implemented Interfaces:
Element

public class PdfPTable
extends Object
implements Element

This is a table that can be put at an absolute position but can also be added to the document as the class Table. In the last case when crossing pages the table always break at full rows; if a row is bigger than the page it is dropped silently to avoid infinite loops.

A PdfPTableEvent can be associated to the table to do custom drawing when the table is rendered.

Author:
Paulo Soares (psoares@consiste.pt)

Field Summary
protected  float[] absoluteWidths
           
static int BACKGROUNDCANVAS
          The index of the duplicate PdfContentByte where the backgroung will be drawn.
static int BASECANVAS
          The index of the original PdfcontentByte.
protected  PdfPCell[] currentRow
           
protected  int currentRowIdx
           
protected  PdfPCell defaultCell
           
protected  int headerRows
          Holds value of property headerRows.
private  int horizontalAlignment
          Holds value of property horizontalAlignment.
protected  boolean isColspan
           
static int LINECANVAS
          The index of the duplicate PdfContentByte where the border lines will be drawn.
protected  float[] relativeWidths
           
protected  ArrayList rows
           
protected  int runDirection
           
private  boolean skipFirstHeader
          Holds value of property skipFirstHeader.
protected  PdfPTableEvent tableEvent
           
static int TEXTCANVAS
          The index of the duplicate PdfContentByte where the text will be drawn.
protected  float totalHeight
           
protected  float totalWidth
           
protected  float widthPercentage
          Holds value of property widthPercentage.
 
Fields inherited from interface com.lowagie.text.Element
ALIGN_BASELINE, ALIGN_BOTTOM, ALIGN_CENTER, ALIGN_JUSTIFIED, 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, GRAPHIC, HEADER, IMGRAW, IMGTEMPLATE, JPEG, KEYWORDS, LIST, LISTITEM, PARAGRAPH, PHRASE, PRODUCER, PTABLE, RECTANGLE, ROW, SECTION, SUBJECT, TABLE, TITLE
 
Constructor Summary
PdfPTable(float[] relativeWidths)
          Constructs a PdfPTable with the relative column widths.
PdfPTable(int numColumns)
          Constructs a PdfPTable with numColumns columns.
PdfPTable(PdfPTable table)
          Constructs a copy of a PdfPTable.
 
Method Summary
 void addCell(Image image)
          Adds a cell element.
 void addCell(PdfPCell cell)
          Adds a cell element.
 void addCell(PdfPTable table)
          Adds a cell element.
 void addCell(Phrase phrase)
          Adds a cell element.
 void addCell(String text)
          Adds a cell element.
static PdfContentByte[] beginWritingRows(PdfContentByte canvas)
          Gets and initializes the 4 layers where the table is written to.
(package private)  void calculateHeights()
           
private  void calculateWidths()
           
 boolean deleteLastRow()
          Deletes the last row in the table.
 boolean deleteRow(int rowNumber)
          Deletes a row from the table.
static void endWritingRows(PdfContentByte[] canvases)
          Finishes writing the table.
 float[] getAbsoluteWidths()
          Gets the absolute sizes of each column width.
 ArrayList getChunks()
          Gets all the chunks in this element.
 PdfPCell getDefaultCell()
          Gets the default PdfPCell that will be used as reference for all the addCell methods except addCell(PdfPCell).
(package private)  float[][] getEventWidths(float xPos, int firstRow, int lastRow, boolean includeHeaders)
           
 float getHeaderHeight()
          Gets the height of the rows that constitute the header as defined by setHeaderRows().
 int getHeaderRows()
          Gets the number of the rows that constitute the header.
 int getHorizontalAlignment()
          Gets the horizontal alignment of the table relative to the page.
(package private)  PdfPRow getRow(int idx)
           
 float getRowHeight(int idx)
          Gets the height of a particular row.
 int getRunDirection()
           
 boolean getSkipFirstHeader()
          Getter for property skipFirstHeader.
 PdfPTableEvent getTableEvent()
          Gets the table event for this page.
 float getTotalHeight()
          Gets the total height of the table.
 float getTotalWidth()
          Gets the full width of the table.
 float getWidthPercentage()
          Gets the width percentage that the table will occupy in the page.
 boolean process(ElementListener listener)
          Processes the element by adding it (or the different parts) to an ElementListener.
 void setHeaderRows(int headerRows)
          Sets the number of the top rows that constitute the header.
 void setHorizontalAlignment(int horizontalAlignment)
          Sets the horizontal alignment of the table relative to the page.
 void setRunDirection(int runDirection)
           
 void setSkipFirstHeader(boolean skipFirstHeader)
          Skips the printing of the first header.
 void setTableEvent(PdfPTableEvent event)
          Sets the table event for this table.
 void setTotalWidth(float totalWidth)
          Sets the full width of the table.
 void setTotalWidth(float[] columnWidth)
          Sets the full width of the table from the absolute column width.
 void setWidthPercentage(float widthPercentage)
          Sets the width percentage that the table will occupy in the page.
 void setWidthPercentage(float[] columnWidth, Rectangle pageSize)
          Sets the percentage width of the table from the absolute column width.
 void setWidths(float[] relativeWidths)
          Sets the relative widths of the table.
 void setWidths(int[] relativeWidths)
          Sets the relative widths of the table.
 int size()
          Gets the number of rows in this table.
 int type()
          Gets the type of the text element.
 float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas)
          Writes the selected rows to the document.
 float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte[] canvases)
          Writes the selected rows to the document.
 float writeSelectedRows(int colStart, int colEnd, int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas)
          Writes the selected rows to the document.
 float writeSelectedRows(int colStart, int colEnd, int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte[] canvases)
          Writes the selected rows and columns to the document.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.lowagie.text.Element
toString
 

Field Detail

BASECANVAS

public static final int BASECANVAS
The index of the original PdfcontentByte.

See Also:
Constant Field Values

BACKGROUNDCANVAS

public static final int BACKGROUNDCANVAS
The index of the duplicate PdfContentByte where the backgroung will be drawn.

See Also:
Constant Field Values

LINECANVAS

public static final int LINECANVAS
The index of the duplicate PdfContentByte where the border lines will be drawn.

See Also:
Constant Field Values

TEXTCANVAS

public static final int TEXTCANVAS
The index of the duplicate PdfContentByte where the text will be drawn.

See Also:
Constant Field Values

rows

protected ArrayList rows

totalHeight

protected float totalHeight

currentRow

protected PdfPCell[] currentRow

currentRowIdx

protected int currentRowIdx

defaultCell

protected PdfPCell defaultCell

totalWidth

protected float totalWidth

relativeWidths

protected float[] relativeWidths

absoluteWidths

protected float[] absoluteWidths

tableEvent

protected PdfPTableEvent tableEvent

headerRows

protected int headerRows
Holds value of property headerRows.


widthPercentage

protected float widthPercentage
Holds value of property widthPercentage.


horizontalAlignment

private int horizontalAlignment
Holds value of property horizontalAlignment.


skipFirstHeader

private boolean skipFirstHeader
Holds value of property skipFirstHeader.


isColspan

protected boolean isColspan

runDirection

protected int runDirection
Constructor Detail

PdfPTable

public PdfPTable(float[] relativeWidths)
Constructs a PdfPTable with the relative column widths.

Parameters:
relativeWidths - the relative column widths

PdfPTable

public PdfPTable(int numColumns)
Constructs a PdfPTable with numColumns columns.

Parameters:
numColumns - the number of columns

PdfPTable

public PdfPTable(PdfPTable table)
Constructs a copy of a PdfPTable.

Parameters:
table - the PdfPTable to be copied
Method Detail

setWidths

public void setWidths(float[] relativeWidths)
               throws DocumentException
Sets the relative widths of the table.

Parameters:
relativeWidths - the relative widths of the table.
Throws:
DocumentException - if the number of widths is different than tne number of columns

setWidths

public void setWidths(int[] relativeWidths)
               throws DocumentException
Sets the relative widths of the table.

Parameters:
relativeWidths - the relative widths of the table.
Throws:
DocumentException - if the number of widths is different than the number of columns

calculateWidths

private void calculateWidths()

setTotalWidth

public void setTotalWidth(float totalWidth)
Sets the full width of the table.

Parameters:
totalWidth - the full width of the table.

setTotalWidth

public void setTotalWidth(float[] columnWidth)
                   throws DocumentException
Sets the full width of the table from the absolute column width.

Parameters:
columnWidth - the absolute width of each column
Throws:
DocumentException - if the number of widths is different than the number of columns

setWidthPercentage

public void setWidthPercentage(float[] columnWidth,
                               Rectangle pageSize)
                        throws DocumentException
Sets the percentage width of the table from the absolute column width.

Parameters:
columnWidth - the absolute width of each column
pageSize - the page size
DocumentException

getTotalWidth

public float getTotalWidth()
Gets the full width of the table.

Returns:
the full width of the table

calculateHeights

void calculateHeights()

getDefaultCell

public PdfPCell getDefaultCell()
Gets the default PdfPCell that will be used as reference for all the addCell methods except addCell(PdfPCell).

Returns:
default PdfPCell

addCell

public void addCell(PdfPCell cell)
Adds a cell element.

Parameters:
cell - the cell element

addCell

public void addCell(String text)
Adds a cell element.

Parameters:
text - the text for the cell

addCell

public void addCell(PdfPTable table)
Adds a cell element.

Parameters:
table - the table to be added to the cell

addCell

public void addCell(Image image)
Adds a cell element.

Parameters:
image - the Image to add to the table. This image will fit in the cell

addCell

public void addCell(Phrase phrase)
Adds a cell element.

Parameters:
phrase - the Phrase to be added to the cell

writeSelectedRows

public float writeSelectedRows(int rowStart,
                               int rowEnd,
                               float xPos,
                               float yPos,
                               PdfContentByte[] canvases)
Writes the selected rows to the document.

canvases is obtained from beginWritingRows().

Parameters:
rowStart - the first row to be written, zero index
rowEnd - the last row to be written + 1. If it is -1 all the rows to the end are written
xPos - the x write coodinate
yPos - the y write coodinate
canvases - an array of 4 PdfContentByte obtained from beginWrittingRows()
Returns:
the y coordinate position of the bottom of the last row
See Also:
beginWritingRows(com.lowagie.text.pdf.PdfContentByte)

writeSelectedRows

public float writeSelectedRows(int colStart,
                               int colEnd,
                               int rowStart,
                               int rowEnd,
                               float xPos,
                               float yPos,
                               PdfContentByte[] canvases)
Writes the selected rows and columns to the document. This method does not clip the columns; this is only important if there are columns with colspan at boundaries.

canvases is obtained from beginWritingRows().

The table event is only fired for complete rows.

Parameters:
colStart - the first column to be written, zero index
colEnd - the last column to be written + 1. If it is -1 all the columns to the end are written
rowStart - the first row to be written, zero index
rowEnd - the last row to be written + 1. If it is -1 all the rows to the end are written
xPos - the x write coodinate
yPos - the y write coodinate
canvases - an array of 4 PdfContentByte obtained from beginWrittingRows()
Returns:
the y coordinate position of the bottom of the last row
See Also:
beginWritingRows(com.lowagie.text.pdf.PdfContentByte)

writeSelectedRows

public float writeSelectedRows(int rowStart,
                               int rowEnd,
                               float xPos,
                               float yPos,
                               PdfContentByte canvas)
Writes the selected rows to the document.

Parameters:
rowStart - the first row to be written, zero index
rowEnd - the last row to be written + 1. If it is -1 all the rows to the end are written
xPos - the x write coodinate
yPos - the y write coodinate
canvas - the PdfContentByte where the rows will be written to
Returns:
the y coordinate position of the bottom of the last row

writeSelectedRows

public float writeSelectedRows(int colStart,
                               int colEnd,
                               int rowStart,
                               int rowEnd,
                               float xPos,
                               float yPos,
                               PdfContentByte canvas)
Writes the selected rows to the document. This method clips the columns; this is only important if there are columns with colspan at boundaries.

The table event is only fired for complete rows.

Parameters:
colStart - the first column to be written, zero index
colEnd - the last column to be written + 1. If it is -1 all the
rowStart - the first row to be written, zero index
rowEnd - the last row to be written + 1. If it is -1 all the rows to the end are written
xPos - the x write coodinate
yPos - the y write coodinate
canvas - the PdfContentByte where the rows will be written to
Returns:
the y coordinate position of the bottom of the last row

beginWritingRows

public static PdfContentByte[] beginWritingRows(PdfContentByte canvas)
Gets and initializes the 4 layers where the table is written to. The text or graphics are added to one of the 4 PdfContentByte returned with the following order:

The layers are placed in sequence on top of each other.

Parameters:
canvas - the PdfContentByte where the rows will be written to
Returns:
an array of 4 PdfContentByte
See Also:
writeSelectedRows(int, int, float, float, PdfContentByte[])

endWritingRows

public static void endWritingRows(PdfContentByte[] canvases)
Finishes writing the table.

Parameters:
canvases - the array returned by beginWritingRows()

size

public int size()
Gets the number of rows in this table.

Returns:
the number of rows in this table

getTotalHeight

public float getTotalHeight()
Gets the total height of the table.

Returns:
the total height of the table

getRowHeight

public float getRowHeight(int idx)
Gets the height of a particular row.

Parameters:
idx - the row index (starts at 0)
Returns:
the height of a particular row

getHeaderHeight

public float getHeaderHeight()
Gets the height of the rows that constitute the header as defined by setHeaderRows().

Returns:
the height of the rows that constitute the header

deleteRow

public boolean deleteRow(int rowNumber)
Deletes a row from the table.

Parameters:
rowNumber - the row to be deleted
Returns:
true if the row was deleted

deleteLastRow

public boolean deleteLastRow()
Deletes the last row in the table.

Returns:
true if the last row was deleted

getHeaderRows

public int getHeaderRows()
Gets the number of the rows that constitute the header.

Returns:
the number of the rows that constitute the header

setHeaderRows

public void setHeaderRows(int headerRows)
Sets the number of the top rows that constitute the header. This header has only meaning if the table is added to Document and the table crosses pages.

Parameters:
headerRows - the number of the top rows that constitute the header

getChunks

public ArrayList getChunks()
Gets all the chunks in this element.

Specified by:
getChunks in interface Element
Returns:
an ArrayList

type

public int type()
Gets the type of the text element.

Specified by:
type in interface Element
Returns:
a type

process

public boolean process(ElementListener listener)
Processes the element by adding it (or the different parts) to an ElementListener.

Specified by:
process in interface Element
Parameters:
listener - an ElementListener
Returns:
true if the element was processed successfully

getWidthPercentage

public float getWidthPercentage()
Gets the width percentage that the table will occupy in the page.

Returns:
the width percentage that the table will occupy in the page

setWidthPercentage

public void setWidthPercentage(float widthPercentage)
Sets the width percentage that the table will occupy in the page.

Parameters:
widthPercentage - the width percentage that the table will occupy in the page

getHorizontalAlignment

public int getHorizontalAlignment()
Gets the horizontal alignment of the table relative to the page.

Returns:
the horizontal alignment of the table relative to the page

setHorizontalAlignment

public void setHorizontalAlignment(int horizontalAlignment)
Sets the horizontal alignment of the table relative to the page. It only has meaning if the width precentage is less than 100%.

Parameters:
horizontalAlignment - the horizontal alignment of the table relative to the page

getRow

PdfPRow getRow(int idx)

setTableEvent

public void setTableEvent(PdfPTableEvent event)
Sets the table event for this table.

Parameters:
event - the table event for this table

getTableEvent

public PdfPTableEvent getTableEvent()
Gets the table event for this page.

Returns:
the table event for this page

getAbsoluteWidths

public float[] getAbsoluteWidths()
Gets the absolute sizes of each column width.

Returns:
he absolute sizes of each column width

getEventWidths

float[][] getEventWidths(float xPos,
                         int firstRow,
                         int lastRow,
                         boolean includeHeaders)

getSkipFirstHeader

public boolean getSkipFirstHeader()
Getter for property skipFirstHeader.

Returns:
Value of property skipFirstHeader.

setSkipFirstHeader

public void 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.

setRunDirection

public void setRunDirection(int runDirection)

getRunDirection

public int getRunDirection()