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

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

Introduction

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

Prototype

public void insertTable(int pos, XWPFTable table) 

Source Link

Document

inserts an existing XWPFTable to the arrays bodyElements and tables

Usage

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Inserts the given {@link MTable}./*from   www .  jav a  2  s. com*/
 * 
 * @param run
 *            the {@link XWPFRun} to insert to
 * @param table
 *            the {@link MTable} to insert
 */
private void insertMTable(XWPFRun run, MTable table) {
    final XWPFTable docTable;
    if (generatedDocument instanceof XWPFDocument) {
        if (table.getLabel() != null) {
            XWPFRun captionRun;
            captionRun = run;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
            captionRun.setText(table.getLabel());
            captionRun.setBold(true);
        }
        docTable = ((XWPFDocument) generatedDocument).createTable();
    } else if (generatedDocument instanceof XWPFHeaderFooter) {
        final XWPFHeaderFooter headerFooter = (XWPFHeaderFooter) generatedDocument;
        final int index = headerFooter._getHdrFtr().getTblArray().length;
        final CTTbl cttbl = headerFooter._getHdrFtr().insertNewTbl(index);
        docTable = new XWPFTable(cttbl, headerFooter);
        headerFooter.insertTable(index, docTable);
    } else if (generatedDocument instanceof XWPFTableCell) {
        XWPFTableCell tcell = (XWPFTableCell) generatedDocument;
        if (table.getLabel() != null) {
            final XWPFRun captionRun = run;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
            captionRun.setText(table.getLabel());
            captionRun.setBold(true);
        }
        CTTbl ctTbl = tcell.getCTTc().addNewTbl();
        docTable = new XWPFTable(ctTbl, tcell);
        int tableRank = tcell.getTables().size();
        tcell.insertTable(tableRank, docTable);
        // A paragraph is mandatory at the end of a cell, so let's always add one.
        tcell.addParagraph();
    } else {
        docTable = null;
        M2DocUtils.appendMessageRun((XWPFParagraph) run.getParent(), ValidationMessageLevel.ERROR,
                "m:table can't be inserted here.");
    }

    if (docTable != null) {
        fillTable(docTable, table);
    }
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

@Override
public IConstruct caseTable(Table table) {
    // Create the table structure in the destination document.

    CTTbl copy = (CTTbl) table.getTable().getCTTbl().copy();
    copy.getTrList().clear();/*from   w  w  w . j av a2 s .c om*/
    if (generatedDocument instanceof XWPFDocument) {
        currentGeneratedTable = ((XWPFDocument) generatedDocument).createTable();
        if (currentGeneratedTable.getRows().size() > 0) {
            currentGeneratedTable.removeRow(0);
        }
        currentGeneratedTable.getCTTbl().set(copy);
    } else if (generatedDocument instanceof XWPFHeaderFooter) {
        final XWPFHeaderFooter headerFooter = (XWPFHeaderFooter) generatedDocument;
        final int index = headerFooter._getHdrFtr().getTblArray().length;
        final CTTbl cttbl = headerFooter._getHdrFtr().insertNewTbl(index);
        XWPFTable newTable = new XWPFTable(cttbl, headerFooter);
        if (newTable.getRows().size() > 0) {
            newTable.removeRow(0);
        }
        headerFooter.insertTable(index, newTable);
        currentGeneratedTable = headerFooter.getTables().get(index);
        currentGeneratedTable.getCTTbl().set(copy);
    } else if (generatedDocument instanceof XWPFTableCell) {
        XWPFTableCell tCell = (XWPFTableCell) generatedDocument;
        int tableRank = tCell.getTables().size();
        XWPFTable newTable = new XWPFTable(copy, tCell, 0, 0);
        if (newTable.getRows().size() > 0) {
            newTable.removeRow(0);
        }
        tCell.insertTable(tableRank, newTable);
        currentGeneratedTable = tCell.getTables().get(tableRank);
    } else {
        throw new UnsupportedOperationException("unknown type of IBody : " + generatedDocument.getClass());
    }
    // iterate on the row
    for (Row row : table.getRows()) {
        doSwitch(row);
    }

    return table;
}

From source file:org.obeonetwork.m2doc.generator.TableClientProcessor.java

License:Open Source License

/**
 * Create a table in a table cell.//from  w ww.  j a  v  a  2 s . co  m
 * 
 * @param tableRun
 *            The table run
 * @param first
 *            whether it's the 1st table created in this cell
 * @param mtable
 *            The table description
 * @param tcell
 *            The cell in which to create a new table
 * @return The newly creatted table, located in the given cell.
 */
private XWPFTable createTableInCell(XWPFRun tableRun, boolean first, MTable mtable, XWPFTableCell tcell) {
    XWPFTable table;
    if (showTitle() && mtable.getLabel() != null) {
        XWPFRun captionRun;
        if (first) {
            captionRun = tableRun;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
        } else {
            XWPFParagraph captionParagraph = tcell.addParagraph();
            captionParagraph.setSpacingAfter(0);
            captionRun = captionParagraph.createRun();
        }
        captionRun.setText(mtable.getLabel());
        captionRun.setBold(true);
    }
    CTTbl ctTbl = tcell.getCTTc().addNewTbl();
    table = new XWPFTable(ctTbl, tcell);
    int tableRank = tcell.getTables().size();
    tcell.insertTable(tableRank, table);
    // A paragraph is mandatory at the end of a cell, so let's always add one.
    tcell.addParagraph();
    return table;
}

From source file:org.obeonetwork.m2doc.generator.TemplateProcessor.java

License:Open Source License

@Override
public AbstractConstruct caseTable(Table object) {
    // Create the table structure in the destination document.
    XWPFTable generatedTable;//from   ww w .  j a  va2 s .  c o m
    CTTbl copy = (CTTbl) object.getTable().getCTTbl().copy();
    copy.getTrList().clear();
    if (generatedDocument instanceof XWPFDocument) {
        generatedTable = ((XWPFDocument) generatedDocument).createTable();
        if (generatedTable.getRows().size() > 0) {
            generatedTable.removeRow(0);
        }
        generatedTable.getCTTbl().set(copy);
    } else if (generatedDocument instanceof XWPFTableCell) {
        XWPFTableCell tCell = (XWPFTableCell) generatedDocument;
        int tableRank = tCell.getTables().size();
        XWPFTable newTable = new XWPFTable(copy, tCell, 0, 0);
        if (newTable.getRows().size() > 0) {
            newTable.removeRow(0);
        }
        tCell.insertTable(tableRank, newTable);
        generatedTable = tCell.getTables().get(tableRank);
    } else {
        throw new UnsupportedOperationException("unknown type of IBody : " + generatedDocument.getClass());
    }
    // iterate on the row
    for (Row row : object.getRows()) {
        XWPFTableRow newRow = generatedTable.createRow();
        CTRow ctRow = (CTRow) row.getTableRow().getCtRow().copy();
        ctRow.getTcList().clear();
        newRow.getCtRow().set(ctRow);
        // iterate on cells.
        for (Cell cell : row.getCells()) {
            XWPFTableCell newCell = newRow.createCell();
            CTTc ctCell = (CTTc) cell.getTableCell().getCTTc().copy();
            ctCell.getPList().clear();
            ctCell.getTblList().clear();
            newCell.getCTTc().set(ctCell);
            // process the cell :
            TemplateProcessor processor = new TemplateProcessor(definitions, queryEnvironment, newCell);
            processor.doSwitch(cell.getTemplate());
        }
    }
    return super.caseTable(object);
}

From source file:org.obeonetwork.m2doc.generator.UserContentRawCopy.java

License:Open Source License

/**
 * Create new Table.//from   w  w  w  .j  a v  a 2s  .  c  o  m
 * TODO OHA fix bug in nested table on usercontent (else case in code)
 * 
 * @param document
 *            document
 * @param inputTable
 *            input Table
 * @return get Table
 */
private XWPFTable createNewTable(IBody document, XWPFTable inputTable) {
    XWPFTable generatedTable;
    CTTbl copy = (CTTbl) inputTable.getCTTbl().copy();
    if (document instanceof XWPFDocument) {
        generatedTable = ((XWPFDocument) document).createTable();
    } else if (document instanceof XWPFTableCell) {
        XWPFTableCell tCell = (XWPFTableCell) document;
        int tableRank = tCell.getTables().size();
        generatedTable = new XWPFTable(copy, tCell, 0, 0);
        tCell.insertTable(tableRank, generatedTable);
        generatedTable = tCell.getTables().get(tableRank);
    } else {
        throw new UnsupportedOperationException("unknown type of IBody : " + document.getClass());
    }
    return generatedTable;
}