Hello there,
I am creating a table with multiple cells at run time and inserting Image Merge Field into them, which are replaced with actual Image during mail merge process.
Table will always have one row and 2-4 columns/cells. When Image is inserted in these table cells, I want Row height and cell width to be fixed. But some how, all the height/width formatting seems destroyed when writing to output document.
Code snippet to Create Table and its cells:
if (imageCodes.Count()>= 2)
{
Table objTable = new Table(builder.Document);
objTable.Rows.Add(new Row(builder.Document));
PageSetup objPageSetup = builder.Document.Sections[0].PageSetup;
double pageWidth = objPageSetup.PageWidth - objPageSetup.LeftMargin - objPageSetup.RightMargin;
double cellWidth = pageWidth / imageCodes.Count();
builder.CurrentParagraph.ParentNode.InsertAfter(objTable, builder.CurrentParagraph);
// Set ROW HEIGHT
if (imageCodes.Count() == 2)
{
objTable.Rows[0].RowFormat.Height = 280;
objTable.Rows[0].RowFormat.HeightRule = HeightRule.Exactly;
}
else if (imageCodes.Count() == 3)
{
objTable.Rows[0].RowFormat.Height = 182;
objTable.Rows[0].RowFormat.HeightRule = HeightRule.Exactly;
}
else
{
objTable.Rows[0].RowFormat.Height = 133;
objTable.Rows[0].RowFormat.HeightRule = HeightRule.Exactly;
}
string imageCode;
foreach(string code in imageCodes)
{
imageCode = "IMAGE" + code;
Cell objCell = new Cell(builder.Document);
objCell.CellFormat.Width = cellWidth;
objCell.EnsureMinimum();
objTable.Rows[0].Cells.Add(objCell);
builder.MoveTo(objCell.ChildNodes[0]);
builder.InsertField(string.Format("MERGEFIELD Image:{0}", imageCode), string.Format("½Image:{0}╗", imageCode));
}
}
For testing purpose, Right now, I am using the same Image to insert in all table cells.
Please see the attached output doc and image that I am insertng during mail merge.
Please guide.
Thank you!