Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.
Tue Sep 17, 2019 8:33 pm
I would like to merge some html into a mergefield. Right now after the merge, the field values are merged as plain text without the html formatting. Is there a way to merge the field values as formatted html?
-

dchillmanspire
-
- Posts: 2
- Joined: Tue Sep 17, 2019 8:28 pm
Wed Sep 18, 2019 7:28 am
Hi,
Thanks for your inquiry.
Please refer to the code below to insert formatted html into MergeField in Word document.
- Code: Select all
Document document = new Document(@"……\filename.docx");
string[] filedNames = new string[] { "Htmlname", "Htmlage" };
string[] filedValues = new string[] { "htmltext1", " htmltext2" };
document.MailMerge.MergeField += MailMerge_MergeField;
document.MailMerge.Execute(filedNames, filedValues);
document.SaveToFile("result.docx", FileFormat.Docx2013);
}
public static void MailMerge_MergeField(object sender, MergeFieldEventArgs args)
{
//The merge fields that expect HTML data should be marked with some prefix, for instance,'Html'.
if (args.FieldName.StartsWith("Html"))
{
args.CurrentMergeField.OwnerParagraph.AppendHTML(args.FieldValue as string);
args.Text = "";
}
}
If the code couldn't solve your issue, to help us better investigate it, please offer us your input html and your desired result as well as the complete code you were using. You could upload them here or send us(
[email protected]) via email.
Best wishes,
Amber
E-iceblue support team
-


Amber.Gu
-
- Posts: 525
- Joined: Tue Jun 04, 2019 3:16 am
Wed Sep 18, 2019 1:56 pm
thanks - this works perfectly.
-

dchillmanspire
-
- Posts: 2
- Joined: Tue Sep 17, 2019 8:28 pm
Thu Sep 19, 2019 1:01 am
Hi,
Thanks for your feedback.
Any question, welcome to contact us.
Best wishes,
Amber
E-iceblue support team
-


Amber.Gu
-
- Posts: 525
- Joined: Tue Jun 04, 2019 3:16 am