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:

Shows how to merge table cells horizontally.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a cell into the first column of the first row.
// This cell will be the first in a range of horizontally merged cells.
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
builder.write("Text in merged cells.");

// Insert a cell into the second column of the first row. Instead of adding text contents,
// we will merge this cell with the first cell that we added directly to the left, and end the row afterward.
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
builder.endRow();

// Insert two more unmerged cells to the second row.
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
builder.insertCell();
builder.write("Text in unmerged cell.");
builder.insertCell();
builder.write("Text in unmerged cell.");
builder.endRow();
builder.endTable();

doc.save(getArtifactsDir() + "CellFormat.HorizontalMerge.docx");

Example:

Shows how to merge table cells vertically.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a cell into the first column of the first row.
// This cell will be the first in a range of vertically merged cells.
builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
builder.write("Text in merged cells.");

// Insert a cell into the second column of the first row, then end the row.
// Also, configure the builder to disable vertical merging in created cells.
builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
builder.write("Text in unmerged cell.");
builder.endRow();

// Insert a cell into the first column of the second row. 
// Instead of adding text contents, we will merge this cell with the first cell that we added directly above.
builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);

// Insert another independent cell in the second column of the second row, and end the table.
builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
builder.write("Text in unmerged cell.");
builder.endRow();
builder.endTable();

doc.save(getArtifactsDir() + "CellFormat.VerticalMerge.docx");

Example:

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

    // 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(final 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.