@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);
}