getHorizontalMerge() always returns 0 (CellMerge.NONE) even when cell is merged

I am trying to determine how many cells are merged horizontally in a table, but cell.getCellFormat().getHorizontalMerge() always returns CellMerge.NONE, even when it comes across merged cells. getVerticalMerge() appears to work fine.

To reproduce, please run the following code on the attached document:

com.aspose.words.Document asposeDoc = new com.aspose.words.Document("testMergedCells.docx");
Table table = (Table)asposeDoc.getChild(NodeType.TABLE, 0, true);
int cellNum = 0;

for (Row row : table.getRows()) {
for (Cell cell : row.getCells()) {
cellNum++;
boolean isHorizontallyMerged = cell.getCellFormat().getHorizontalMerge() != CellMerge.NONE;
boolean isVerticallyMerged = cell.getCellFormat().getVerticalMerge() != CellMerge.NONE;

String msg = "Cell " + cellNum;

if(isHorizontallyMerged) {
msg += " Horizontally Merged";
}
if(isVerticallyMerged) {
msg += " Vertically Merged";
}

System.out.println(msg);
}
}

Any help much appreciated, thank you.

Hi Alex,


Thanks for your inquiry.

If you use the code from this article to print the horizontal and vertical merge types of cells in your table, you’ll find a few occurrences where it says that the cells are vertically merged.

Regarding Horizontally merged cells, please note that By Microsoft Word design, rows in a table in a Microsoft Word document are completely independent. It means each row can have any number of cells of any width. So if you imagine first row with one wide cell and second row with two narrow cells, then looking at this document the cell in the first row will appear horizontally merged. But it is not a merged cell; it is just a single wide cell. Another perfectly valid scenario is when the first row has two cells. First cell has CellMerge.First and second cell has CellMerge.Previous, in this case it is a merged cell. In both cases, the visual appearance in Microsoft Word is exactly the same. Both cases are valid and Microsoft Word treats merged Cells as one wide Cell.

If we can help you with anything else, please feel free to ask.

Best regards,

This thread helped me out, thanks. I managed to get horizontal cell merge to work by calculating columns using the cell.getCellFormat().getWidth().



However, this info really needs to be in the documentation, both in the API and in the guides. As it is, the docs for CellFormat.getHorizontalMerge() is wrong and misleading.

Hi Jacob,

It's great you were able to find what you were looking for. We have already mentioned about it in the documentation; I would suggest you please read the following article for more details:
http://www.aspose.com/docs/display/wordsjava/Working+with+Merged+Cells

However, we will also review you request of adding further information to the online API pages. Please let us know any time you have any further queries.

Best regards,