2

I am trying to loop through a Dataset, creating a page per item using Aspose.Words Mail-Merge functionality. The below code is looping through a Dataset - and passing some values to the Mail-Merge Execute function.

var blankDocument = new Document();
var pageDocument = new Document(sFilename);
...
foreach (DataRow row in ds.Tables[0].Rows){
    var sBarCode = row["BarCode"].ToString();
    var imageFilePath = HttpContext.Current.Server.MapPath("\\_temp\\") + sBarCode + ".png";

    var tempDoc = (Document)pageDocument.Clone(true);

    var fieldNames = new string[] { "Test", "Barcode" };
    var fieldData = new object[] { imageFilePath, imageFilePath };

    tempDoc.MailMerge.Execute(fieldNames, fieldData);

    blankDocument.AppendDocument(tempDoc, ImportFormatMode.KeepSourceFormatting);
}
var stream = new MemoryStream();
blankDocument.Save(stream, SaveFormat.Docx);
// I then output this stream using headers, 
// to cause the browser to download the document.

The mail merge item { MERGEFIELD Test } gets the correct data from the Dataset. However the actual image displays page 1's image on all pages using:

{ INCLUDEPICTURE "{MERGEFIELD Barcode }" \* MERGEFORMAT \d }

Say this is my data for the "Barcode" field:

c:\img1.png
c:\img2.png
c:\img3.png

Page one of this document, displays c:\img1.png in text for the "Test" field. And the image that is show, is img1.png.

However Page 2 shows c:\img2.png as the text, but displays img1.png as the actual image.

Does anyone have any insight on this?

Edit: It seems as this is more of a Word issue. When I toggle between Alt+F9 modes inside Word, the image actually displays c:\img1.png as the source. So that would be why it is being displayed on every page.

I've simplified it to:

{ INCLUDEPICTURE "{MERGEFIELD Barcode }" \d }

Also, added test data for this field inside Word's Mailings Recipient List. When I preview, it doesn't pull in the data, changing the image. So, this is the root problem.

shA.t
  • 16,580
  • 5
  • 54
  • 111
tcarter2005
  • 597
  • 3
  • 12
  • 19

2 Answers2

4

I know this is old question. But still I would like to answer it.

Using Aspose.Words it is very easy to insert images upon executing mail merge. To achieve this you should simply use mergefield with a special name, like Image:MyImageFieldName. https://docs.aspose.com/words/net/insert-checkboxes-html-or-images-during-mail-merge/#how-to-insert-images-from-a-database

Also, it is not required to loop through rows in your dataset and execute mail merge for each row. Simply pass whole data into MailMerge.Execute method and Aspose.Words will duplicate template for each record in the data. Here is a simple example of such template Template screenshot

After executing mail merge using the following code:

// Create dummy data.
DataTable dt = new DataTable();
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Columns.Add("MyImage");
dt.Rows.Add("John", "Smith", @"C:\Temp\1.png");
dt.Rows.Add("Jane", "Smith", @"C:\Temp\2.png");

// Open template, execute mail merge and save the result.
Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.Execute(dt);
doc.Save(@"C:\Temp\out.docx");

The result will look like the following: Result screenshot

Disclosure: I work at Aspose.Words team.

Alexey Noskov
  • 1,722
  • 1
  • 7
  • 13
  • Hi I trying to do same but no luck. I have image path in one variable and need to resize image before adding width 50, Hight 50. row[mergeFieldName]=imagePath; this is how I am doing at last adding row to table. – BritSys Feb 26 '21 at 12:47
  • 1
    @BritSys You can implement IFieldMergingCallback https://apireference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback to control how the image is inserted into the document upon mail merge. In your particular case, you can use ImageFieldMergingArgs.ImageHeight and ImageFieldMergingArgs.ImageWidth properties https://apireference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback – Alexey Noskov Mar 01 '21 at 07:06
  • @AlexeyNoskov Is it possible to do this without having to use the "Image:" prefix for images? – TKharaishvili Feb 10 '22 at 13:50
  • 1
    @TKharaishvili Yes, you can achieve this without "Image:" prefix. But in this case it will be required to implement `IFieldMergingCallback` and on `FieldMerging` create `DocumentBuilder` move it to the field and insert the image. https://apireference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback – Alexey Noskov Feb 10 '22 at 17:50
2

If this was Word doing the output, (not sure about Aspose), there would be two possible problems here.

  1. INCLUDEPICTURE expects backslashes to be doubled up, e.g. "c\\img2.png", or (somewhat less reliable) to use forward slashes, or Mac ":" separators on that platform. It may be OK if the data comes in via a field result as you are doing here, though.

  2. INCLUDEPICTURE results have not updated automatically "by design" since Microsoft modified a bunch of field behaviors for security reasons about 10 years ago. If you are merging to an output document, you can probably work around that by using the following nested fields:

    { INCLUDEPICTURE { IF TRUE "{ MERGEFIELD Barcode }" } }
    

    or to remove the fields in the result document,

    { IF { INCLUDEPICTURE { IF TRUE "{ MERGEFIELD Barcode }" } } {
    INCLUDEPICTURE { IF TRUE "{ MERGEFIELD Barcode }" } } }
    

All the { } need to be inserted with Ctrl+F9 in the usual way.
(Don't ask me where this use of "TRUE" is documented - as far as I know, it is not.)

shA.t
  • 16,580
  • 5
  • 54
  • 111