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);
sdt.AppendChild(markerParagraph);
DocumentBuilder builder = new DocumentBuilder(target);
builder.MoveTo(markerParagraph);
builder.InsertBreak(BreakType.SectionBreakContinuous);
builder.CurrentSection.PageSetup.LeftMargin = 60; // 36 (Our special size)
builder.CurrentSection.PageSetup.RightMargin = 5; // 5 (Our special size)
string sdtHtml = "<sdt xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>" +
"<sdtPr><alias w:val='SDTAlias'/></sdtPr>" +
"<sdtContent><p>Content of the SDT</p></sdtContent>" +
"</sdt>";
builder.InsertHtml(sdtHtml);
builder.InsertBreak(BreakType.SectionBreakContinuous);
builder.CurrentSection.PageSetup.LeftMargin = 72;
builder.CurrentSection.PageSetup.RightMargin = 72;
sdt.RemoveAllChildren();
Appreciate any help or suggestions.
Best,
Mark
@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
builder.MoveTo(beforePara);
builder.InsertBreak(BreakType.SectionBreakContinuous);
if (!builder.CurrentParagraph.HasChildNodes)
builder.CurrentParagraph.Remove();
beforePara.Remove();
builder.MoveTo(afterPara);
builder.InsertBreak(BreakType.SectionBreakContinuous);
if (!builder.CurrentParagraph.HasChildNodes)
builder.CurrentParagraph.Remove();
afterPara.Remove();
// change margins of the SDT's parent section.
Section sdtSection = (Section)sdt.GetAncestor(NodeType.Section);
sdtSection.PageSetup.LeftMargin = 10;
sdtSection.PageSetup.RightMargin = 10;
}
doc.Save(@"C:\\Temp\\out.docx");
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;
doc.Save(@"C:\Temp\out.docx");
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.