Add two different image using one mergefield in the same word document in c#

How to add the two different images using one merge field in the same word document. As per the below example

If I have two pages in one word document and in that I have added the “Image:Fruit” as a merge field in both the pages and then on one page I want to print the Apple image and on the second page I want to print the Banana image.

Can anyone please help me with this.

@SmitMachchhar

We suggest you please implement IFieldMergingCallback interface and insert the image in IFieldMergingCallback.ImageFieldMerging.

If you still face problem, please ZIP and attach your template document and images here for testing. We will investigate the issue and provide you more information on it.

@tahir.manzoor

Thanks for your reply

I am using the IFieldMergingCallback but when I do the printing of images it shows the only one image in both the pages but I want two different images to be printed with the same Mergefield tag.

Please refer to the attached ZIP which contains one document and two images.MergeField Document and Images.zip (1.9 MB)

@SmitMachchhar

We have modified your template document and attached it with this post for your kind reference.
Docs.zip (1.9 MB)

You can use following code example to achieve your requirement.

Document doc = new Document(MyDir + "Image.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

DataTable table = new DataTable("tablename");
table.Columns.Add("Fruit");
table.Rows.Add(new object[] { MyDir + "Apple.jpg" });
table.Rows.Add(new object[] { MyDir + "Banana.jpg" });
doc.MailMerge.ExecuteWithRegions(table);

doc.Save(MyDir + "out.docx");

@tahir.manzoor

Thanks for your reply

I had tried this and working fine but this code is creating the extra page and rather than passing the image path can we pass the file data for Image type?.

Another thing is, for example, I have three pages and using the same MergeField I want to print one image on the first page and another image on the third page is this possible?.

@SmitMachchhar

We have modified the template document to avoid the page break issue.
_template.zip (14.6 KB)

You can use the same template document to get the desired output.

Yes, you can use image type data source for mail merge with regions. We suggest you please read the articles about mail merge with regions from here:
Mail Merge with Regions Explained
How to Execute Mail Merge with Regions

@tahir.manzoor

Thanks for your reply

This document will print both the images on the same page, but my requirement is on the different pages.

I am getting unwanted multiple blank pages in between and how to skip the one page and then print the image.

Thank you for this. I will check the articles.

@SmitMachchhar

Please make sure that you are using the same template document. In case, you are using old version of Aspose.Wrods, we suggest you please upgrade to the latest version of Aspose.Words for .NET 20.6.

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.