I have a Word 2007 template (saved in Word 2003 format - .doc) on which I am trying to combine an INCLUDEPICTURE field with a MERGEFIELD. The objective of this is to include a relevent map in a letter depending on the content of a merge data field, e.g.,
It appears that this nested approach does not work with Aspose. The MERGEFIELD command works but the INCLUDEPICTURE does not.
I have been told that … Aspose.Words does support INCLUDEPICTURE fields but since
they are converted to a shape upon merging the reference to a textbox inside
the merge field is lost.
Could you give me an example of what I would have to do in code to make the map appear in the letter?
Thank you for reporting this problem to us. I managed to reproduce it on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved.
The problem occurs because fields nested into INCLUDEPICTURE field are broken upon loading.
Maybe as a workaround, you can try inserting embedded images upon mail merge. Please follow the link to learn how to achieve this:
Downloading the 9.2.0.0 dll, I see that there are changes around mail merging in this newer version, leave it with me I’ll see if I code the workaround against 9.2.0.0.
Thanks for your request. Yes, you are right, there were few breaking changes in API starting from 9.2.0 version Aspose.Words. Please let me know if you have any questions, I will be glad to help you.
My Word template has : {IncludePIcture “MERGEFIELD Image:textbox1}”}
The CSV merge data file has the data field: Image:textbox1
After including the suggested workaround code I’m getting “Buffer cannot be null. Paramater name: Buffer ” from the line highlighted in red :
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
{
// The field value is a byte array, just cast it and create a stream on it.
MemoryStream imageStream = new MemoryStream((byte[])e.FieldValue);
// Now the mail merge engine will retrieve the image from the stream.
e.ImageStream = imageStream;
}
Thanks for your request. You do not need to use INCLUDEPICTURE field. In your template you should just have merge field like the following: { MERGEFIELD Image:textbox1 }
I created simple code and template, which shows how to achieve this:
[Test]
public void Test001()
{
// Open template document.
Document doc = new Document(@"Test001\in.doc");
// Add mail merge callback.
doc.MailMerge.FieldMergingCallback = new MailMergeCallback();
// Execute mail merge.
doc.MailMerge.Execute(new string[] { "textbox1" }, new object[] { File.ReadAllBytes(@"C:\Temp\out.jpg") });
// Save output document.
doc.Save(@"Test001\out.doc");
}
private class MailMergeCallback : IFieldMergingCallback
{
public void FieldMerging(FieldMergingArgs args)
{
// Do nothing.
}
public void ImageFieldMerging(ImageFieldMergingArgs args)
{
// The field value is a byte array, just cast it and create a stream on it.
MemoryStream imageStream = new MemoryStream((byte[])args.FieldValue);
// Now the mail merge engine will retrieve the image from the stream.
args.ImageStream = imageStream;
}
}
I have now managed to use the suggested code and get the mail merge to work.
However it only works with very small images. I tried a 247Kb image, no errors were generated but when I tried to open the merged document (.doc) in Word 2007 I get “There is not enough memory or disk space to display or print the picture”.
My guess is that this is because the image is corrupt.
I found an old thread with a similar problem, which had been fixed:
Thanks for your inquiry. Could you please attach your template, image and output document here for testing? I will check the issue and provide you more information.
Thank you for additional information. It seems this image is broken; I cannot open the files you have attached. But I have tried with images with different size, and I cannot reproduce the problem.
Thanks for your inquiry. The issue is not scheduled yet, so I cannot provide you any reliable estimate at the moment.
I would like to add that even MS Word cannot insert the images you attached. So I suppose even if you insert them into MS Word document using INCLUDEPICTURE field, MS Word will not display them.
This does import the picture successfully so there is nothing wrong with the image and large size images do work.
I have also experimented with mail merging images of different sizes using Aspose and found that images larger than around 300Kb produce the memory error problem.
So there is a difference between the results I get when doing an Aspose merge and the results you get. Maybe it is due to the patch level in software or the memory available on the PC?
I am using Microsoft Office Ultimate Word 2007 (12.0.6525.1000) SP2 for my templates.
Can you please tell me how large the images that you have successfully mail merged are and whether you have any other suggestions that I could try?
We have a customer who makes extensive use of merging images so this is a big problem to us.