Free Support Forum - aspose.com

How to merge HTML encode data to Merge field

Hi Sir,

I am using a mail merge in Asopose.words . I am facing problem merging the fields with HTML encoded data from database.
My scenario here is, i am using CKEditor to save the text into the database as HTML encoded text. When I retrive back from database and merge with the merge fields in the template, it is getting merged along with the HTML tags and other characters.

How to display only the main content?

Any solutions?. Please help me.


Regards
Thanks You

Hi Chhodup,


Thanks for your inquiry. In your case, I suggest you to use IFieldMergingCallback interface to achieve your requirements. Please read following documentation links for your kind reference.
http://www.aspose.com/docs/display/wordsnet/IFieldMergingCallback+Members
http://www.aspose.com/docs/display/wordsnet/How+to++Insert+Images+from+a+Database

Please check the following code snippet for your kind reference. This code example shows how to mail merge HTML data into a document.

<span style=“font-size:10.0pt;
font-family:“Courier New”;color:green;mso-font-kerning:0pt;mso-ansi-language:
PL;mso-no-proof:yes”>// File ‘MailMerge.InsertHtml.doc’ has merge field named
‘htmlField1’ in it.<o:p></o:p>

// File 'MailMerge.HtmlData.html' contains some valid Html data.

// The same approach can be used when merging HTML data from database.

public void MailMergeInsertHtml()

{

Document doc = new Document(MyDir + "MailMerge.InsertHtml.doc");

// Add a handler for the MergeField event.

doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();

// Load some Html from file.

StreamReader sr = File.OpenText(MyDir + "MailMerge.HtmlData.html");

string htmltext = sr.ReadToEnd();

sr.Close();

// Execute mail merge.

doc.MailMerge.Execute(new string[] { "htmlField1" }, new string[] { htmltext });

// Save resulting document with a new name.

doc.Save(MyDir + "MailMerge.InsertHtml Out.doc");

}

private class HandleMergeFieldInsertHtml : IFieldMergingCallback

{

///

/// This is called when merge field is actually merged with data in the document.

///

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)

{

// All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'.

if (e.DocumentFieldName.StartsWith("html"))

{

// Insert the text for this merge field as HTML data, using DocumentBuilder.

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToMergeField(e.DocumentFieldName);

builder.InsertHtml((string)e.FieldValue);

// The HTML text itself should not be inserted.

// We have already inserted it as an HTML.

e.Text = "";

}

}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)

{

// Do nothing.

}

}