com.aspose.words

Class CellMerge

  • java.lang.Object
    • com.aspose.words.CellMerge
public class CellMerge 
extends java.lang.Object

Utility class containing constants. Specifies how a cell in a table is merged with other cells.

Example:

Creates a table with two rows with cells in the first row horizontally merged.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
builder.write("Text in merged cells.");

builder.insertCell();
// This cell is merged to the previous and should be empty.
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
builder.endRow();

builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
builder.write("Text in one cell.");

builder.insertCell();
builder.write("Text in another cell.");
builder.endRow();
builder.endTable();

Example:

Creates a table with two columns with cells merged vertically in the first column.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
builder.write("Text in merged cells.");

builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
builder.write("Text in one cell");
builder.endRow();

builder.insertCell();
// This cell is vertically merged to the cell above and should be empty.
builder.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);

builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
builder.write("Text in another cell");
builder.endRow();
builder.endTable();

Example:

Prints the horizontal and vertical merge type of a cell.
public void checkCellsMerged() throws Exception
{
    Document doc = new Document(getMyDir() + "Table.MergedCells.doc");

    // Retrieve the first table in the document.
    Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);

    for (Row row : table.getRows())
    {
        for (Cell cell : row.getCells())
        {
            System.out.println(printCellMergeType(cell));
        }
    }

}

public String printCellMergeType(Cell cell)
{
    boolean isHorizontallyMerged = cell.getCellFormat().getHorizontalMerge() != CellMerge.NONE;
    boolean isVerticallyMerged = cell.getCellFormat().getVerticalMerge() != CellMerge.NONE;
    String cellLocation = MessageFormat.format("R{0}, C{1}", cell.getParentRow().getParentTable().indexOf(cell.getParentRow()) + 1, cell.getParentRow().indexOf(cell) + 1);

    if (isHorizontallyMerged && isVerticallyMerged)
        return MessageFormat.format("The cell at {0} is both horizontally and vertically merged", cellLocation);
    else if (isHorizontallyMerged)
        return MessageFormat.format("The cell at {0} is horizontally merged.", cellLocation);
    else if (isVerticallyMerged) return MessageFormat.format("The cell at {0} is vertically merged", cellLocation);
    else return MessageFormat.format("The cell at {0} is not merged", cellLocation);
}

Field Summary
static final intNONE = 0
The cell is not merged.
static final intFIRST = 1
The cell is the first cell in a range of merged cells.
static final intPREVIOUS = 2
The cell is merged to the previous cell horizontally or vertically.
 

    • Field Detail

      • NONE = 0

        public static final int NONE
        The cell is not merged.
      • FIRST = 1

        public static final int FIRST
        The cell is the first cell in a range of merged cells.
      • PREVIOUS = 2

        public static final int PREVIOUS
        The cell is merged to the previous cell horizontally or vertically.