InsertImage w/Multiple Images

I have recently upgraded from an older version of Aspose.Words and have run into a change in behavior that I am having a problem resolving.
I have a Word document (attached) that has several merge fields where I attempt to insert images using the attached procedure. When the Word document is generated the first image (at merge field “populationGraph1”) does not get displayed. This was not a problem in the older version of Aspose.Words.
Thanks for your help.
Lee
In case it’s of interest here is the fetchGraph procedure used in the GenerateGraphs procedure:

public MemoryStream fetchGraph(string graphURL)
{
    WebRequest request = WebRequest.Create(graphURL);
    WebResponse response = request.GetResponse();
    byte[] responseData = new byte[response.ContentLength];
    int bytesRead = 0;
    while (bytesRead < response.ContentLength)
    {
        int bytesToRead = (int)response.ContentLength - bytesRead;
        bytesRead += response.GetResponseStream().Read(responseData, bytesRead, bytesToRead);
    }
    response.Close();
    // The field value is a byte array, just cast it and create a stream on it.
    MemoryStream imageStream = new MemoryStream(responseData);
    return imageStream;
}

Hi
Thanks for your request. I can’t reproduce this problem on my side. Please make sure that fetchGraph returns proper image.
Also for testing you can remove all content from your template except mergefield where you should insert images and try inserting images. Or just try to save image before inserting it into the document.
Best regards.

I have tried as you’ve suggested and removed all content from the template except for the merge field. No sucess there. But I was able to save the image that does not display and tried doing an InsertImage from the file, but it still would not display.

Hi
Thank you for additional information. Could you please attach an image that is not displayed and simplified template. I will try to reproduce the problem and provide you more information.
Best regards.

The requested files are attached.
Thanks for your help.
Lee

Hi
Thank you for additional information. I still can’t reproduce the problem on my side. I used the following code for testing.

Document doc = new Document(@"Test038\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
System.Drawing.Image img;
builder.MoveToMergeField("populationGraph1");
img = System.Drawing.Image.FromFile(@"Test038\graph1.png");
builder.InsertImage(img);
img.Dispose();
doc.Save(@"Test038\out.doc");

Image is inserted correctly.
Best regards.

Hi Alexey.
I tried the test as you described and it worked so I did a bit of rearranging of where I insert the images and found that if I performed doc.MailMerge.Exceute prior to inserting the images the first image would not display. But if I did the image inserts prior to any doc.MailMerge.Execute it worked fine.
I tried this test with different parameters for the MailMerge and had the same results.
Perhaps you can test this on your end.
Thanks for your help.
Lee

Hi

Thank you for additional information. Please make sure that mergefield “populationGraph1” exists in the document after performing Mail Merge. In your test document there is no “populationGraph1” mergefield at all. There is only “populationGraph2”.
Please try using the attached template and the following code:

Document doc = new Document(@"Test038\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
doc.MailMerge.Execute(new string[] { "populationGraph2" }, new object[] { "text" });
System.Drawing.Image img;
builder.MoveToMergeField("populationGraph1");
img = System.Drawing.Image.FromFile(@"Test038\graph1.png");
builder.InsertImage(img);
img.Dispose();
doc.Save(@"Test038\out.doc");

Also I see that DocumentBuilder does not work after performing mail merge (if you does not call MoveTo method before) and I created new issue #6918 in our defect database.
Best regards.

Hi Alexey:
Is it possible that issue #6918 could possibly explain the following related behavior? In the Word document we have the following merge fields:
«populationText1»

«populationTable1»
«populationGraph1»
«populationTable2»
«populationGraph2»

«populationText2»
The result now shows populationTable2 appended to populationTable1 with populationGraph1 and populationGraph2 following the tables. Here is how the tables appear:

Population - County and Selected Cities
1990 2000 2005 2010
Alameda County 1,279,269 1,443,741 1,452,821 1,454,362
Berkeley 102,671 102,743 102,296 105,304
Emeryville 5,663 6,882 7,497 7,667
Oakland 372,319 399,484 402,358 405,056
San Francisco 723,960 776,733 734,552 686,340
San Jose 783,001 894,943 890,394 882,464
San Leandro 69,480 79,452 79,438 78,323
Population Change (%) - County and Selected Cities
1990-2000 2000-2005 2005-2010
Alameda County 12.9% 0.6% 0.1%
Berkeley 0.1% -0.4% 2.9%
Emeryville 21.5% 8.9% 2.3%
Oakland 7.3% 0.7% 0.7%
San Francisco 7.3% -5.4% -6.6%
San Jose 14.3% -0.5% -0.9%
San Leandro 14.4% 0.0% -1.4%
I’ve attached that portion of the Word document with the images as well.
Thanks for your help
Lee

Hi
Thanks for you request. No I don’t think this behavior is related to this issue. I think there is something wrong in your code.
Why don’t you insert imaged during Mail Merge? I think this could resolve some of your problems.
https://reference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback/
Best regards.

Hi Alexey:
While I was unable to implement a solution using MergImageField, I did resolve my issue by breaking apart the table inserts and interspersing the image inserts, i.e., insert Population Table 1, insert Population Graph 1, insert Population Table 2, and insert Population Graph 2. None of these inserts involved a mail merge, but a mail merge was performed prior to this as well as after.
I’m not sure why this would make a difference but at this point I am pleased that I have a working solution.
Regards
Lee

Hi
It is nice that you found working solution for this issue. Please let me know in case of any issues. I will be glad to assist you.
Best regards.

I am closing #6918 because mail merge adds/removes text in the document in such a way that DocumentBuilder is not able to track its current position. The simple workaround (and in fact a better practice) is just to create DocumentBuilder after your mail merge, just before you want to modify your document using DocumentBuilder. There is no need to create it sooner.