Image mail merge with path from database

Hi,

I have just read the tutorial for image merging from database at https://docs.aspose.com/words/java/working-with-images/

However, that tutorial seems to suppose that the image itself is on the database, however, in my use case, it just stores the path.

The problem is that the value for the merge field is still a byte array, as if the class treats it as the image itself, while in reality I have to build the full path in the event handler.

Strange enough, if I return a numeric value for the image field, say an ID, then it is passed to the event as a Long, not byte[].

Is there a way to have the library keep my string as a string, instead of extracting it as byte[] ?

Hi Matteo,

Thanks for your inquiry. ImageFieldMerging 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.

There are three properties available ImageFileName, ImageStream and Image to specify where the image must be taken from. Set only one of these properties.

In your case, I suggest you please use ImageFileName property as show in following code snippet. Hope this helps you.

public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception
{
    args.setImageFileName(args.getFieldValue().toString());
}

Hi,

My problem is that calling args.getFieldValue() returns a byte[], and calling toString() on it returns the java internal representation of the object, not the string which the byte[] represents.

To clarify, I get the full path in the query as:

SELECT CONCAT('/base/path/to/images', path_field) as myimage
FROM mytable
WHERE id = 1;

And then in the .doc template I have a merge field like «Image:myimage»; however the problem is that in the event handler I have that args.getFieldValue().toString() is something like “[B@ad515” and so the image fails to load.

I am already able to “solve” this problem by doing some explicit transformation like

args.setImageFileName(new String(args.getFieldValue(), "UTF-8"));

but that would break should I ever have images really stored on the database!

Hi Matteo,

Thanks for your inquiry. It is nice to hear from you that you have solved your query.

*mtassinari:

but that would break should I ever have images really stored on the database!*

No, it depends on your requirements. You need to code in imageFieldMerging according to your database table’s structure.