HorizontalMerge & VerticalMerge

Hello,
We discovered a problem on a Aspose test where are used properties VerticalMerge and HorizontalMerge. These properties are not evaluated correctly. I attached the test file.

Best regards,
Adriana Minea
Software Developer
IBM Romania

I’m attaching the unit test used when we discovered the problem.
TestFile.docx is the document attached to this thread.

[TestMethod]
public void ThenMergeStatusAreCorrect()
{
    const string testFile = "TestFile.docx";
    var document = new Document(EmbeddedResourceHelper.GetStream(testFile, Assembly.GetExecutingAssembly()));

    var firstTable = document.FirstSection.Body.Tables.ToArray().First();
    var lastTable = document.FirstSection.Body.Tables.ToArray().Last();

    Assert.IsNotNull(firstTable);
    Assert.AreNotEqual(CellMerge.None, firstTable.FirstRow.Cells[0].CellFormat.HorizontalMerge);

    Assert.IsNotNull(lastTable);
    Assert.AreNotEqual(CellMerge.None, lastTable.FirstRow.Cells[0].CellFormat.VerticalMerge);

}

Hi Adriana,

Thanks for your inquiry.

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 MS Word is exactly the same. Both cases are valid. Please check following articles for more details:

Working with Merged Cells

Document doc = new Document(MyDir + @"AsposeID_1.docx");
foreach (Table tbl in doc.GetChildNodes(NodeType.Table, true))
{
    foreach (Row row in tbl)
    {
        foreach (Cell cell in row.Cells)
        {
            Console.WriteLine(PrintCellMergeType(cell));
        }
    }
    Console.WriteLine("---------------");
}
public static string PrintCellMergeType(Cell cell)
{
    bool isHorizontallyMerged = cell.CellFormat.HorizontalMerge != CellMerge.None;
    bool isVerticallyMerged = cell.CellFormat.VerticalMerge != CellMerge.None;
    string cellLocation = string.Format("R{0}, C{1}", cell.ParentRow.ParentTable.IndexOf(cell.ParentRow) + 1, cell.ParentRow.IndexOf(cell) + 1);
    if (isHorizontallyMerged && isVerticallyMerged)
        return string.Format("The cell at {0} is both horizontally and vertically merged", cellLocation);
    else if (isHorizontallyMerged)
        return string.Format("The cell at {0} is horizontally merged.", cellLocation);
    else if (isVerticallyMerged)
        return string.Format("The cell at {0} is vertically merged", cellLocation);
    else
        return string.Format("The cell at {0} is not merged", cellLocation);
}

Hope, this helps.

Best regards,