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?

Thanks.

1 Like

Hi
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);
        fis.close();

        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();
mm.setCleanupOptions(
        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    |
        MailMergeCleanupOptions.REMOVE_STATIC_FIELDS
        //MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS
);

// Trim whitespace from merge inputs.
mm.setTrimWhitespaces(true);

// Allows using 'Mustache' syntax for mail merge template.
mm.setUseNonMergeFields(true);

DataTable dataTable = new DataTable();
DataRow dataRow = dataTable.newRow();
dataRow.getColumn().add("DocumentImage");
dataTable.getRows().add(dataRow);

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

// Does not work
mm.execute(globalDataRow);

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

doc.save("./output.pdf");

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.

@LiamMitchell,

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);
dataTable.getColumns().add(ImageCol);
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);
dataTable.getRows().add(row);

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

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

doc.save("E:/Data/Imageout.docx");		

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());
        e.setImageStream(imageStream);
		
	}
}
1 Like