|
private static void MailMerge_MergeField(object sender, MergeFieldEventArgs args)
{ var paragraph = args.CurrentMergeField.OwnerParagraph; var paraIndex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph); var fieldIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField); bool isValidHtml = paragraph.Document.LastSection.Body.IsValidXHTML(args.FieldValue.ToString(),paragraph.Document.XHTMLValidateOption); if (isValidHtml) { paragraph.OwnerTextBody.InsertXHTML(args.FieldValue.ToString(), paraIndex, fieldIndex); args.Text = string.Empty; } else { args.Text = "insert failed"; } } |
|
//Loads the template document WordDocument document = new WordDocument(); document.EnsureMinimal(); //Html string to be inserted string htmlstring = "<html><body><h1>test</h1></body></html>"; //Validates the Html string bool isValidHtml = document.LastSection.Body.IsValidXHTML(htmlstring, XHTMLValidationType.Transitional); //When the Html string passes validation, it is inserted to the document if (isValidHtml) { //Appends Html string as first item of the second paragraph in the document document.Sections[0].Body.InsertXHTML(htmlstring, 0, 0); } //Saves and closes the document document.Save("Sample.docx"); document.Close(); |
|
//Html string to be inserted string htmlstring = File.ReadAllText(@"D:\FullHTML.txt"); //Validates the Html string bool isValidHtml = document.LastSection.Body.IsValidXHTML(htmlstring, XHTMLValidationType.Transitional); |
|
//Html string to be inserted
string htmlstring = "<html><body>test<br/>break</body></html>"
|