Creating page margins and sections to insert text

I would like to modify the contents of a StructuredDocumentTag to contain page margins that are specific to that sdt and then insert content into that sdt (using the margins of that sdt). I thought the below code would achieve that, however, the content appears at the bottom of the page and the page breaks that I wanted continuous are “Page Breaks”. In the below code I am using insertHtml to create the SDT that will contain the text i would like to insert. Am I completely doing the wrong thing? or how can I achieve creating text using specific margin sizes.

var markerParagraph = new Paragraph(target);

DocumentBuilder builder = new DocumentBuilder(target);

builder.CurrentSection.PageSetup.LeftMargin = 60;       // 36   (Our special size) 
builder.CurrentSection.PageSetup.RightMargin = 5;       // 5    (Our special size)
string sdtHtml = "<sdt xmlns:w=''>" +
                         "<sdtPr><alias w:val='SDTAlias'/></sdtPr>" +
                         "<sdtContent><p>Content of the SDT</p></sdtContent>" +

builder.CurrentSection.PageSetup.LeftMargin = 72;
builder.CurrentSection.PageSetup.RightMargin = 72;


Appreciate any help or suggestions.

@markchequer If I understand your requirements properly, it is required to put an SDT into separate section and specify margins for this section. You can achieve this using the following ocde:

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// Get SDT, which shoul dbe put into a separate section.
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);

// Make sure we work with block level SDT.
if (sdt.Level == MarkupLevel.Block)
    // Put paragraph before SDT and after it. The paragraphs will be uses as markers.
    Paragraph beforePara = sdt.ParentNode.InsertBefore(new Paragraph(doc), sdt);
    Paragraph afterPara = sdt.ParentNode.InsertAfter(new Paragraph(doc), sdt);

    // Insert section break before and after SDT. Then remove marker paragraphs
    if (!builder.CurrentParagraph.HasChildNodes)

    if (!builder.CurrentParagraph.HasChildNodes)

    // change margins of the SDT's parent section.
    Section sdtSection = (Section)sdt.GetAncestor(NodeType.Section);
    sdtSection.PageSetup.LeftMargin = 10;
    sdtSection.PageSetup.RightMargin = 10;


in.docx (21.2 KB)
out.docx (17.9 KB)

Yes, this does work, with the exception that the second “break” is being created as a pagebreak not continuous…

SectionProblem.docx (51.5 KB)

Attached is the document i am using if that helps

@markchequer As I can see there is section break next page after the SDT in your document. Do you need to replace it with section break continuous? If so, you can achieve this using the following code:

Document doc = new Document(@"C:\Temp\in.docx");
doc.LastSection.PageSetup.SectionStart = SectionStart.Continuous;

out.docx (43.1 KB)

I can see that aspose is creating the sections correctly, the problem seems to be when i load this document into word using office js using

“body.insertFileFromBase64(content, Word.InsertLocation.start);”

I’ll investigate further, thank you.

@markchequer It is perfect that you managed to find the reason of the problem. Please feel free to ask in case of any other issues, we will be glad to help you.