Example usage for org.apache.poi.xwpf.usermodel XWPFTableRow getTable

List of usage examples for org.apache.poi.xwpf.usermodel XWPFTableRow getTable

Introduction

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

Prototype

public XWPFTable getTable() 

Source Link

Usage

From source file:fr.opensagres.poi.xwpf.converter.core.styles.table.row.AbstractTableRowValueProvider.java

License:Open Source License

@Override
protected String[] getStyleID(XWPFTableRow row) {
    return new String[] { row.getTable().getStyleID() };
}

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

License:Open Source License

private void computeCellInfos(XWPFTableRow row) {
    if (nbColumns == null) {
        nbColumns = XWPFTableUtil.computeColWidths(row.getTable()).length;
    }//w w  w  .  ja  v  a2  s . c  o  m

    int rowIndex = table.getRows().indexOf(row);
    boolean firstRow = rowIndex == 0;
    boolean lastRow = rowIndex == (table.getRows().size() - 1);

    boolean firstCol = true;
    boolean lastCol = false;
    int cellIndex = 0;
    CTRow ctRow = row.getCtRow();
    XmlCursor c = ctRow.newCursor();
    c.selectPath("./*");
    while (c.toNextSelection()) {
        XmlObject o = c.getObject();
        if (o instanceof CTTc) {
            CTTc tc = (CTTc) o;
            XWPFTableCell cell = row.getTableCell(tc);
            cellIndex = getCellIndex(cellIndex, cell);
            lastCol = (cellIndex == nbColumns - 1);
            addCellInfo(cell, firstRow, lastRow, firstCol, lastCol);
            firstCol = false;
        } else if (o instanceof CTSdtCell) {
            // Fix bug of POI
            CTSdtCell sdtCell = (CTSdtCell) o;
            List<CTTc> tcList = sdtCell.getSdtContent().getTcList();
            for (CTTc ctTc : tcList) {
                XWPFTableCell cell = new XWPFTableCell(ctTc, row, row.getTable().getBody());
                cellIndex = getCellIndex(cellIndex, cell);
                lastCol = (cellIndex == nbColumns - 1);
                addCellInfo(row.getTableCell(ctTc), firstRow, lastRow, firstCol, lastCol);
                firstCol = false;
            }
        }
    }
    c.dispose();
}

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

License:Open Source License

protected void visitTableRow(XWPFTableRow row, float[] colWidths, T tableContainer, boolean firstRow,
        boolean lastRowIfNoneVMerge, int rowIndex, int rowsSize) throws Exception {

    boolean headerRow = stylesDocument.isTableRowHeader(row);
    startVisitTableRow(row, tableContainer, rowIndex, headerRow);

    int nbColumns = colWidths.length;
    // Process cell
    boolean firstCol = true;
    boolean lastCol = false;
    boolean lastRow = false;
    List<XWPFTableCell> vMergedCells = null;
    List<XWPFTableCell> cells = row.getTableCells();
    if (nbColumns > cells.size()) {
        // Columns number is not equal to cells number.
        // POI have a bug with
        // <w:tr w:rsidR="00C55C20">
        // <w:tc>
        // <w:tc>...
        // <w:sdt>
        // <w:sdtContent>
        // <w:tc> <= this tc which is a XWPFTableCell is not included in the row.getTableCells();

        firstCol = true;/*from w ww  .  j  a  va  2 s . c om*/
        int cellIndex = -1;
        int cellPtr = 0;
        CTRow ctRow = row.getCtRow();
        XmlCursor c = ctRow.newCursor();
        c.selectPath("./*");
        while (c.toNextSelection()) {
            XmlObject o = c.getObject();
            if (o instanceof CTTc) {
                CTTc tc = (CTTc) o;
                XWPFTableCell cell = row.getTableCell(tc);
                cellIndex = getCellIndex(cellIndex, cell);
                lastCol = (cellIndex == nbColumns);
                vMergedCells = getVMergedCells(cell, rowIndex, cellPtr);
                if (vMergedCells == null || vMergedCells.size() > 0) {
                    lastRow = isLastRow(lastRowIfNoneVMerge, rowIndex, rowsSize, vMergedCells);
                    visitCell(cell, tableContainer, firstRow, lastRow, firstCol, lastCol, rowIndex, cellPtr,
                            vMergedCells);
                }
                cellPtr++;
                firstCol = false;
            } else if (o instanceof CTSdtCell) {
                // Fix bug of POI
                CTSdtCell sdtCell = (CTSdtCell) o;
                List<CTTc> tcList = sdtCell.getSdtContent().getTcList();
                for (CTTc ctTc : tcList) {
                    XWPFTableCell cell = new XWPFTableCell(ctTc, row, row.getTable().getBody());
                    cellIndex = getCellIndex(cellIndex, cell);
                    lastCol = (cellIndex == nbColumns);
                    List<XWPFTableCell> rowCells = row.getTableCells();
                    if (!rowCells.contains(cell)) {
                        rowCells.add(cell);
                    }
                    vMergedCells = getVMergedCells(cell, rowIndex, cellPtr);
                    if (vMergedCells == null || vMergedCells.size() > 0) {
                        lastRow = isLastRow(lastRowIfNoneVMerge, rowIndex, rowsSize, vMergedCells);
                        visitCell(cell, tableContainer, firstRow, lastRow, firstCol, lastCol, rowIndex, cellPtr,
                                vMergedCells);
                    }
                    cellPtr++;
                    firstCol = false;
                }
            }
        }
        c.dispose();
    } else {
        // Column number is equal to cells number.
        for (int i = 0; i < cells.size(); i++) {
            lastCol = (i == cells.size() - 1);
            XWPFTableCell cell = cells.get(i);
            vMergedCells = getVMergedCells(cell, rowIndex, i);
            if (vMergedCells == null || vMergedCells.size() > 0) {
                lastRow = isLastRow(lastRowIfNoneVMerge, rowIndex, rowsSize, vMergedCells);
                visitCell(cell, tableContainer, firstRow, lastRow, firstCol, lastCol, rowIndex, i,
                        vMergedCells);
            }
            firstCol = false;
        }
    }

    endVisitTableRow(row, tableContainer, firstRow, lastRow, headerRow);
}

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();

    // 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);
    }/*from  w w  w. j a  v  a  2s .co m*/

    // 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 void startVisitTableRow(XWPFTableRow row, Object tableContainer, int rowIndex, boolean headerRow)
        throws Exception {

    // 1) create attributes
    // Create class attributes.
    XWPFTable table = row.getTable();
    AttributesImpl attributes = createClassAttribute(table.getStyleID());

    // 2) create element
    if (headerRow) {
        startElement(TH_ELEMENT, attributes);
    } else {//from   w  w w  . j  a v a  2  s.  co m
        startElement(TR_ELEMENT, attributes);
    }
}

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();
    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);
        }/*ww w .  j  a v  a 2s.c  o m*/

        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;
}