@parthiban.natarajan,
I think, you can build logic on the following code to get the desired output:
Document doc = new Document(@"C:\Temp\Aspose_19_09_pml_sample\\Member- English- Sample.docx");
MemoryStream wdStream = new MemoryStream();
doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();
string strDMRdrugswithexplanations = getPMLHtml();
if (strDMRdrugswithexplanations.IndexOf("href='PML.css'") > 0)
strDMRdrugswithexplanations = strDMRdrugswithexplanations.Replace("href='PML.css'", "href='" + "C:\\Temp\\Aspose_19_09_pml_sample\\PML.css'");
doc.MailMerge.Execute(new string[] { "PML" }, new string[] { strDMRdrugswithexplanations });
doc.Save(@"C:\Temp\Aspose_19_09_pml_sample\\21.10 custom code.docx");
internal class HandleMergeFieldInsertHtml : IFieldMergingCallback
{
public void ImageFieldMerging(ImageFieldMergingArgs args)
{
throw new NotImplementedException();
}
/// <summary>
/// This is called when merge field is actually merged with data in the document.
/// </summary>
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
//string fontStyle = "<head></head><body><font isUnicode='true' face='Arial' size=2><table><tr><td>Sample text with Custome font Embedded </td></tr></table></font><br><font isUnicode='true' face='Courier New' size=10><s>Sample Text </s>in <u>Courier New</u> font</font></body>";
// All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'.
if (string.Compare(e.FieldName, "PML", true) == 0 && e.FieldValue != null)
{
// Insert the text for this merge field as HTML data, using DocumentBuilder.
DocumentBuilder builder = new DocumentBuilder(e.Document);
builder.MoveToMergeField(e.DocumentFieldName);
HandleNodeChanging handler = new HandleNodeChanging();
e.Document.NodeChangingCallback = handler;
builder.InsertHtml(e.FieldValue.ToString(), true);
e.Document.NodeChangingCallback = null;
// Process Table(s) reference that we just inserted via InsertHtml
foreach (Table table in handler.InsertedTables)
if (string.IsNullOrEmpty(table.ToString(SaveFormat.Text).Trim()) && table.Rows.Count == 1)
table.Remove();
// Process Paragraph(s) reference that we just inserted via InsertHtml
foreach (Paragraph para in handler.InsertedParagraphs)
if (string.IsNullOrEmpty(para.ToString(SaveFormat.Text).Trim()) && para.ParagraphBreakFont.Hidden)
para.ParagraphBreakFont.Size = 1;
e.Text = "";
}
}
}
private static string getPMLHtml()
{
return File.ReadAllText(@"C:\Temp\Aspose_19_09_pml_sample\App_Data\PML.html");
}
public class HandleNodeChanging : INodeChangingCallback
{
void INodeChangingCallback.NodeInserted(NodeChangingArgs args)
{
if (args.Node.NodeType == NodeType.Table)
mInsertedTables.Add(args.Node);
if (args.Node.NodeType == NodeType.Paragraph)
mInsertedParagraphs.Add(args.Node);
}
void INodeChangingCallback.NodeInserting(NodeChangingArgs args)
{
// Do Nothing
}
void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)
{
// Do Nothing
}
void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)
{
// Do Nothing
}
public List<Node> InsertedTables
{
get { return mInsertedTables; }
}
public List<Node> InsertedParagraphs
{
get { return mInsertedParagraphs; }
}
private readonly List<Node> mInsertedTables = new List<Node>();
private readonly List<Node> mInsertedParagraphs = new List<Node>();
}