DocumentBuilderInsertHtml Method (String)
Inserts an HTML string into the document.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.12
public void InsertHtml(
	string html


Type: SystemString
An HTML string to insert into the document.

You can use InsertHtml to insert an HTML fragment or whole HTML document.

Inserts MathMl into a document using.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

const String mathMl =
    "<math xmlns=\"\"><mrow><msub><mi>a</mi><mrow><mn>1</mn></mrow></msub><mo>+</mo><msub><mi>b</mi><mrow><mn>1</mn></mrow></msub></mrow></math>";

Inserts HTML into a document. The formatting specified in the HTML is applied.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

string html = "<P align='right'>Paragraph right</P>" + "<b>Implicit paragraph left</b>" +
              "<div align='center'>Div center</div>" + "<h1 align='left'>Heading 1 left.</h1>";


doc.Save(ArtifactsDir + "DocumentBuilder.InsertHtml.doc");
Replaces text specified with regular expression with HTML.
// Open the document.
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    builder.Writeln("Hello <CustomerName>,");

    FindReplaceOptions options = new FindReplaceOptions();
    options.ReplacingCallback = new ReplaceWithHtmlEvaluator(options);

    doc.Range.Replace(new Regex(@" <CustomerName>,"), String.Empty, options);

    // Save the modified document.
    doc.Save(ArtifactsDir + "Range.ReplaceWithInsertHtml.doc");


private class ReplaceWithHtmlEvaluator : IReplacingCallback
    internal ReplaceWithHtmlEvaluator(FindReplaceOptions options)
        mOptions = options;

    /// <summary>
    /// NOTE: This is a simplistic method that will only work well when the match
    /// starts at the beginning of a run.
    /// </summary>
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
        DocumentBuilder builder = new DocumentBuilder((Document) args.MatchNode.Document);

        // Replace '<CustomerName>' text with a red bold name.
        builder.InsertHtml("<b><font color='red'>James Bond, </font></b>");
        args.Replacement = "";

        return ReplaceAction.Replace;

    private readonly FindReplaceOptions mOptions;
Shows how to mail merge HTML data into a document.
// File 'MailMerge.InsertHtml.doc' has merge field named 'htmlField1' in it.
    // File 'MailMerge.HtmlData.html' contains some valid HTML data.
    // The same approach can be used when merging HTML data from database.
    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();

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

    // Save resulting document with a new name.
    doc.Save(ArtifactsDir + "MailMerge.InsertHtml.doc");

private class HandleMergeFieldInsertHtml : IFieldMergingCallback
    /// <summary>
    /// This is called when merge field is actually merged with data in the document.
    /// </summary>
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
        // All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'.
        if (args.DocumentFieldName.StartsWith("html") && args.Field.GetFieldCode().Contains("\\b"))
            FieldMergeField field = args.Field;

            // Insert the text for this merge field as HTML data, using DocumentBuilder.
            DocumentBuilder builder = new DocumentBuilder(args.Document);
            builder.InsertHtml((string) args.FieldValue);

            // The HTML text itself should not be inserted.
            // We have already inserted it as an HTML.
            args.Text = "";

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
        // Do nothing.
See Also