Free Support Forum - aspose.com

InsertHTML Method is ruining ParagraphFormat for the entire document

I have a simple test setup in which I add some HTML to a document & then try to format the paragraph after the InsertHTML method is called.


What I am trying to do is have the Writeln method called after InsertHTML have 0 spacing before & after the text. Currently, the Writeln writes the text with “auto” spacing after (ParagraphFormat.SpaceAfter is incorrectly set to Auto instead of “0”, even though it is explicitly called).

Here is the code:

	public Document createTestReport()
{
Document doc = new Document();
DocumentBuilder db = new DocumentBuilder(doc);
    db.InsertParagraph();
    db.ParagraphFormat.SpaceAfter = 0;
    db.ParagraphFormat.SpaceBefore = 0;

	db.InsertHtml(<span style="color:#a31515;">"<p>BlahBlahBlah</p><ul><li>Some List Item</li><li>Another</li></ul>"</span>);

	db.Font.Bold = <span style="color:blue;">true</span>;
    db.ParagraphFormat.SpaceAfter = 0;
    db.ParagraphFormat.SpaceBefore = 0;
	db.Writeln(<span style="color:#a31515;">"Some Title"</span>);

	<span style="color:blue;">return</span> doc;
}</pre></div>
Hi Dan,

Thanks for your inquiry. Please note that, content inserted by DocumentBuilder.insertHtml method does not inherit formatting specified in DocumentBuilder options. Whole formatting is taken from HTML snippet. If you insert HTML with no formatting specified, then default formatting is used for inserted content.

Please call ParagraphFormat.ClearFormatting method after inserting the HTML. This method resets to default paragraph formatting.

Document doc = new Document();

DocumentBuilder db = new DocumentBuilder(doc);

db.InsertParagraph();

db.ParagraphFormat.SpaceAfter = 0;

db.ParagraphFormat.SpaceBefore = 0;

db.InsertHtml("

BlahBlahBlah

  • Some List Item
  • Another
");

db.ParagraphFormat.ClearFormatting();

db.Font.Bold = true;

db.ParagraphFormat.SpaceAfter = 0;

db.ParagraphFormat.SpaceBefore = 0;

db.Writeln("Some Title");