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

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

Introduction

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

Prototype

public List<XWPFParagraph> getParagraphs() 

Source Link

Document

returns a list of paragraphs

Usage

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

License:Open Source License

/**
 * Initialize properrly a new table cell to make it easy to insert the data in this cell. Deals with the style to apply to this cell
 * depending on the row and column it belongs to.
 * /*from  ww w  .  j a v a 2s .c  o  m*/
 * @param cell
 *            Newly created cell to initialize
 * @param row
 *            The row the cell belongs to, the style of which will be used for the cell if defined.
 * @param column
 *            The column the cell belongs to, the style of which will be used for the cell if defined and the row's style is
 *            <code>null</code>.
 * @return The paragraph of the cell after initialization.
 */
private XWPFParagraph initializeEmptyTableCell(XWPFTableCell cell, MRow row, MColumn column) {
    XWPFParagraph cellParagraph = cell.getParagraphs().get(0);
    cellParagraph.setSpacingBefore(0);
    cellParagraph.setSpacingAfter(0);
    MStyle style = row == null ? null : row.getStyle();
    if (style == null) {
        style = column == null ? null : column.getStyle();
    }
    if (style != null) {
        cell.setColor(hexColor(style.getBackgroundColor()));
    }
    return cellParagraph;
}

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

License:Open Source License

/**
 * Create a new run in the cell's paragraph and set this run's text, and apply the given style to the cell and its paragraph.
 * //www  . ja v  a2  s.c om
 * @param cell
 *            Cell to fill in
 * @param text
 *            Text to set in the cell
 * @param style
 *            Style to use, can be <code>null</code>
 */
private void setCellContent(XWPFTableCell cell, String text, MStyle style) {
    XWPFParagraph cellParagraph = cell.getParagraphs().get(0);
    XWPFRun cellRun = cellParagraph.createRun();
    cellRun.setText(text);
    if (style != null) {
        cell.setColor(hexColor(style.getBackgroundColor()));
        applyTableClientStyle(cellRun, style);
    }
}

From source file:org.obeonetwork.m2doc.generator.test.TableClientProcessorWithStyleTest.java

License:Open Source License

@Test
@Override//  w  ww  . j  a va 2  s .c o m
public void testWithoutAnyParameter() throws ProviderException {
    super.testWithoutAnyParameter();

    // Check styles
    List<XWPFTable> tables = doc.getTables();
    XWPFTable table = tables.get(0);

    // First Row (header)
    XWPFTableRow row = table.getRow(0);

    // Header => no style
    XWPFTableCell cell = row.getCell(1);
    XWPFParagraph cellParagraph = cell.getParagraphs().get(0);
    XWPFRun cellRun = cellParagraph.getRuns().get(0);
    assertNull(cell.getColor());
    assertNull(cellRun.getColor());
    assertFalse(cellRun.isBold());
    assertFalse(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline());

    // Header => no style
    cell = row.getCell(2);
    cellParagraph = cell.getParagraphs().get(0);
    cellRun = cellParagraph.getRuns().get(0);
    assertNull(cell.getColor());
    assertNull(cellRun.getColor());
    assertFalse(cellRun.isBold());
    assertFalse(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline());

    // First row
    row = table.getRow(1);

    // first row, row header => no style
    cell = row.getCell(0);
    cellParagraph = cell.getParagraphs().get(0);
    cellRun = cellParagraph.getRuns().get(0);
    assertNull(cell.getColor());
    assertNull(cellRun.getColor());
    assertFalse(cellRun.isBold());
    assertFalse(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline());

    // first row, first cell => cell style
    cell = row.getCell(1);
    cellParagraph = cell.getParagraphs().get(0);
    cellRun = cellParagraph.getRuns().get(0);
    assertEquals(CELL_BG_COLOR, cell.getColor().toUpperCase());
    assertEquals(CELL_FG_COLOR, cellRun.getColor().toUpperCase());
    assertEquals(CELL_FONT_SIZE, cellRun.getFontSize());
    assertTrue(cellRun.isBold());
    assertFalse(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.SINGLE, cellRun.getUnderline());

    // first row, second cell => row style
    cell = row.getCell(2);
    cellParagraph = cell.getParagraphs().get(0);
    assertEquals(ROW_BG_COLOR, cell.getColor().toUpperCase());
    assertTrue(cellParagraph.getRuns().isEmpty());

    // Second row
    row = table.getRow(2);

    // second row, row header => no style
    cell = row.getCell(0);
    cellParagraph = cell.getParagraphs().get(0);
    cellRun = cellParagraph.getRuns().get(0);
    assertNull(cell.getColor());
    assertNull(cellRun.getColor());
    assertFalse(cellRun.isBold());
    assertFalse(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline());

    // second row, first cell => column style
    cell = row.getCell(1);
    cellParagraph = cell.getParagraphs().get(0);
    assertEquals(COL_BG_COLOR, cell.getColor().toUpperCase());
    assertTrue(cellParagraph.getRuns().isEmpty());

    // second row, second cell => cell style
    cell = row.getCell(2);
    cellParagraph = cell.getParagraphs().get(0);
    cellRun = cellParagraph.getRuns().get(0);
    assertEquals(CELL_BG_COLOR, cell.getColor().toUpperCase());
    assertEquals(CELL_FG_COLOR, cellRun.getColor().toUpperCase());
    assertEquals(CELL_FONT_SIZE, cellRun.getFontSize());
    assertTrue(cellRun.isBold());
    assertFalse(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.SINGLE, cellRun.getUnderline());
}

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

License:Open Source License

/**
 * Collect Relation Id in table.// w w w . ja v  a2 s  . c  o  m
 * Put picture in output document and keep old and new picture id in map.
 * 
 * @param inputWTable
 *            inputWTable
 * @param outputDoc
 *            outputDoc
 * @throws InvalidFormatException
 *             InvalidFormatException
 */
private void collectRelationId(XWPFTable inputWTable, XWPFDocument outputDoc) throws InvalidFormatException {
    for (XWPFTableRow row : inputWTable.getRows()) {
        for (XWPFTableCell cell : row.getTableCells()) {
            for (XWPFParagraph paragraph : cell.getParagraphs()) {
                collectRelationId(paragraph, outputDoc);
            }
        }
    }
}

From source file:org.obeonetwork.m2doc.tests.generator.TableClientProcessorWithStyleTests.java

License:Open Source License

@Test
@Override/*  www.  j  a  va2s. c  o m*/
public void testWithoutAnyParameter() throws ProviderException {
    super.testWithoutAnyParameter();

    // Check styles
    List<XWPFTable> tables = doc.getTables();
    XWPFTable table = tables.get(0);

    // First Row (header)
    XWPFTableRow row = table.getRow(0);

    // Header => col 1 has "column style"
    XWPFTableCell cell = row.getCell(1);
    XWPFParagraph cellParagraph = cell.getParagraphs().get(0);
    XWPFRun cellRun = cellParagraph.getRuns().get(0);
    assertEquals(COL_BG_COLOR, cell.getColor().toUpperCase());
    assertEquals(COL_FG_COLOR, cellRun.getColor().toUpperCase());
    assertEquals(COL_FONT_SIZE, cellRun.getFontSize());
    assertTrue(cellRun.isBold());
    assertFalse(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline());

    // Header => no style
    cell = row.getCell(2);
    cellParagraph = cell.getParagraphs().get(0);
    cellRun = cellParagraph.getRuns().get(0);
    assertNull(cell.getColor());
    assertNull(cellRun.getColor());
    assertFalse(cellRun.isBold());
    assertFalse(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline());

    // First row
    row = table.getRow(1);

    // first row, row header => no style
    cell = row.getCell(0);
    cellParagraph = cell.getParagraphs().get(0);
    cellRun = cellParagraph.getRuns().get(0);
    assertNull(cell.getColor());
    assertNull(cellRun.getColor());
    assertFalse(cellRun.isBold());
    assertFalse(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline());

    // first row, first cell => cell style
    cell = row.getCell(1);
    cellParagraph = cell.getParagraphs().get(0);
    cellRun = cellParagraph.getRuns().get(0);
    assertEquals(ROW_BG_COLOR, cell.getColor().toUpperCase());
    assertEquals(ROW_FG_COLOR, cellRun.getColor().toUpperCase());
    assertEquals(ROW_FONT_SIZE, cellRun.getFontSize());
    assertFalse(cellRun.isBold());
    assertTrue(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline());

    // first row, second cell => no style
    cell = row.getCell(2);
    cellParagraph = cell.getParagraphs().get(0);
    assertNull(cell.getColor());
    cellRun = cellParagraph.getRuns().get(0);
    assertFalse(cellRun.isBold());
    assertFalse(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline());

    // Second row
    row = table.getRow(2);

    // second row, row header => no style
    cell = row.getCell(0);
    cellParagraph = cell.getParagraphs().get(0);
    cellRun = cellParagraph.getRuns().get(0);
    assertNull(cell.getColor());
    assertNull(cellRun.getColor());
    assertFalse(cellRun.isBold());
    assertFalse(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline());

    // second row, first cell => no style
    cell = row.getCell(1);
    cellParagraph = cell.getParagraphs().get(0);
    assertNull(cell.getColor());
    cellRun = cellParagraph.getRuns().get(0);
    assertFalse(cellRun.isBold());
    assertFalse(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline());

    // second row, second cell => cell style
    cell = row.getCell(2);
    cellParagraph = cell.getParagraphs().get(0);
    cellRun = cellParagraph.getRuns().get(0);
    assertEquals(CELL_BG_COLOR, cell.getColor().toUpperCase());
    assertEquals(CELL_FG_COLOR, cellRun.getColor().toUpperCase());
    assertEquals(CELL_FONT_SIZE, cellRun.getFontSize());
    assertTrue(cellRun.isBold());
    assertFalse(cellRun.isItalic());
    assertFalse(cellRun.isStrikeThrough());
    assertEquals(UnderlinePatterns.SINGLE, cellRun.getUnderline());
}