How to Convert Horizontally Merged Cells by Width to Cells Merged by HorizontalMerge using Java

Hi, Team.

I found out that com.aspose.words.Cell#getCellFormat#getHorizontalMerge returns incorrect number for some documents. For example, in document example.zip (17.3 KB) it returns 0 for all cells in the second row (with text: “Total Agricultural Products”, “Primary Agricultural Products”, “Processed Agricultural Products”). I have checked word XML. It containce correct data: <w:gridSpan w:val=“2”/>
It is really a problem for us because we cannot build the correct table according to incorrect data.
The version of Aspose.Word is 21.4.0.

Best regards,
Svetlana

@skorpusova

We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-22134. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@skorpusova

We have closed WORDSNET-22134 as ‘Not a Bug’. 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 + "example.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);
}

@tahir.manzoor

Thanks a lot. Now it works correctly.

Best regards,
Svetlana