Hi,
Based on some of the old threads I have created my own implementation of IMailMergeDataSource to parse json and use that for the mail merge. However, what I’m not sure how to get dynamic rows of a table populated in the merged document.
I have attached the implementation and the template.
attachments.zip (24.8 KB)
Here is the code which generates the merged document using the template
public byte[] mergeWordDocWithRegions(byte[] templateStream, IMailMergeDataSource ds) {
ByteArrayOutputStream docOutStream = new ByteArrayOutputStream();
try {
Document doc = new Document(new ByteArrayInputStream(templateStream));
doc.getMailMerge().setUseNonMergeFields(true);
// This is to cleanup the mergeFields in the word template for which data has not been filled in
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS);
// Execute the mail merge.
//doc.getMailMerge().execute(fieldNames, fieldValues);
doc.getMailMerge().execute(ds);
LogHelper.info(LOG, "Mail merge performed successfully.");
doc.save(docOutStream, SaveFormat.PDF);
LogHelper.info(LOG, "Survey has been saved as PDF format");
} catch (Exception e) {
LogHelper.error(LOG, "Exception stack" + ExceptionUtils.getStackTrace(e));
}
return docOutStream.toByteArray();
}
Regards,
Sandesh