DocumentBuilderInsertHtml Method (String, Boolean)

Inserts an HTML string into the document.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.5
public void InsertHtml(
	string html,
	bool useBuilderFormatting


Type: SystemString
An HTML string to insert into the document.
Type: SystemBoolean
A value indicating whether formatting specified in DocumentBuilder is used as base formatting for text imported from HTML.

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


When useBuilderFormatting is false, DocumentBuilder formating is ignored and formatting of inserted text is based on default HTML formatting. As a result, the text looks as it is rendered in browsers.

When useBuilderFormatting is true, formatting of inserted text is based on DocumentBuilder formatting, and the text looks as if it were inserted with Write(String).

Shows how to insert Html content into a document using a builder while applying the builder's formatting.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Set the builder's text alignment
builder.ParagraphFormat.Alignment = ParagraphAlignment.Distributed;

// If we insert text while setting useBuilderFormatting to true, any formatting applied to the builder will be applied to inserted .html content
// However, if the html text has formatting coded into it, that formatting takes precedence over the builder's formatting
// In this case, elements with "align" attributes do not get affected by the ParagraphAlignment we specified above
    "<P align='right'>Paragraph right</P>" + "<b>Implicit paragraph left</b>" +
    "<div align='center'>Div center</div>" + "<h1 align='left'>Heading 1 left.</h1>", true);

doc.Save(ArtifactsDir + "DocumentBuilder.InsertHtmlWithFormatting.docx");
See Also