INCLUDEPICTURE associated with a MERGEFIELD

Hello,

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.,

{INCLUDEPICTURE "{IF TRUE "C:\Maps\{MERGEFIELD MapName}.jpg"}" \d }

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?

Thanks,
Mike

Hi Mike,

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:

Best regards,

I am trying the workaround that you suggested in:

I have a problem with this line:

doc.MailMerge.FieldMergingCallback = new HandleMergeImageFieldFromBlob();

The FieldMergingCallback property is not in the list of available properties. I am using
v9.1.0.0 of the Aspose.Words.dll.

Does the workaround use a different version of the dll?

Thanks,
Mike

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.

Hi

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.

Best regards,

Hi,

Still having problems …

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;
}

At this point e.FieldValue is empty.

Do you have any suggestions?

Thanks,
Mike

Hi Mike,

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;
    }
}

Best regards,

Hi there,

The issue maybe occuring because name of the datafield in your CSV file is “Image: textbox1”, it should just be “textbox1”.

Thanks

Hi,

Thanks for explaining what I needed to do.

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:

Do you have any suggestions?

Thanks,
Mike

Hi Mike,

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.

Best regards.

Hi Alexey,

Files attached as requested.

Mike

Hi Mike,

Thanks for your request. But I cannot see the image file you have attached. Could you please reattach it here in the forum?

Best regards,

Hi Alexy,

OK, tried again, hopefully the image is attached this time.

I have tried a few images, teh only one that worked was very small - 4Kb.

Thanks,
Mike

Hi

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.

Best regards,

Hello Andrey,

Thank you for trying to reproduce the problem, I am pleased to hear that you have been able to mailmerge merge large images.

I will get one of my colleagues to try and see how they get on.

Thanks,
Mike

Thank you. We will wait for your result.

Best regards,

Alexey,

Do you know when a fix will be available to this problem? If it is soon we will wait for the fix rather than coding a workaround for our customers.

Thanks,
Mike

Hi

Thanks for your inquiry. Currently I cannot provide you any reliable estimate regarding this issue. You will be notified as soon as it is resolved.

Best regards,

Hi Mike,

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.

Best regards.

Hi,

Thank you for your reply, I am sorry that the images I sent did not work.

I have tried using ImportPicture directly in Word:

{IMPORTPICTURE “c:\Toshiba0800x0600.bmp” * MERGEFORMAT}

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.

Thanks,
Mike