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

Free Support Forum - aspose.com

Change orientation for individual section


I’m using Aspose Word to export report. I want set orientation for each page. My solution is: After built document, I had loop all of sections in document and check my condition, if section satisfy conditions, it will be orientation Landscape, otherwise Portrait. But I have issue: I don’t know how to insert section break for each page.

Please help me to give solution.


Hi Hanh,

Thanks for your inquiry. Could you please attach your input and expected output Word document here for testing? I will investigate the issue on my side and provide you more information.


Please see my input and my expect output.
I hope I will receive your answer soon.


Hi Hanh,

Thanks for sharing the detail. The output document does not contain the section break at every page.

You can achieve your requirements using Aspose.Words.Layout classes. Aspose.Words uses our own Rendering Engine to layout documents into pages. The Aspose.Words.Layout namespace provides
classes that allow to access information such as on what page and where
on a page particular document elements are positioned, when the document
is formatted into pages. Please read about LayoutCollector and
from here:

You will be able to find out on which page a particular document node (e.g. run, paragraph or table cell) is located by using the GetStartPageIndex, GetEndPageIndex and GetNumPagesSpanned methods. These methods automatically build page layout model of the document and update fields if required.

In your case, I suggest you following solution.

1) Please use LayoutCollector.GetStartPageIndex method to get the index of the page where node begins.
2) Move the cursor to the desired location. Read about moving cursor from here.
3) Insert section break of type continuous at your desired location using DocumentBuilder.InsertBreak.
4) Once you have insert the section break, you can change the page orientation using PageSetup.Orientation property.

Hope this helps you. Please let us know if you have any more queries.


Thanks for your feedback. I tried to use LayoutCollector class in order to break section. Following is my code :
var paragraphs = templatedoc.GetChildNodes(NodeType.Paragraph, true);
var collector = new LayoutCollector(templatedoc);
            <span style="color:blue;">var</span> pageIndex = 1;
            <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">Paragraph</span> para <span style="color:blue;">in</span> paragraphs)
                <span style="color:blue;">if</span> (collector.GetStartPageIndex(para) == pageIndex && para.GetAncestor(<span style="color:#2b91af;">NodeType</span>.GroupShape) == <span style="color:blue;">null</span>)
                    templatebuilder.InsertBreak(<span style="color:#2b91af;">BreakType</span>.SectionBreakContinuous);

            <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">Section</span> section <span style="color:blue;">in</span> templatedoc.Sections)

                <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">Table</span> table <span style="color:blue;">in</span> section.Body.Tables)
                    <span style="color:blue;">if</span> (table.Rows[0].Cells.Count < 5) <span style="color:blue;">continue</span>;
                    section.PageSetup.Orientation = <span style="color:#2b91af;">Orientation</span>.Landscape;

            }</pre><pre style="font-family: Consolas; font-size: 13px; background: white;">But with above code I've an issue. If I insert Section Break Continuous before first paragraph in page, </pre><pre style="font-family: Consolas; font-size: 13px; background: white;">This document will insert blank page above this page or automatic insert another paragraph. </pre><pre style="font-family: Consolas; font-size: 13px; background: white;">I don't know how to fix this issue.</pre><pre style="font-family: Consolas; font-size: 13px; background: white;">Please help me to get solution. I hope I will be received your answer soon.</pre><pre style="font-family: Consolas; font-size: 13px; background: white;">Many thanks,</pre></div>

Hi Hanh,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you insert a section break continuous at the start or end of paragraph, an empty paragraph is inserted in the document. Please use following code example to achieve your requirements. Hope this helps you.
<!–[if gte mso 9]>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0pt 5.4pt 0pt 5.4pt; mso-para-margin-top:0pt; mso-para-margin-right:0pt; mso-para-margin-bottom:8.0pt; mso-para-margin-left:0pt; line-height:107%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri",sans-serif; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}


Document doc = new Document(MyDir + "input.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

LayoutCollector layoutCollector = new LayoutCollector(doc);

var i = 1;

var pageIndex = 1;

var paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);

foreach (Paragraph para in paragraphs)


if (layoutCollector.GetStartPageIndex(para) == pageIndex && para.GetAncestor(NodeType.GroupShape) == null)


if (layoutCollector.GetStartPageIndex(((Section)para.GetAncestor(NodeType.Section)).Body.LastParagraph) != pageIndex)


Run afterRun = new Run(doc, "");



builder.StartBookmark("bookmark" + i);

builder.EndBookmark("bookmark" + i);






foreach (Bookmark bookmark in doc.Range.Bookmarks)


if (bookmark.Name.StartsWith("bookmark"))







doc.Save(MyDir + "Out.docx");