Problem when resizing image during mail merge

Hi everybody,

I have a problem while resizing an image during mail merge as showed here.

The problem is, in my template, which I attached, the cell which will contain the image spans multple rows, so the trick used to get row height cannot work since it returns the height of a single row and not the sum of all those.

How can I get the actual cell height then, in order to do proper resizing?

Thanks.

Hi Matteo,

Thanks for your inquiry. In your case, the six Rows from start have three Cells each while the remaining Rows are having only two Cells. The Row height of each Row is 0.24". I think, you can keep track of Rows[x].Cells.Count e.g. while the Rows have three Cells, keep adding up the Row heights to the Shape.Height property. This way you will get 6 * 0.24 figure. I hope, this helps.

Best regards,

This does help, but only in this specific case, while I need a more generic algorithm.

Is there a way to know the “rowspan” of a cell using aspose.words? I do not see such method, but it could be just me not finding it.

Hi Matteo,

Thanks for your request. Here is a draft code that keeps track of Cell nodes that are vertically merged and adds up the Row heights to the Shape.Height property:

public class HanldeImageMergeFields : IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
    }
    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        DocumentBuilder builder = new DocumentBuilder(args.Document);
        builder.MoveToMergeField(args.FieldName);
        Shape img = builder.InsertImage(args.FieldValue.ToString());
        Cell c = (Cell)builder.CurrentParagraph.GetAncestor(NodeType.Cell);
        img.Width = c.CellFormat.Width;
        img.Height = 0;
        Table tbl = c.ParentRow.ParentTable;
        for (int startRowIndex = tbl.Rows.IndexOf(c.ParentRow);
        startRowIndex < tbl.Rows.Count;
        startRowIndex++)
        {
            Row row = tbl.Rows[startRowIndex];
            foreach (Cell cell in row.Cells)
            {
                bool isVerticallyMerged = cell.CellFormat.VerticalMerge != CellMerge.None;
                if (isVerticallyMerged)
                    img.Height += row.RowFormat.Height;
            }
        }
    }
}

I hope, this helps.

Moreover, I would suggest you please read the following article that outlines the topic ‘Working with Merged Cells’.
https://docs.aspose.com/words/net/working-with-columns-and-rows/

Best regards,