CellFormat.getHorizonatalMerge() does not work properly

Hi,
I am using Aspose.Words (Versions 21.4) to read word documents created using Microsoft Word. The issue is related to the horizontally merged table cells (merged using Micorsoft Word). When read, the merged cells appear to be one wide cell. The value returned from CellFormat.getHorizonatalMerge() is 0 for all the table cells.

Any suggestions or workaround to find out the merged cells is appreciated.

Many thanks and kind regards.

@m.aitwakrim

Please call Table.convertToHorizontallyMergedCells method as shown below to convert cells horizontally merged by width to cells merged by HorizontalMerge.

Document doc = new Document(MyDir + "input.docx");
Table table = doc.getFirstSection().getBody().getTables().get(0);
table.convertToHorizontallyMergedCells();
for (Row row : table.getRows()) {
    for (Cell cell : row.getCells()) {
        System.out.println(printCellMergeType(cell));
    }
}

public static 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);
    if (isHorizontallyMerged)
        return MessageFormat.format("The cell at {0} is horizontally merged.", cellLocation);

    return isVerticallyMerged ? MessageFormat.format("The cell at {0} is vertically merged", cellLocation) : MessageFormat.format("The cell at {0} is not merged", cellLocation);
}
1 Like

Thank you Tahir. This fixed the issue.

A post was split to a new topic: How to Convert cells horizontally merged by width to cells merged by Horizontal Merge