Insert Image in Table Cells at run time - Formatting Issue

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!

Any comments on this?

Hi

Thank you for your request. First of all your code can be simplified as shown below:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
string[] imageCodes = new string[]
{
    "test1",
    "test2"
};
if (imageCodes.Length>= 2)
{
    Table objTable = builder.StartTable();
    PageSetup objPageSetup = builder.Document.Sections[0].PageSetup;
    double pageWidth = objPageSetup.PageWidth - objPageSetup.LeftMargin - objPageSetup.RightMargin;
    double cellWidth = pageWidth / imageCodes.Length;
    // Set cells width.
    builder.CellFormat.Width = cellWidth;
    builder.RowFormat.AllowAutoFit = false;
    // Set row height.
    builder.RowFormat.HeightRule = HeightRule.Exactly;
    if (imageCodes.Length == 2)
        builder.RowFormat.Height = 280;
    else if (imageCodes.Length == 3)
        builder.RowFormat.Height = 182;
    else
        builder.RowFormat.Height = 133;
    foreach(string code in imageCodes)
    {
        builder.InsertCell();
        builder.InsertField(string.Format("MERGEFIELD Image:IMAGE{0}", code), string.Format("½Image:IMAGE{0}╗", code));
    }
    builder.EndRow();
    builder.EndTable();
}
// Execute mail merge.
doc.MailMerge.Execute(new string[]
    {
        "IMAGEtest1",
        "IMAGEtest2"
    },
    new object[]
    {
        @"Common\large.jpg", @"Common\large.jpg"
    });
// Save output document.
doc.Save(@"Test001\out.doc");

The highlighted line of code resolves the problem with cell width.
Hope this helps.
Best regards.

Thanks Alexey,

The sample code did help me to make my code look better and work better

There is one more issue:
When I am inserting Images with bigger size in table cells with smaller size,
it’s just showing the part of image that fits into the table cell size.

Is there any way I can compress the Image size to fit into table cell while doing mail merge?

Output document is attached for your reference. If you click on the Image inside table cell, it even shows the image handlers as per origional size of image.

Please guide.

Hi Nutan,

Thanks for your request. You can calculate image size to fit the container (table cell in your case). You can use the same technique as described here to achieve this:
https://forum.aspose.com/t/111719
https://forum.aspose.com/t/95167
Hope this helps.
Best regards.