We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

CSS left-margin of paragraph starts at page edge

Hi there
We use

  • a word template
  • HTML formated text
  • style attribute padding-left (indent)

<p style="padding-left: 40px;">Test</p>

The paragraph in which we insert the HTML text is already indented (Word template)

We expect the additional margin in the style attribute of the HTML paragraph tag to be added to the existing indentation in the Word template.
Instead when using padding-left the paragraph is indented from the page’s edge.

Is this a missing feature or is there a way to create that behaviour in the Word template?

@pf.sminion This is expected behavior. Because your HTML produces a separate paragraph, which has it’s own indent:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.ParagraphFormat.LeftIndent = 100;
builder.Write("Paragraph with indent");
builder.InsertHtml("<p style=\"padding-left: 40px;\">Test</p>"); // This will create a separate paragrph.
doc.Save(@"C:\Temp\out.docx");

So there is either the existing LeftIndet formatting or the one set in the HTML style attribute. They are mutually exclusive and can’t be added up. Correct?

@pf.sminion Yes, you are correct.

@pf.sminion You can work this around by updating the inserted paragraphs. For example you can use the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.ParagraphFormat.LeftIndent = 100;
builder.Write("Paragraph with indent");
// Remember the current paragraph.
Paragraph currentPara = builder.CurrentParagraph;
builder.InsertHtml("<p style=\"padding-left: 40px;\">Test</p>"); // This will create a separate paragrph.
// Now adjust the inserted paragraphs indents.
Node currentNode = currentPara.NextSibling;
while (currentNode != null && currentNode != builder.CurrentParagraph)
{
    if (currentNode.NodeType == NodeType.Paragraph)
        ((Paragraph)currentNode).ParagraphFormat.LeftIndent += currentPara.ParagraphFormat.LeftIndent;
    currentNode = currentNode.NextSibling;
}
doc.Save(@"C:\Temp\out.docx");