Mail Merge Word Document with images with Aspose.Words REST API

Hi There,

Hoping someone can help. We’re having issues merging an image from the Aspose storage into a word document.

We’re currently using the technique described here:

https://docs.aspose.cloud/words/mail-merge/

At the bottom of that screen, it indicates that it should be possible to merge XML with images and gives the structure for the XML. It doesn’t however seem to work, or provide examples of how the merge field should be placed into the word document.

We can merge the data using this method, but not the image.

Any help would be greatly appreciated.

Many thanks

Hi there,

Thanks for your inquiry. I have tested the scenario and have not found any issue while using executeTemplate. Please check the following xml. The image file should be in folder1 at storage. Please upload the image file at storage and Execute Mail Merge Template .

This is the first caption !!!

folder1\image1.jpg

This is the second caption !!!

folder1\image2.jpg

I suggest you please download the Aspose Cloud SDK and use following code example to check the executeTemplate. I have attached the intput/output and xml file with this post for your kind reference. Hope this helps you. Please let us know if you have any more queries.

try {
  string inputFile = @ "C:\temp\TestExecuteTemplate.doc";
  string xml = null;
  using(System.IO.StreamReader sr = new System.IO.StreamReader(@ "C:\TestOrders.xml")) 
{
    xml = sr.ReadToEnd();
  }
  Aspose.Cloud.Storage.Folder folder = new Aspose.Cloud.Storage.Folder();
  folder.UploadFile(inputFile, "");
  string fileName = Path.GetFileName(inputFile);
  Aspose.Cloud.Words.MailMerge mailMerge = new Aspose.Cloud.Words.MailMerge();
  mailMerge.ExecuteTemplate(fileName, xml, SaveFormat.Docx, "c:\\temp\\Output.docx");
} catch (Exception ex) {
  Console.WriteLine(ex.Message);
  Console.ReadLine();
}

Thanks for the reply. We’ve already had this information from another support staff member and are still having issues.

What we require (as asked for in the post) is an example of the word template merge field that is required using the above method. So exactly how should the merge field be formatted in the word document.

Our current word doc has the following fields in it:

{{#foreach Item}}

{{Caption}}

{{Img}}

{{/foreach Item}}

And at the moment, it’s just printing the name of the image in the document. There is obviously something I’m missing to get the word document to render the image.

Thanks

Dan

Hi Dan,

Thanks for your inquiry. Please check the input document in my previous post.

Please specify a field name prefix like Image: MyFieldName in the document to be able to directly merge images during Mail Merge. Please check the image merge field as shown below:

{{#foreach Item}}

{{Caption}}

{{Image:img}}

{{/foreach Item}}

@DanBailey

We have recently released API Version 4.0 of Aspose.Words Cloud API. In this version, we have introduced Storage API methods along with many other breaking changes. Please check the updated sample code for Mail Merge.

string clientId = "####-####-####-####-####";
string secret = "##################";
var config = new Configuration { ClientId = clientId, ClientSecret = secret };
var wordsApi = new WordsApi(config);

string fileName = "TestMailMergeWithImages.doc";
string destName = "Out_TestMailMergeWithImages.docx";

// Upload original document to Cloud Storage
wordsApi.UploadFile(new UploadFileRequest(File.Open(fileName, FileMode.Open), fileName, null));

string data = File.ReadAllText("MailMergeData.txt", System.Text.Encoding.UTF8);

ExecuteMailMergeRequest request = new ExecuteMailMergeRequest(fileName, data, null, null, null, null, null, null, null, null, destName);
DocumentResponse result = wordsApi.ExecuteMailMerge(request);