Free Support Forum - aspose.com

Horizotally merge multiple cells

how can horizotally merge multiple cells
Hi Ajeesh,

Thank you very much for your inquiry.

You can use CellFormat.HorizontalMerge property of any Cell for merging it horizontally with neighbouring cells. Please have a look here in the documentation reference to see how merging of Cells can be performed.

Following code snippet performs horizontal cell merging in first row of the the table and should be helpful:
string csPath = "C:\\Temp\\";
Document doc = new Document(csPath+"sample.docx"); 
// Get first table in the document.
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
// Loop through the rows in a table.
foreach (Row row in table.Rows)
{
     // Loop through the cells in a row.
     foreach (Cell cell in row.Cells)
     {
          // If it is a first cell, set its horizontal merge property to First
          // otherwise, set it to Previous.
          if (cell.IsFirstCell)
               cell.CellFormat.HorizontalMerge = CellMerge.First;
          else
               cell.CellFormat.HorizontalMerge = CellMerge.Previous;
     }
     break;
}
// Save the document in Word format.
doc.Save(csPath + "output.docx"); 
Attached is the input word document with a table (sample.docx) and output word document (output.docx) in which all cells in the first row are merged horizontally into one.

Hope this helps. Please feel free to ask if you have any more queries.