Can you programatically merge cells in a Word Table vertically. eg. row 10, cell 0 merged with row 11, cell 0 to make one vertical cell, but leaving cell1 in both rows on the right?
I need to insert multiple images down the right column (cell 1 in each row), but have the text describing the images in one vertical cell on the left.
I can only see properties describing existing merges in the document, not how to actually do a merge. Also, I don't know how many images there will be (could be 2-10).
Can you programatically insert multiple images into one cell, one under the other, then I wouldn't need to merge cells at all.
Any help, ideas on this scenario (doing mailmerge with Word document, and DocumentBuilder.InsertImage) would be so much appreciated. :-)
Many thanks
Barry
Hi Barry,
Thanks for your inquiry.
Barry: Can you programatically merge cells in a Word Table vertically. eg. row 10, cell 0 merged with row 11, cell 0 to make one vertical cell, but leaving cell1 in both rows on the right?
Please try using the following code snippet:
Document doc = new Document(@“c:\temp\in.docx”);
Table tbl = doc.FirstSection.Body.Tables[0];
HorizontallyMergeCells(tbl.Rows[0].Cells[1], tbl.Rows[0].Cells[2]);
VerticallyMergeCells(tbl.Rows[0].Cells[0], tbl.Rows[1].Cells[0]);
doc.Save(@“c:\temp\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;
}
Secondly, to be to insert multiple images in a cell, I would suggest you use MailMerge fields for each Image inside that cell and then perform simple mail merge operation. Please see the following code snippet for inserting Images from files:
Document doc = new Document(@“c:\temp\in.docx”);
doc.MailMerge.Execute(
new string[] { “img1”, “img2”},
new object[] { @“c:\temp\1.jpg”, @“c:\temp\2.jpg” });
doc.Save(@“c:\temp\out.docx”);
Moreover, I would suggest you please read the following useful article:
Best Regards,