Change orientation for individual section

Hi,

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.

Thanks,
Hanh

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.

Hi,

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

Thanks,

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
LayoutEnumerator from here:
https://reference.aspose.com/words/net/aspose.words.layout/layoutcollector/
https://reference.aspose.com/words/net/aspose.words.layout/layoutenumerator/

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.

Hi,

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);
var pageIndex = 1;
foreach (Paragraph para in paragraphs)
{
    if (collector.GetStartPageIndex(para) == pageIndex && para.GetAncestor(NodeType.GroupShape) == null)
    {
        templatebuilder.MoveTo(para);
        templatebuilder.InsertBreak(BreakType.SectionBreakContinuous);
        pageIndex++;
    }
}

foreach (Section section in templatedoc.Sections)
{

    foreach (Table table in section.Body.Tables)
    {
        if (table.Rows[0].Cells.Count < 5) continue;
        section.PageSetup.Orientation = Orientation.Landscape;
    }

}

But with above code I’ve an issue. If I insert Section Break Continuous before first paragraph in page,
This document will insert blank page above this page or automatic insert another paragraph.
I don’t know how to fix this issue.
Please help me to get solution. I hope I will be received your answer soon.
Many thanks,

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.

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, "");
            para.Runs.Add(afterRun);
            builder.MoveTo(afterRun);
            builder.StartBookmark("bookmark" + i);
            builder.EndBookmark("bookmark" + i);
            i++;
        }
        pageIndex++;
    }
}
foreach (Bookmark bookmark in doc.Range.Bookmarks)
{
    if (bookmark.Name.StartsWith("bookmark"))
    {
        builder.MoveTo(bookmark.BookmarkStart);
        builder.InsertBreak(BreakType.SectionBreakContinuous);
        bookmark.BookmarkStart.ParentNode.Remove();
    }
}
doc.Save(MyDir + "Out.docx");