Example usage for org.apache.poi.xwpf.usermodel XWPFTableCell getTableRow

List of usage examples for org.apache.poi.xwpf.usermodel XWPFTableCell getTableRow

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFTableCell getTableRow.

Prototype

public XWPFTableRow getTableRow() 

Source Link

Usage

From source file:fr.opensagres.poi.xwpf.converter.core.styles.AbstractValueProvider.java

License:Open Source License

public Value getValueFromStyles(XWPFElement element, XWPFStylesDocument stylesDocument) {

    // 1) At first get from cache or compute the default value
    String key = getKey(element, stylesDocument, null, null);
    // search from the cache
    Object defaultValue = stylesDocument.getValue(key);
    if (defaultValue == null) {
        // compute the default value and cache it
        defaultValue = getDefaultValue(element, stylesDocument);
        if (defaultValue == null) {
            defaultValue = XWPFStylesDocument.EMPTY_VALUE;
        }//from   w  w  w  . j  a  va 2 s  .  c  o  m
        updateValueCache(stylesDocument, key, defaultValue);
    }

    // 2) Search value from the linked style
    Object result = getValueFromStyleIds(element, stylesDocument, defaultValue);
    if (result != null) {
        return getValueOrNull(result);
    }
    // 3) Search if the XWPF element (paragraph or run) belongs to table which is styled.
    XWPFTableCell cell = getParentTableCell(element);
    if (cell != null) {
        XWPFTable table = cell.getTableRow().getTable();
        String tableStyleID = table.getStyleID();
        if (StringUtils.isNotEmpty(tableStyleID)) {
            // the current XWPFElement paragraph, run, etc belongs to a cell of a table which is styled.

            // 1) search styles from <w:style w:type="table" w:styleId="XXX"><w:tblStylePr
            // w:type="firstRow">, <w:tblStylePr w:type="lastRow">, etc
            TableCellInfo cellInfo = stylesDocument.getTableCellInfo(cell);
            result = getValueFromTableStyleId(element, stylesDocument, tableStyleID, cellInfo);
            if (result != null) {
                return getValueOrNull(result);
            }
            // no styles founded, search from the <w:style w:type="table" w:styleId="XXXX">
            result = getValueFromStyleId(element, stylesDocument, tableStyleID, defaultValue);
            if (result != null) {
                return getValueOrNull(result);
            }
        }
    }
    updateValueCache(stylesDocument, key, defaultValue);
    return getValueOrNull(defaultValue);
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.TableInfo.java

License:Open Source License

public TableCellInfo getCellInfo(XWPFTableCell cell) {
    TableCellInfo cellInfo = cellInfos.get(cell);
    if (cellInfo != null) {
        return cellInfo;
    }/*  w ww.ja  v  a2s  .co m*/
    // Compute it
    computeCellInfos(cell.getTableRow());
    return cellInfos.get(cell);

}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.XWPFStylesDocument.java

License:Open Source License

/**
 * Returns the table cell borders with conflicts.
 * /*from w  w w.java 2 s . com*/
 * @param cell
 * @param borderSide
 * @return
 * @see http://officeopenxml.com/WPtableCellBorderConflicts.php
 */
public TableCellBorder getTableCellBorderWithConflicts(XWPFTableCell cell, BorderSide borderSide) {
    /**
     * Conflicts between cell borders and table and table-level exception borders If the cell spacing is zero, then
     * there is a conflict. The following rules apply as between cell borders and table and table-level exception
     * (row) borders (Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference
     * 17.4.40.):
     */

    /**
     * 1) If there is a cell border, then the cell border is displayed.
     */

    /**
     * 2) If there is no cell border but there is a table-level exception border on the row, then that table-level
     * exception border is displayed.
     */

    TableCellBorder border = getTableCellBorder(cell, borderSide);
    if (border == null) {
        XWPFTable table = cell.getTableRow().getTable();
        boolean borderInside = isBorderInside(cell, borderSide);
        if (borderInside) {
            border = getTableCellBorderInside(cell, borderSide);
            if (border == null) {
                border = getTableBorderInside(table, borderSide);
            }
        }
        if (border == null && !borderInside) {
            /**
             * 3) If there is no cell or table-level exception border, then the table border is displayed.
             */
            border = getTableBorder(table, borderSide);
        }
    }
    return border;
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.XWPFStylesDocument.java

License:Open Source License

public TableCellInfo getTableCellInfo(XWPFTableCell cell) {
    XWPFTable table = cell.getTableRow().getTable();
    return getTableInfo(table).getCellInfo(cell);
}

From source file:fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.java

License:Open Source License

/**
 * Returns true if the given paragraph which is empty (none <w:r> run) must generate new line and false otherwise.
 * /*from   w  ww . j a  v a 2s  . co m*/
 * @param paragraph
 * @param index
 * @return
 */
private boolean isAddNewLine(XWPFParagraph paragraph, int index) {
    // a new line must be generated if :
    // - there is next paragraph/table
    // - if the body is a cell (with none vMerge) and contains just this paragraph
    IBody body = paragraph.getBody();
    List<IBodyElement> bodyElements = body.getBodyElements();
    if (body.getPartType() == BodyType.TABLECELL && bodyElements.size() == 1) {
        XWPFTableCell cell = (XWPFTableCell) body;
        STMerge.Enum vMerge = stylesDocument.getTableCellVMerge(cell);
        if (vMerge != null && vMerge.equals(STMerge.CONTINUE)) {
            // here a new line must not be generated because the body is a cell (with none vMerge) and contains just
            // this paragraph
            return false;
        }
        // Loop for each cell of the row : if all cells are empty, new line must be generated otherwise none empty
        // line must be generated.
        XWPFTableRow row = cell.getTableRow();
        List<XWPFTableCell> cells = row.getTableCells();
        for (XWPFTableCell c : cells) {
            if (c.getBodyElements().size() != 1) {
                return false;
            }
            IBodyElement element = c.getBodyElements().get(0);
            if (element.getElementType() != BodyElementType.PARAGRAPH) {
                return false;
            }
            return ((XWPFParagraph) element).getRuns().size() == 0;
        }
        return true;

    }
    // here a new line must be generated if there is next paragraph/table
    return bodyElements.size() > index + 1;
}

From source file:fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.java

License:Open Source License

private List<XWPFTableCell> getVMergedCells(XWPFTableCell cell, int rowIndex, int cellIndex) {
    List<XWPFTableCell> vMergedCells = null;
    STMerge.Enum vMerge = stylesDocument.getTableCellVMerge(cell);
    if (vMerge != null) {
        if (vMerge.equals(STMerge.RESTART)) {
            // vMerge="restart"
            // Loop for each table cell of each row upon vMerge="restart" was found or cell without vMerge
            // was declared.
            vMergedCells = new ArrayList<XWPFTableCell>();
            vMergedCells.add(cell);/*  www  . j  a  v a2s. c om*/

            XWPFTableRow row = null;
            XWPFTableCell c;
            XWPFTable table = cell.getTableRow().getTable();
            for (int i = rowIndex + 1; i < table.getRows().size(); i++) {
                row = table.getRow(i);
                c = row.getCell(cellIndex);
                if (c == null) {
                    break;
                }
                vMerge = stylesDocument.getTableCellVMerge(c);
                if (vMerge != null && vMerge.equals(STMerge.CONTINUE)) {

                    vMergedCells.add(c);
                } else {
                    return vMergedCells;
                }
            }
        } else {
            // vMerge="continue", ignore the cell because it was already processed
            return Collections.emptyList();
        }
    }
    return vMergedCells;
}

From source file:fr.opensagres.poi.xwpf.converter.pdf.internal.PdfMapper.java

License:Open Source License

@Override
protected IITextContainer startVisitTableCell(final XWPFTableCell cell, IITextContainer pdfTableContainer,
        boolean firstRow, boolean lastRow, boolean firstCol, boolean lastCol, List<XWPFTableCell> vMergeCells)
        throws Exception {
    XWPFTableRow row = cell.getTableRow();
    XWPFTable table = row.getTable();//  w  ww.  j  av  a  2 s  .c om

    // 1) store table cell info
    stylesDocument.getTableInfo(table).addCellInfo(cell, firstRow, lastRow, firstCol, lastCol);

    // 2) create PDF cell
    StylableTable pdfPTable = (StylableTable) pdfTableContainer;

    StylableTableCell pdfPCell = pdfDocument.createTableCell(pdfPTable);
    // pdfPCell.setUseAscender( true );
    // pdfPCell.setUseDescender( true );

    XWPFTableCell lastVMergedCell = null;
    if (vMergeCells != null) {
        pdfPCell.setRowspan(vMergeCells.size());
        lastVMergedCell = vMergeCells.get(vMergeCells.size() - 1);
        stylesDocument.getTableInfo(table).addCellInfo(lastVMergedCell, false, lastRow, firstCol, lastCol);
    }

    // border-top
    TableCellBorder borderTop = stylesDocument.getTableCellBorderWithConflicts(cell, BorderSide.TOP);
    if (borderTop != null) {
        boolean borderTopInside = stylesDocument.isBorderInside(cell, BorderSide.TOP);
        if (borderTopInside) {
            // Manage conflict border with the adjacent border bottom

        }
    }

    pdfPCell.setBorderTop(borderTop, false);

    // border-bottom

    XWPFTableCell theCell = lastVMergedCell != null ? lastVMergedCell : cell;
    TableCellBorder borderBottom = stylesDocument.getTableCellBorderWithConflicts(theCell, BorderSide.BOTTOM);
    pdfPCell.setBorderBottom(borderBottom, stylesDocument.isBorderInside(theCell, BorderSide.BOTTOM));

    // border-left
    TableCellBorder borderLeft = stylesDocument.getTableCellBorderWithConflicts(cell, BorderSide.LEFT);
    pdfPCell.setBorderLeft(borderLeft, stylesDocument.isBorderInside(cell, BorderSide.LEFT));

    // border-right
    TableCellBorder borderRight = stylesDocument.getTableCellBorderWithConflicts(cell, BorderSide.RIGHT);
    pdfPCell.setBorderRight(borderRight, stylesDocument.isBorderInside(cell, BorderSide.RIGHT));

    // Text direction <w:textDirection
    CTTextDirection direction = stylesDocument.getTextDirection(cell);
    if (direction != null) {
        int dir = direction.getVal().intValue();
        switch (dir) {
        case STTextDirection.INT_BT_LR:
            pdfPCell.setRotation(90);
            break;
        case STTextDirection.INT_TB_RL:
            pdfPCell.setRotation(270);
            break;
        }
    }

    // Colspan
    BigInteger gridSpan = stylesDocument.getTableCellGridSpan(cell);
    if (gridSpan != null) {
        pdfPCell.setColspan(gridSpan.intValue());
    }

    // Background Color
    Color backgroundColor = stylesDocument.getTableCellBackgroundColor(cell);
    if (backgroundColor != null) {
        pdfPCell.setBackgroundColor(Converter.toBaseColor(backgroundColor));
    }

    // Vertical aligment
    Enum jc = stylesDocument.getTableCellVerticalAlignment(cell);
    if (jc != null) {
        switch (jc.intValue()) {
        case STVerticalJc.INT_BOTTOM:
            pdfPCell.setVerticalAlignment(Element.ALIGN_BOTTOM);
            break;
        case STVerticalJc.INT_CENTER:
            pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            break;
        case STVerticalJc.INT_TOP:
            pdfPCell.setVerticalAlignment(Element.ALIGN_TOP);
            break;
        }
    }
    // Cell margin
    Float marginTop = stylesDocument.getTableCellMarginTop(cell);
    if (marginTop == null) {
        marginTop = stylesDocument.getTableRowMarginTop(row);
        if (marginTop == null) {
            marginTop = stylesDocument.getTableMarginTop(table);
        }
    }
    if (marginTop != null) {
        pdfPCell.setPaddingTop(marginTop);
    }
    Float marginBottom = stylesDocument.getTableCellMarginBottom(cell);
    if (marginBottom == null) {
        marginBottom = stylesDocument.getTableRowMarginBottom(row);
        if (marginBottom == null) {
            marginBottom = stylesDocument.getTableMarginBottom(table);
        }
    }
    if (marginBottom != null && marginBottom > 0) {
        pdfPCell.setPaddingBottom(marginBottom);
    }
    Float marginLeft = stylesDocument.getTableCellMarginLeft(cell);
    if (marginLeft == null) {
        marginLeft = stylesDocument.getTableRowMarginLeft(row);
        if (marginLeft == null) {
            marginLeft = stylesDocument.getTableMarginLeft(table);
        }
    }
    if (marginLeft != null) {
        pdfPCell.setPaddingLeft(marginLeft);
    }
    Float marginRight = stylesDocument.getTableCellMarginRight(cell);
    if (marginRight == null) {
        marginRight = stylesDocument.getTableRowMarginRight(row);
        if (marginRight == null) {
            marginRight = stylesDocument.getTableMarginRight(table);
        }
    }
    if (marginRight != null) {
        pdfPCell.setPaddingRight(marginRight);
    }

    // Row height
    TableHeight tableHeight = stylesDocument.getTableRowHeight(row);
    if (tableHeight != null) {
        if (tableHeight.minimum) {
            pdfPCell.setMinimumHeight(tableHeight.height);
        } else {
            pdfPCell.setFixedHeight(tableHeight.height);
        }
    }
    // No wrap
    Boolean noWrap = stylesDocument.getTableCellNoWrap(cell);
    if (noWrap != null) {
        pdfPCell.setNoWrap(noWrap);
    }
    return pdfPCell;
}

From source file:fr.opensagres.poi.xwpf.converter.xhtml.internal.XHTMLMapper.java

License:Open Source License

@Override
protected Object startVisitTableCell(XWPFTableCell cell, Object tableContainer, boolean firstRow,
        boolean lastRow, boolean firstCell, boolean lastCell, List<XWPFTableCell> vMergeCells)
        throws Exception {
    // 1) create attributes
    // 1.1) Create class attributes.
    XWPFTableRow row = cell.getTableRow();
    XWPFTable table = row.getTable();/*  www  .  ja va  2  s .c  o m*/
    AttributesImpl attributes = createClassAttribute(table.getStyleID());

    // 1.2) Create "style" attributes.
    CTTcPr tcPr = cell.getCTTc().getTcPr();
    CSSStyle cssStyle = getStylesDocument().createCSSStyle(tcPr);
    //At lease support solid borders for now
    if (cssStyle != null) {
        TableCellBorder border = getStylesDocument().getTableBorder(table, BorderSide.TOP);
        if (border != null) {
            String style = border.getBorderSize() + "px solid "
                    + StringUtils.toHexString(border.getBorderColor());
            cssStyle.addProperty(CSSStylePropertyConstants.BORDER_TOP, style);
        }

        border = getStylesDocument().getTableBorder(table, BorderSide.BOTTOM);
        if (border != null) {
            String style = border.getBorderSize() + "px solid "
                    + StringUtils.toHexString(border.getBorderColor());
            cssStyle.addProperty(CSSStylePropertyConstants.BORDER_BOTTOM, style);
        }

        border = getStylesDocument().getTableBorder(table, BorderSide.LEFT);
        if (border != null) {
            String style = border.getBorderSize() + "px solid "
                    + StringUtils.toHexString(border.getBorderColor());
            cssStyle.addProperty(CSSStylePropertyConstants.BORDER_LEFT, style);
        }

        border = getStylesDocument().getTableBorder(table, BorderSide.RIGHT);
        if (border != null) {
            String style = border.getBorderSize() + "px solid "
                    + StringUtils.toHexString(border.getBorderColor());
            cssStyle.addProperty(CSSStylePropertyConstants.BORDER_RIGHT, style);
        }
    }
    attributes = createStyleAttribute(cssStyle, attributes);

    // colspan attribute
    BigInteger gridSpan = stylesDocument.getTableCellGridSpan(cell);
    if (gridSpan != null) {
        attributes = SAXHelper.addAttrValue(attributes, COLSPAN_ATTR, gridSpan.intValue());
    }

    if (vMergeCells != null) {
        attributes = SAXHelper.addAttrValue(attributes, ROWSPAN_ATTR, vMergeCells.size());
    }

    // 2) create element
    startElement(TD_ELEMENT, attributes);

    return null;
}