Free Support Forum -

Mail Merge recommendations? I want my images to retain formatting

I have been able to successfully merge images into my documents by specifying { MERGEFIELD Image:FieldName } as the merge field (note "Image:" is case-sensitive), however the mergefield is completely replaced - and the image is inserted as-is (with small variations on size - presumably because of Word limitations).

I need the ability to have a formatted image, and during the mailmerge process to change the image source.

I have tried nested fields to achieve this:






None of these approaches have worked.

Any and all suggestions are welcome!


Earl Brown

Hi Earl,

Thank you for your interest in Aspose.

You can use the following function to format the image. Please note that, in the template file, you need to fix the size of table-cell, and put a textbox within the cell (no need to fix its size) which will be used to display the image.This piece of code will automatically display the input image inside the textbox after adjusting the size of textbox to the cell boundary.

private bool InsertImage(string mergeFieldName, string imageFileName, DocumentBuilder builder)
if (builder.MoveToMergeField(mergeFieldName))
double width = -1;
double height = -1;
Cell cell = (Cell)builder.CurrentParagraph.GetAncestor(typeof(Aspose.Words.Tables.Cell));

if (cell != null)
cell.CellFormat.LeftPadding = 0;
cell.CellFormat.RightPadding = 0;
cell.CellFormat.TopPadding = 0;
cell.CellFormat.BottomPadding = 0;
cell.CellFormat.WrapText = false;
cell.CellFormat.FitText = true;
// Get cell dimensions.
width = cell.CellFormat.Width;
height = cell.ParentRow.RowFormat.Height;
Shape shape = (Shape)builder.CurrentParagraph.GetAncestor(typeof(Aspose.Words.Drawing.Shape));
if ((shape != null) && (shape.ShapeType == ShapeType.TextBox))
// Set the textbox properties so that the inserted image could occupy the textbox space exactly.
shape.TextBox.InternalMarginTop = 0;
shape.TextBox.InternalMarginLeft = 0;
shape.TextBox.InternalMarginBottom = 0;
shape.TextBox.InternalMarginRight = 0;
// Get cell dimensions.
width = shape.Width;
height = shape.Height;
// Insert image with or without rescaling, based on the previously done analysis.
builder.InsertImage(imageFileName, width, height);
// Signal the caller that the image was succesfully inserted at merge field position.
return true;
// Signal the caller that no merge field with the specified name could be found in the document.
return false;

If you have any other question, please feel free to ask.

Adnan Mujahid Khan

Thanks for the quick response!

Aspose *does* look like what we want to use...if I can get these issues cleared up, we'll be good-to-go.

I like the work-around you've provided, but I do have some questions about it. I realize there is the IFieldMergingCallback.ImageFieldMerging method to capture the event, but:

* How do I get the DocumentBuilder object?

* How do I tell the MailMerge engine to ignore this field because I did the work myself?

Maybe this is a more fruitful question:

* Would there be any way to get the "parent text box" of a merge field? If I had that, I'm pretty sure I could hijack your example and massage it to do what I'm looking for.

Thanks again!

Here are your concerns:

How do I get the DocumentBuilder object?
You can get the Document builder object by using the Document object that you would most probably be currently working with. See the code snippet:
Document doc = new Document (“Template.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);

How do I tell the MailMerge engine to ignore this field because I did the work myself?
As you mentioned, IFieldMergingCallback could be used for this purpose. You may consult this link for detailed description and demonstration.

Will you please slightly explain your last question?

Best Regards,
Adnan Mujahid Khan

Thanks for your support, I believe I found the answer to my question in this post:

Thanks, again!

Hi Earl,

It is perfect that you managed to achieve what you need. Please let us know if you need more assistance. We are always glad to help you.

Best regards,