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

Free Support Forum - aspose.com

How to display images in WORD/PDF generated from XML

Dear Team,

I am working on a requirement where I need to display the Images in WORD/PDF Document. Here the Image data is being encoded in base64 encoding format and data is being sent in XML. When I am using the merge fields, the image data is not being displayed.

For reference I am attaching the sample XML and word template which I am using to convert into PDF and display.

Kindly help in giving some inputs on how to proceed further.

Thanks and regards

Pradeep Goli

Hi Pradeep,

Thanks for your inquiry. In your case, we suggest you please implement IFieldMergingCallback interface to achieve your requirements as shown in following code example.

Please specify a field name prefix like Image:MyFieldName in the document to be able to directly merge images during Mail Merge. Please convert base64 string into InputStream and use ImageFieldMergingArgs.ImageStream property to specify the stream for the mail merge engine to read an image.

We have attached the modified template document and output Pdf with this post for your kind reference.

Document doc = new Document(MyDir + "Template_modified.doc");

DataSet ds = new DataSet();

ds.readXml(MyDir + "Test XML.xml");

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


doc.save(MyDir + "Out.docx");

doc.save(MyDir + "Out.pdf");

class MyFieldMergingCallback implements IFieldMergingCallback {

public void fieldMerging(FieldMergingArgs args) throws Exception {


public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {



byte[] decoded = Base64.decode(args.getFieldValue().toString());

InputStream is = new ByteArrayInputStream(decoded);