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

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

Introduction

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

Prototype

@Internal
    public CTTc getCTTc() 

Source Link

Usage

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

License:Open Source License

/**
 * Inserts the given {@link MTable}./*from w  w w .j  a  v a2 s.c  o  m*/
 * 
 * @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 caseCell(Cell cell) {
    final XWPFTableCell newCell = currentGeneratedRow.createCell();
    final CTTc ctCell = (CTTc) cell.getTableCell().getCTTc().copy();
    ctCell.getPList().clear();//  ww w  .j  av  a 2 s  .  c  o m
    ctCell.getTblList().clear();
    newCell.getCTTc().set(ctCell);

    final IBody savedGeneratedDocument = generatedDocument;
    generatedDocument = newCell;
    try {
        doSwitch(cell.getTemplate());
    } finally {
        generatedDocument = savedGeneratedDocument;
    }

    return null;
}

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

License:Open Source License

/**
 * Create a table in a table cell.//w w  w  .ja v a  2 s. c om
 * 
 * @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  v a2 s.co  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);
}