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

Free Support Forum - aspose.com

Image merging does not work with DataTable overload

I have images merging into a Word document using code that looks almost identical to that posted here: https://forum.aspose.com/t/113520.

This works perfectly. However, if I modify the code just slightly so that it uses the DataTable overload of doc.MailMerge.Execute, it fails.

The reason it fails is that the event handler has a value of ‘system.byte[]’ for e.FieldValue instead of the actual bytes. This is despite the fact that I can see with the debugger that the actual bytes, not a string, are going into the data row of the data table when I build it. Remember, the Word document and event handler, both of which worked fine before, have not changed.

Is there something wrong with the DataTable overload of Execute that makes it turn everything into strings?


1 Like

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Could you please attach your template and provide me your code for testing? I will try to reproduce the issue and provide you more information.

Best regards.

Yes this is still occurring, on DataRow as well.

    public static byte[] getFileBytes(File file) throws Exception {
        int fileLength = (int) file.length();
        byte[] fileBytes = new byte[fileLength];

        FileInputStream fis = new FileInputStream(file);
        fis.read(fileBytes, 0, fileLength);

        return fileBytes;

// From here down is sample code.

// Open the template document.
Document doc = new Document("./template.docx");

// Get the MailMerge object for the doc and set some options for cleanup & formatting.
MailMerge mm = doc.getMailMerge();
        MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS |
        MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS   |
        // Note: Comment out to see fields that are not updated in output doc.
        MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS    |

// Trim whitespace from merge inputs.

// Allows using 'Mustache' syntax for mail merge template.

DataTable dataTable = new DataTable();
DataRow dataRow = dataTable.newRow();

dataRow.set("DocumentImage", getFileBytes(new File("./myImage.png")));

// Does not work

// Works.
//mm.execute(new String[]{ "DocumentImage" }, new Object[]{ getFileBytes(new File("./myImage.png") }));


document should contain {{ Image:DocumentImage }}

Any chance of having the library updated to allow for images in DataRows/DataSet/DataTable and executeWithRegions?

Expectation: Image is merged into the document.


Thanks for your interest in Aspose. Please check following code snippet to use merge images via DataTable. You need to set column type before you add value in DataRow and use IFieldMergingCallback to the purpose.

DataTable dataTable = new DataTable();
DataColumn ImageCol = new DataColumn("DocumentImage", ByteArrayOutputStream.class);
DataRow row = dataTable.newRow();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedImage img = ImageIO.read(new File("E:/Data/Aspose.jpg"));
ImageIO.write(img, "jpg", baos);
row.set(0, baos);

com.aspose.words.Document doc = new com.aspose.words.Document("E:/data/ImageTemplate.docx");

doc.getMailMerge().setFieldMergingCallback(new HandleImageMergeField());


private static class HandleImageMergeField 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 {
	    ByteArrayOutputStream baos = (ByteArrayOutputStream) e.getFieldValue();
        ByteArrayInputStream imageStream = new ByteArrayInputStream(baos.toByteArray());
1 Like