Package com.itextpdf.tool.xml.html.table
Class Table
- java.lang.Object
-
- com.itextpdf.tool.xml.html.AbstractTagProcessor
-
- com.itextpdf.tool.xml.html.table.Table
-
- All Implemented Interfaces:
CssAppliersAware
,TagProcessor
public class Table extends AbstractTagProcessor
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
Table.NormalRowComparator
Reorganizes table rows based on the designated normalTableRowElement.Place
in a table.private class
Table.RepeatedRowComparator
Reorganizes table rows based on the designated repeatedTableRowElement.Place
in a table.
-
Field Summary
Fields Modifier and Type Field Description static float
DEFAULT_CELL_BORDER_WIDTH
private static FontSizeTranslator
fst
private static Logger
LOG
private static CssUtils
utils
-
Constructor Summary
Constructors Constructor Description Table()
Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private float
calculateDivWidestElementWidth(java.util.ArrayList<Element> divContent)
An attempt to calculate a valid div width in case it is not fixed.private float
calculateTargetWidth(Tag tag, float[] columnWidths, float outerWidth, WorkerContext ctx)
Calculates the target width.java.util.List<Element>
end(WorkerContext ctx, Tag tag, java.util.List<Element> currentContent)
Classes extending AbstractTagProcessor should override this method for actions that should be done inTagProcessor.endElement(WorkerContext, Tag, List)
.static float
getBorderOrCellSpacing(boolean getHor, java.util.Map<java.lang.String,java.lang.String> css, java.util.Map<java.lang.String,java.lang.String> attributes)
Extracts and parses the style border-spacing or the attribute cellspacing of a table tag, if present.private float
getCellStartWidth(HtmlCell cell)
Calculates the start width of a cell.private float
getTableOuterWidth(Tag tag, float horBorderSpacing, WorkerContext ctx)
Adds horizontal values of a table and its parent if present.private float
getTableWidth(float[] widths, float outerWidth)
Calculates the total width based on the given widths array and the given outer width.protected PdfPTable
intPdfPTable(int numberOfColumn)
boolean
isStackOwner()
Defaults to false.static TableStyleValues
setBorderAttributeForCell(Tag tag)
private float[]
setCellWidthAndWidestWord(HtmlCell cell)
Sets the default cell width and widest word of a cell.static TableStyleValues
setStyleValues(Tag tag)
Set the table style values in aTableStyleValues
object based on attributes and css of the given tag.private void
setVerticalMargin(PdfPTable table, Tag t, TableStyleValues values, WorkerContext ctx)
Sets the top and bottom margin of the given table.private void
widenLastCell(java.util.List<TableRowElement> tableRows, float horBorderSpacing)
Adds horizontal border spacing to the right padding of the last cell of each row.-
Methods inherited from class com.itextpdf.tool.xml.html.AbstractTagProcessor
content, createParagraph, currentContentToParagraph, currentContentToParagraph, doRtlIndentCorrections, endElement, getCssAppliers, getCSSResolver, getHtmlPipelineContext, getRunDirection, invertTextAlignForParagraph, invertTextAlignForParagraph, setCssAppliers, start, startElement, textContent, updateParagraphFontIfNeeded
-
-
-
-
Field Detail
-
DEFAULT_CELL_BORDER_WIDTH
public static final float DEFAULT_CELL_BORDER_WIDTH
- See Also:
- Constant Field Values
-
LOG
private static final Logger LOG
-
utils
private static final CssUtils utils
-
fst
private static final FontSizeTranslator fst
-
-
Method Detail
-
end
public java.util.List<Element> end(WorkerContext ctx, Tag tag, java.util.List<Element> currentContent)
Description copied from class:AbstractTagProcessor
Classes extending AbstractTagProcessor should override this method for actions that should be done inTagProcessor.endElement(WorkerContext, Tag, List)
. TheAbstractTagProcessor.endElement(WorkerContext, Tag, List)
calls this method after or before doing certain stuff, (see it's description).- Overrides:
end
in classAbstractTagProcessor
- Parameters:
ctx
- the WorkerContexttag
- the tagcurrentContent
- the content created from e.g. inner tags, inner content and not yet added to document.- Returns:
- a List containing iText Element objects
-
intPdfPTable
protected PdfPTable intPdfPTable(int numberOfColumn)
-
calculateTargetWidth
private float calculateTargetWidth(Tag tag, float[] columnWidths, float outerWidth, WorkerContext ctx) throws NoCustomContextException
Calculates the target width. First checks:- if the attribute or style "width" is found in the given tag and it is not wider than pageWidth - outerWidth, then the targetWidth = width value
- if the columnWidths array in total is not wider than pageWidth - outerWidth, then the targetWidth = the total of the columnWidths array
- if table's parent is a root tag or table has no parent, then the
targetWidth = width of the page - outerWidth
getTableOuterWidth(Tag, float, WorkerContext)
.
- Parameters:
tag
- containing attributes and css.columnWidths
- float[] containing the widest lines of text found in the columns.outerWidth
- width needed for margins and borders.ctx
-- Returns:
- float the target width of a table.
- Throws:
NoCustomContextException
-
widenLastCell
private void widenLastCell(java.util.List<TableRowElement> tableRows, float horBorderSpacing)
Adds horizontal border spacing to the right padding of the last cell of each row.- Parameters:
tableRows
- List ofTableRowElement
objects of the table.horBorderSpacing
- float containing the horizontal border spacing of the table.
-
setStyleValues
public static TableStyleValues setStyleValues(Tag tag)
Set the table style values in aTableStyleValues
object based on attributes and css of the given tag.- Parameters:
tag
- containing attributes and css.- Returns:
- a
TableStyleValues
object containing the table's style values.
-
setBorderAttributeForCell
public static TableStyleValues setBorderAttributeForCell(Tag tag)
-
getBorderOrCellSpacing
public static float getBorderOrCellSpacing(boolean getHor, java.util.Map<java.lang.String,java.lang.String> css, java.util.Map<java.lang.String,java.lang.String> attributes)
Extracts and parses the style border-spacing or the attribute cellspacing of a table tag, if present. Favors the style border-spacing over the attribute cellspacing.
If style="border-collapse:collapse" is found in the css, the spacing is always 0f.
If no spacing is set, the default of 1.5pt is returned.- Parameters:
getHor
- true for horizontal spacing, false for vertical spacing.css
- of the table tag.attributes
- of the table tag.- Returns:
- horizontal or vertical spacing between two cells or a cell and the border of the table.
-
setCellWidthAndWidestWord
private float[] setCellWidthAndWidestWord(HtmlCell cell)
Sets the default cell width and widest word of a cell.- cell width =
getCellStartWidth(HtmlCell)
+ the width of the widest line of text. - widest word =
getCellStartWidth(HtmlCell)
+ the widest word of the cell.
- Parameters:
cell
- HtmlCell of which the widths are needed.- Returns:
- float array containing the default cell width and the widest
word.
- float[0] = cell width.
- float[1] = widest word.
- cell width =
-
calculateDivWidestElementWidth
private float calculateDivWidestElementWidth(java.util.ArrayList<Element> divContent)
An attempt to calculate a valid div width in case it is not fixed. It is used as alternative to div.getActualWidth, which doesn't work here in case of not fixed div's width (it returns 0). This method is probably has to be improved in future. The main idea of this method is to return the widest element's width, so the created cell will be able to contain it.
-
getTableWidth
private float getTableWidth(float[] widths, float outerWidth) throws NoCustomContextException
Calculates the total width based on the given widths array and the given outer width.- Parameters:
widths
- array of floats containing column width values.outerWidth
- equals the required space outside of the table for margins and borders.- Returns:
- a table's width.
- Throws:
NoCustomContextException
-
getTableOuterWidth
private float getTableOuterWidth(Tag tag, float horBorderSpacing, WorkerContext ctx) throws NoCustomContextException
Adds horizontal values of a table and its parent if present. Following values are added up:- left and right margins of the table.
- left and right border widths of the table.
- left and right margins of the parent of the table is present.
- one horizontal border spacing.
- Parameters:
tag
-horBorderSpacing
-ctx
-- Returns:
- float containing the needed space for margins of table and parent(s) and the borders of the table.
- Throws:
NoCustomContextException
-
getCellStartWidth
private float getCellStartWidth(HtmlCell cell)
Calculates the start width of a cell. Following values are added up:- padding left, this includes left border width and a horizontal border spacing.
- padding right, this includes right border width.
- the (colspan - 1) * horizontal border spacing.
- Parameters:
cell
- HtmlCell of which the start width is needed.- Returns:
- float containing the start width.
-
setVerticalMargin
private void setVerticalMargin(PdfPTable table, Tag t, TableStyleValues values, WorkerContext ctx) throws NoCustomContextException
Sets the top and bottom margin of the given table.- Parameters:
table
- PdfPTable on which the margins need to be set.t
- Tag containing the margin styles and font size if needed.values
-TableStyleValues
containing border widths and border spacing values.ctx
-- Throws:
NoCustomContextException
-
isStackOwner
public boolean isStackOwner()
Description copied from class:AbstractTagProcessor
Defaults to false.- Specified by:
isStackOwner
in interfaceTagProcessor
- Overrides:
isStackOwner
in classAbstractTagProcessor
- Returns:
- true if the tag implementation must keep it's own currentContent stack.
- See Also:
TagProcessor.isStackOwner()
-
-