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

Free Support Forum - aspose.com

Rich Text Format in WORD

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,


Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • 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.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you code to achieve the same using Aspose.Words. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

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,


Thanks for your inquiry. Please use the following code:
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.

}
}

I hope, this helps.

Best regards,