How to Change Orientation of Single Paragraph or Page using Open XML and C#?

c#, export-to-word, openxml

Solution

You have to put a next page section break to begin a new section, and set the orientation for that section. Once you are done, start a new section with the default page orientation.

Next page Section Break to start landscape orientation:

doc.MainDocumentPart.Document.Body.Append(
    new Paragraph(
        new ParagraphProperties(
            new SectionProperties(
                new PageSize() { Width = (UInt32Value)12240U, Height = (UInt32Value)15840U, Orient = PageOrientationValues.Landscape },
                new PageMargin() { Top = 720, Right = Convert.ToUInt32(rightmargin * 1440.0), Bottom = 360, Left = Convert.ToUInt32(leftmargin * 1440.0), Header = (UInt32Value)450U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U }))));

Next page Section Break to start portrait orientation:

doc.MainDocumentPart.Document.Body.Append(
    new Paragraph(
        new ParagraphProperties(
            new SectionProperties(
                new PageSize() { Width = (UInt32Value)12240U, Height = (UInt32Value)15840U },
                new PageMargin() { Top = 720, Right = Convert.ToUInt32(rightmargin * 1440.0), Bottom = 360, Left = Convert.ToUInt32(leftmargin * 1440.0), Header = (UInt32Value)450U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U }))));

Problem

I am generating a word document using C# and Open XML SDK. I want to change the orientation of some paragraphs and their containing pages to landscape while keeping the other ones in portrait orientation. I tried some solutions, but they didn't achieve what I am looking for and instead the orientation of all the pages have been changed to landscape except the first one. These solutions include: - Adding SectionProperties to the paragraph that I want to change its orientation to landscape: ``` WordprocessingDocument WordDocument = WordprocessingDocument.Open(ReportFile, true) Paragraph paragraph = new Paragraph(new ParagraphProperties( new SectionProperties( new PageSize() { Width = (UInt32Value)15840U, Height = (UInt32Value)12240U, Orient = PageOrientationValues.Landscape }))); WordDocument.MainDocumentPart.Document.Body.Append(paragraph); ``` - Adding a new Body to the main document and appending the paragraph to it: ``` WordprocessingDocument WordDocument = WordprocessingDocument.Open(ReportFile, true) Body body = new Body(); Paragraph paragraph = new Paragraph(new ParagraphProperties( new SectionProperties( new PageSize() { Width = (UInt32Value)15840U, Height = (UInt32Value)12240U, Orient = PageOrientationValues.Landscape }))); body.Append(paragraph); WordDocument.MainDocumentPart.Document.Append(body); ``` - Adding a new Body to the main document and appending SectionProprties to it directly: ``` WordprocessingDocument WordDocument = WordprocessingDocument.Open(ReportFile, true) Body body = new Body(); Paragraph paragraph = new Paragraph(); SectionProperties sectionProp = new SectionProperties(new PageSize() { Width = (UInt32Value)15840U, Height = (UInt32Value)12240U, Orient = PageOrientationValues.Landscape }); body.Append(paragraph); body.Append(sectionProp); WordDocument.MainDocumentPart.Document.Append(body); ``` So, is there a way for changing the orientation of a single paragraph/page?

Original source