We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Mail merge with both text and image

Hi


I have Apple text and Apple image needs to be mail merged <>, <> in word dcoument, How to do this? Please advice.

Or, is there any way i can move the builder to this <> tag holder and insert the image. Please advice


Regards
Showji
Hi Showji,

Thanks for your interest in Aspose.Words for Java.

1 - You can move the cursor to a specific location in document and then insert text/image using DocumentBuilder class.

2 - You can replace a merge field with text/image using mail merge functionality of Aspose.Words. Please refer to the following sections of the documentation which outline everything you need to know about mail merge:

About Mail Merge in Aspose.Words
Prepare a template Document
Simple Mail Merge Explained
How to Execute Simple Mail Merge

Moreover, to be able to insert images at a merge field, you first need to configure merge field's name according to the following syntax in your template document:

Image:MyFieldName

Then all you have to do is pass Image URLs to the mail merge engine as follows:

doc.getMailMerge().execute(
new String[] { "MyFieldName" },
new Object[] { "C:\\Temp\\Aspose.Words.png" });

Please let us know if we can be of any further assistance.

Best regards,
  1. Intead of moving the cursor to specific location using para number, can i move the cursor to a mergefiled <>?

    2. Is it mandatory to have the image merge field to be of “Image:fieldnmae” syntax pattern?

    3. Instead of passing the Image path, can i pass the bytes array of the image?


    Thank you for the information.


    Regards
    Showji

Hi Showji,


Thanks for your inquiry.

1. Yes, you can move cursor to a specified merge field and insert image there. Please see the following overloads of DocumentBuilder.MoveToMergeField methods:

DocumentBuilder.MoveToMergeField Method (String)
DocumentBuilder.MoveToMergeField Method (String, Boolean, Boolean)

Here is sample code to achieve this:
Document doc = new Document(getMyDir() + “in.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveToMergeField(“pic”);
builder.insertImage(getMyDir() + “aspose.words.png”);

doc.save(getMyDir() + “out.docx”);

2 & 3. If you want to control how data is inserted into merge fields during a mail merge operation, you need to implement IFieldMergingCallback Interface. This interface has a IFieldMergingCallback.ImageFieldMerging event which occurs during mail merge when an image mail merge field is encountered in the document. You can respond to this event to return a file name, stream, or an Image object to the mail merge engine so it is inserted into the document. The following article will be helpful as well:
http://www.aspose.com/docs/display/wordsjava/How+to++Insert+Images+from+a+Database

However, if you just want to move the cursor to a merge field and then insert image at this place using DocumentBuilder, then there is no need to prefix merge fields with “Image:” marker.

Best regards,