Converts Cells Horizontally Merged by Width in Table in Word Document to Cells Merged by CellFormat.HorizontalMerge Property using Java

I read the document and example. But In below case I can not get proper row, col in
| A1 | A2 | A3 | A4 |
|…B1…| B4 |
(style is humble, B1 merged 3 cells ( A1, A2, A3 ) )

I get row,col as below code.

int row = cell.getParentRow().getParentTable().indexOf(cell.getParentRow());
int col = cell.getParentRow().indexOf(cell);

“B4”'s col is 1. because row1’s child is just 2.
I want “B4”'s col is 3.

How can I get this info?
In other words, How can get info that “B1” is merged 3 cells?

I found this value is horizontal merge count cell.
Is it right? If it is right where is 3900 defined as constant?


I believe, the Table.ConvertToHorizontallyMergedCells Method will help you to achieve what you are looking for. Please check this sample document and try running the following code:

Java Code:

Document doc = new Document("E:\\Temp\\merged cells.docx");

Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);

for (Row row : table.getRows()) {
    for (Cell cell : row.getCells()) {

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

    if (isHorizontallyMerged && isVerticallyMerged)
        return "The cell at " + cellLocation + " is both horizontally and vertically merged";
    else if (isHorizontallyMerged)
        return "The cell at " + cellLocation + " is horizontally merged.";
    else if (isVerticallyMerged)
        return "The cell at " + cellLocation + " is vertically merged";
        return "The cell at " + cellLocation + " is not merged";

Ah Thank you very much!
I missing “table.convertToHorizontallyMergedCells();” That is key!


Thanks for your feedback. It is great that you were able to find what you were looking for. Please let us know any time you may have any further queries in future.