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

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

Introduction

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

Prototype

public List<IBodyElement> getBodyElements() 

Source Link

Document

returns an Iterator with paragraphs and tables

Usage

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.
 * //  ww  w  . j ava  2 s .  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

protected void visitTableCellBody(XWPFTableCell cell, List<XWPFTableCell> vMergeCells, T tableCellContainer)
        throws Exception {
    if (vMergeCells != null) {

        for (XWPFTableCell mergedCell : vMergeCells) {
            List<IBodyElement> bodyElements = mergedCell.getBodyElements();
            visitBodyElements(bodyElements, tableCellContainer);
        }//from   w  w  w . j  av  a2 s.  co m
    } else {
        List<IBodyElement> bodyElements = cell.getBodyElements();
        visitBodyElements(bodyElements, tableCellContainer);
    }
}

From source file:org.articleEditor.insertContent.DocumentUpdater1.java

License:Apache License

DocumentPosition searchTable(XWPFTable table, int offset) throws BadLocationException {
    for (XWPFTableRow row : table.getRows()) {
        for (XWPFTableCell cell : row.getTableCells()) {
            DocumentPosition position = searchPart(cell.getBodyElements(), offset);
            if (position != null) {
                return position;
            }/*from  ww  w.j a  v  a  2 s.  c o  m*/
        }
    }
    return null;
}

From source file:org.articleEditor.insertContent.POIDocxReader.java

License:Apache License

protected void processTable(XWPFTable table) throws BadLocationException {
    int rowCount = table.getNumberOfRows();
    int columnCount = 0;
    for (XWPFTableRow row : table.getRows()) {
        columnCount = Math.max(columnCount, row.getTableCells().size());
    }//from w  w  w.  j a v  a2s.c  o m
    int[] rowsHeight = new int[rowCount];
    int[] columnsWidth = new int[columnCount];
    for (int i = 0; i < rowCount; i++) {
        rowsHeight[i] = 1;
    }
    for (int i = 0; i < columnCount; i++) {
        List<CTTblGridCol> colList = table.getCTTbl().getTblGrid().getGridColList();
        columnsWidth[i] = colList.get(i).getW().intValue() / INDENTS_MULTIPLIER;
    }
    SimpleAttributeSet tableAttrs = new SimpleAttributeSet();
    document.insertTable(currentOffset, rowCount, columnCount, tableAttrs, columnsWidth, rowsHeight);
    for (XWPFTableRow row : table.getRows()) {
        for (XWPFTableCell cell : row.getTableCells()) {
            iteratePart(cell.getBodyElements());
            currentOffset++;
        }
    }
}