I want to remove all vertical merges in tables in all sections of the document

// Iterate through all the sections in the document
foreach (Section section in doc.Sections)
{
    // Iterate through all tables in the section
    foreach (Table table in section.Body.Tables)
    {
        // Create a 2D array to track the cells
        Cell[,] cells = new Cell[table.Rows.Count, table.Rows[0].Cells.Count];

        // Populate the cells array
        for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
        {
            Row row = table.Rows[rowIndex];
            for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
            {
                cells[rowIndex, cellIndex] = row.Cells[cellIndex];
            }
        }

        // Iterate through the cells of the table
        for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
        {
            for (int cellIndex = 0; cellIndex < table.Rows[0].Cells.Count; cellIndex++)
            {
                Cell cell = cells[rowIndex, cellIndex];

                // Check if the cell is vertically merged with itself
                if (IsSelfVerticalMerged(cell, cells))
                {
                    // Remove the vertical merge from the cell
                    cell.CellFormat.VerticalMerge = CellMerge.None;
                }
            }
        }
    }
}


private static bool IsSelfVerticalMerged(Cell cell, Cell[,] cells)
{
    // Check if the cell is vertically merged with the previous cell in the same row
    if (cell.CellFormat.VerticalMerge == CellMerge.Previous && cell.ParentRow.RowIndex > 0)
    {
        Cell previousCell = cells[cell.ParentRow.RowIndex - 1, cell.ParentRow.IndexOf(cell)];
        if (previousCell.CellFormat.VerticalMerge == CellMerge.Next)
        {
            return true;
        }
    }

    return false;
}

I tried to remove all vertical merges in the document. “cell.ParentRow.RowIndex” and “CellMerge.Next” not available for me. How can I acheive this. Please help

@coderthiyagarajan1980 Please try the following code

foreach (Cell cell in doc.GetChildNodes(NodeType.Cell, true))
    cell.CellFormat.VerticalMerge = CellMerge.None;

Yes It’s resolved.

1 Like