Hi John,
Thanks for your inquiry.
john.nelson:
I can get the image into the document using doc.insertImage(byte[]) but I really need to use regions to get my dataset into the document.
You can get data source in the class to which you have implemented the IFieldMergingCallback interface. Please check the highlighted code below. Hope this helps you. If this does not help you, please share some more detail about your query. We will then provide you more information on this along with code.
private class ResultSetTest implements IFieldMergingCallback {
java.sql.ResultSet resultset;
ResultSetTest(java.sql.ResultSet mailMergeDataSource)
{
resultset = mailMergeDataSource;
}
public void fieldMerging(FieldMergingArgs e) throws Exception {
if (e.getFieldName().equals("Field1")) {
resultset.first();
System.out.println(resultset.getString("field1"));
}
}
public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
// Do nothing.
}
}
Document doc = new Document(MyDir + "MailMergeTest.docx");
java.sql.ResultSet table = createCachedRowSet(new String[] { "field1", "field2" });
addRow(table, new String[] { "Value 1", "Value 2" });
addRow(table, new String[] { "Value 1", "Value 2" });
addRow(table, new String[] { "Value 1", "Value 2" });
com.aspose.words.DataTable dt = new com.aspose.words.DataTable(table, "Test");
ResultSetTest callback = new ResultSetTest(table);
doc.getMailMerge().setFieldMergingCallback(callback);
doc.getMailMerge().executeWithRegions(dt);
doc.save(MyDir + "Out.docx");
john.nelson:
I have also been successful using imageFieldMerging() method - but I saved that byte[] as a static variable and used that in imageFieldMerging().
It would be great if you please share some more detail about this query.
john.nelson:
Assuming I can gett he image into the resultset, how do i go about accessing the imabe (byte[]) in imageFieldMerging() ?
You can get/set image in imageFieldMerging. Please check getImage/setImage and getImageStream/setImageStream method of ImageFieldMergingArgs class. Please check the members of ImageFieldMergingArgs class from here:
https://reference.aspose.com/words/java/com.aspose.words/FieldMergingArgs
private class HandleMergeImageFieldFromBlob implements IFieldMergingCallback {
public void fieldMerging(FieldMergingArgs args) throws Exception {
// Do nothing.
}
/**
* This is called when mail merge engine encounters Image:XXX merge field in the document.
* You have a chance to return an Image object, file name or a stream that contains the image.
*/
public void imageFieldMerging(ImageFieldMergingArgs e) throws Exception {
// The field value is a byte array, just cast it and create a stream on it.
ByteArrayInputStream imageStream = new ByteArrayInputStream((byte[]) e.getFieldValue());
// Now the mail merge engine will retrieve the image from the stream.
e.setImageStream(imageStream);
}
}
john.nelson:
Is there a phone number I can reach you? It would be easier to talk about.
We only provide technical support through forums and live chat. It means that we do not provide technical support through telephone right now.