Dear Team,
I would like to know how to process the richtext data received for some fields and insert to a mergefield. Here I am using the method doc.getMailMerge().execute() and directly passing the data to get merged to fields in Word Document.
For e.g. data received contains like paragraphs tags
Kindly help in resolving the issue
Thanks and Regards
Pradeep Goli
Hi Pradeep,
- Your input Word document containing template
- String representing the richtext data. (Copy the sting in .txt file and attach it)
- Your expected document which shows the correct output. Please create this document using Microsoft Word application.
Dear Team,
I could achieve the HTML Formatting for simple merge fields through the code “builder.insertHtml” writing this handler method i.e.
if (“FIELDNAME”.equals(e.getFieldName()))
{
// Insert the text for this merge field as HTML data, using DocumentBuilder.
DocumentBuilder builder = new DocumentBuilder(e.getDocument());
builder.moveToMergeField(e.getDocumentFieldName());
builder.insertHtml((String) e.getFieldValue());
// The HTML text itself should not be inserted.
// We have already inserted it as an HTML.
e.setText("");
}
But here I am facing a new issue i.e. we have a requirement to include the Rich Text data inside a table cell i.e. merge fields.
Please find the test case documents attached here for reference.
Thanks and Regards
Pradeep Goli
Hi Pradeep,
DataTable dataTable = new DataTable(“DEPARTMENT”);dataTable.getColumns().add(“DEP_NAME”);
DataRow row = dataTable.newRow();
row.set(0, “HTML VALUE”);
dataTable.getRows().add(row);Document doc = new Document(getMyDir() + “Template MergeFields.doc”);
doc.getMailMerge().setFieldMergingCallback(new HandleMergeFieldInsertHtml());
doc.getMailMerge().executeWithRegions(dataTable);doc.save(getMyDir() + “15.7.0.docx”);
private static class HandleMergeFieldInsertHtml implements IFieldMergingCallback {
public void fieldMerging(FieldMergingArgs e) throws Exception {
if (e.getDocumentFieldName().equals(“DEP_NAME”)) {
DocumentBuilder builder = new DocumentBuilder(e.getDocument());
builder.moveToMergeField(e.getDocumentFieldName());
builder.insertHtml((String) e.getFieldValue());e<font color="BLUE"><b>.</b></font>setText<font color="BLUE"><b>(</b></font><font color="PURPLE">""</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> <font color="BLUE"><b>}</b></font> <font color="BLUE"><b>}</b></font> <font color="RED"><b>public</b></font> <font color="RED"><b>void</b></font> imageFieldMerging<font color="BLUE"><b>(</b></font>ImageFieldMergingArgs args<font color="BLUE"><b>)</b></font> <font color="RED"><b>throws</b></font> Exception <font color="BLUE"><b>{</b></font> <font color="GREEN"><i>// Do nothing.
}
}