Free Support Forum - aspose.com

How to merge four cells in two rows and two columns in word table

How to merge four cells in two rows and two columns in word table。The first cell and second cell in the first row, the first cell and second cell in the second row, I want merge these four cells, How can I do?

Hi Aostarit,


Thanks for your inquiry. Please use the following code snippet to achieve your requirements.

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>Document<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> doc = new Document(MyDir

  • “in.docx”);<o:p></o:p>

Table table = (Table)doc.GetChild(NodeType.Table, 0, true);

// We want to merge the range of cells found inbetween these two cells.

Cell cellStartRange = table.Rows[0].Cells[0];

Cell cellEndRange = table.Rows[0].Cells[1];

HorizontallyMergeCells(cellStartRange, cellEndRange);

doc.Save(MyDir + "out.docx");

private static void HorizontallyMergeCells(Cell c1, Cell c2)

{

c1.CellFormat.HorizontalMerge = CellMerge.First;

//Move all content from next cell to previous

foreach (Node child in c2.ChildNodes)

c1.AppendChild(child);

c2.CellFormat.HorizontalMerge = CellMerge.Previous;

}

private static void VerticallyMergeCells(Cell c1, Cell c2)

{

c1.CellFormat.VerticalMerge = CellMerge.First;

//Move all content from bottom cell to top

foreach (Node child in c2.ChildNodes)

c1.AppendChild(child);

c2.CellFormat.VerticalMerge = CellMerge.Previous;

}


You can also achieve the same thing by using vertical and horizontal merge of a table. Please check "MergeCells" method from the following article:
http://docs.aspose.com/display/wordsnet/Working+with+Merged+Cells