Make a page orientation

Hi,
I am using Aspose.Words for .NET, When i am trying to convert a page orientation it converts all the pages instead of the single page.Please find the below code.

public override VisitorAction VisitParagraphStart(Paragraph paragraph)
{
    if (paragraph.Range.Text.Contains("<<$TOC$>>"))
    {
        paragraph.Runs[0].Text = "Table of Contents";
        Document doc = (Document) paragraph.Document;
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.MoveTo(paragraph);
        builder.CurrentSection.PageSetup.Orientation = Orientation.Landscape;
        builder.InsertBreak(BreakType.PageBreak);
        builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
        builder.InsertBreak(BreakType.PageBreak);
        doc.UpdateFields();
    }
    else if (paragraph.Range.Text.Contains("<<$BIAs$>>"))
    {
        paragraph.Runs[0].Text = "BIA tables should go here";
        paragraph.CenterParagraph();
    }

    return base.VisitParagraphStart(paragraph);
}

Could anyone help me in this requirement.

Regards,
Kavi

Hi there,

Thanks for your inquiry. PageSetup class represents the page setup properties of a section. PageSetup object contains all the page setup attributes of a section (Orientation, left margin, bottom margin, paper size, and so on) as properties.

Section class represents a single section in a document. Section can have one Body and maximum one HeaderFooter of each HeaderFooterType. Body and HeaderFooter nodes can be in any order inside Section.

A minimal valid section needs to have Body with one Paragraph. Each section has its own set of properties that specify page size, orientation, margins etc.
https://docs.aspose.com/words/net/working-with-sections/

In your case, I suggest you please insert break of type SectionBreakNewPage instead of type PageBreak in your document. Hope this helps you. Please let us know if you have any more queries.

Hi,
I am trying to Write the whole html document into the word document, So Aspose write the whole html document into a single section. So whenever i am trying to change the page orientation of the particular paragraph’s section it is converting the whole document(single section). How to handle this situation.

Regards,
Kavi

Hi Kavi,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you open your HTML in MS Word, you will get the same output (one section).

In your case, I suggest you please insert the section break continuous at the start of each page. You can insert section break after first paragraph of each page using following code example. Hope this helps you. Please let us know if you have any more queries.

var doc = new Document(MyDir + "in.html");
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
LayoutCollector collector = new LayoutCollector(doc);
int pageIndex = 1;
foreach(Paragraph para in paragraphs)
{
    if (collector.GetStartPageIndex(para) == pageIndex && para.GetAncestor(NodeType.GroupShape) == null)
    {
        builder.MoveTo(para);
        builder.InsertBreak(BreakType.SectionBreakContinuous);
        pageIndex++;
    }
}
int i = 1;
foreach(Section section in doc.Sections)
{
    if (i % 2 == 0)
        section.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
    else
        section.PageSetup.Orientation = Aspose.Words.Orientation.Portrait;
    i++;
}
doc.Save(MyDir + "Out.docx");
1 Like