Free Support Forum - aspose.com

Creating Table Cells and inserting in Images at run time

Hello there,

I am inserting a table with one row and multiple cells in my source document.
And then inserting actual Image into each cell while mail merge.

I want to fix/lock the cell width so that when big images are inserted in these table cells,
images don’t overflow the page width.

Code to insert table cells and Image Merge Fields at run-time:
string[] imageCodes = imageAttributes[0].Value.Split(’,’);
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);

foreach (string imageCode in imageCodes)
{
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:IMAGE{0}”, imageCode), string.Format("«Image:IMAGE{0}»", imageCode));
}
}

in MergeImageField handler:
private void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs e)
{
byte[] imageBytes = (e.FieldValue as byte[]);

if (imageBytes != null)
{
e.Image = System.Drawing.Image.FromStream(new MemoryStream((byte[])e.FieldValue));
}
}

Please guide me how can I lock cell width in above scenerio.

Thanks a lot!

This is working fine :-), Please close this topic.

Thank you!