Similar to this post (Margin-top, margin-bottom, margin-left, margin-right not working), I am having an issue with inserting HTML into a Word Doc (via DocumentBuilder.InsertHtml) and the resulting formatting in the document not using the indent settings (margins) from within the unordered list within the HTML.
HTML inserted (note the removed <'s for visibility):
<ul style="margin-left:0;">
<li style="margin-left:0;"><span style="margin-left: 1cm;">Coffee </span> </li>
<li>Tea </li>
</ul>
HTML displayed in browser:

Result inserted into Word doc:

Could you please recommend an alternative to either the HTML or insert method that will result in the HTML being inserted into the Word Doc with the margins used (i.e. the spacing between the bullet point and “Coffee”).
Thank you for your help.
@daveywc
To ensure that the HTML margins are respected when inserting an unordered list into a Word document using Aspose.Words, you can utilize the HtmlInsertOptions parameter with the InsertHtml method. This approach allows you to specify how the HTML should be processed, ensuring that your formatting is preserved.
Cause
The default behavior of the InsertHtml method may not respect the margins defined in your HTML, leading to formatting issues in the Word document.
Solution
To address this, you can set the UseBuilderFormatting property of HtmlInsertOptions to false. This will ensure that the method respects the HTML’s own styling, including margins and indents.
Here’s a code snippet that demonstrates how to insert your HTML with the desired formatting:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create HtmlInsertOptions to control the insertion behavior
HtmlInsertOptions options = new HtmlInsertOptions
{
UseBuilderFormatting = false // This will ignore DocumentBuilder's formatting
};
// Your HTML string
string html = @"
<ul style='margin-left:0;'>
<li style='margin-left:0;'><span style='margin-left: 1cm;'>Coffee</span></li>
<li>Tea</li>
</ul>";
// Insert the HTML into the document
builder.InsertHtml(html, options);
// Save the document
doc.Save("output.docx");
Next Steps
If you continue to experience issues with the formatting, consider adjusting the HTML or using additional CSS styles to fine-tune the appearance. You may also want to test different margin values to achieve the desired look.
Please let me know if this resolves your issue or if you have any further questions!
I’ve tried setting the HtmlFormatOptions (enum) to not use the builder formatting and other settings along those lines but it has not changed the output.
I am also forced to use an older version of Aspose (~23.7) due to memory issues experienced on v25.4.
@daveywc First of all, please note, Aspose.Words is designed to work with MS Word documents. HTML documents and MS Word documents object models are quite different and it is not always possible to provide 100% fidelity after conversion one format to another. In most cases Aspose.Words mimics MS Word behavior when work with HTML.
If you convert your HTML to DOCX using MS Word the result is pretty much the same as Aspose.Words output.
Aspose.Words: out.docx (8.4 KB)
MS Word: ms.docx (13.4 KB)
As an option, you can use spaces to move content from the bullet point.
<ul>
<li><span> </span><span>Coffee</span></li>
<li>Tea</li>
</ul>
Thank you for your response. I will explore other options for the HTML; however, your suggested change is not suitable as I do need a specific width (in cm) of space between the bullet point and text (hence the reason for setting the margin to 1cm).
If you have any other suggestions on how I can achieve this specific width gap, I’d appreciate it.
Thanks again for your help.
@daveywc You can try using padding-left attribute:
<html>
<body>
<ul>
<li style="padding-left: 1cm;">Coffee</li>
<li>Tea </li>
</ul>
</body>
</html>
out.docx (8.5 KB)
1 Like
Thank you @alexey.noskov, using the padding-left attribute does impact the alignment in the Word doc when the HTML is inserted.
Thank you for all your help.
1 Like