none
Can you show a way to for a Word document to begin with the page orientation in default/portrait and end in landscape within the same document? RRS feed

  • Question

  • I am trying to create a word document to that changes to landscape after reaching a certain point.  I have added a page break and changed the page orientation to landscape, however the entire document becomes landscape and not the ending portion.  I am not sure of the best approach for this issue.  I have attached a shortened version of my code to demonstrate my intentions.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    namespace HASPOnline
    {
        public class TestDoc2
        {
            public TestDoc2()
            {
                    string documentPath = @"C:\Users\me\Desktop\PageOrientationTest.docx";
                    using (WordprocessingDocument package = WordprocessingDocument.Create(documentPath, WordprocessingDocumentType.Document))
                    {
                        AddParts(package);
                        MainDocumentPart mainPart = package.MainDocumentPart;
                        Body body = package.MainDocumentPart.Document.Body;
                        //body.Append(PageOrientationPortrait());
                        Paragraph paraDocType = body.AppendChild(new Paragraph());
                        //Create run thats hold text that will be added to the paragraph
                        Run runDocHeader = paraDocType.AppendChild(new Run());
                        RunProperties rpDocHdr = new RunProperties();
                        rpDocHdr.Append(new Bold());
                        FontSize fs = new FontSize();
                        fs.Val = new StringValue("40");
                        rpDocHdr.Append(fs);
                        runDocHeader.AppendChild<RunProperties>(rpDocHdr);
                        //Add text to the run
                        runDocHeader.AppendChild(new Text("Health and Safety Plan"));
                        //Title Area
                        Paragraph paraHaspTitle = body.AppendChild(new Paragraph());
                        Run runTitle = paraHaspTitle.AppendChild(new Run());
                        RunProperties runproperties = runTitle.AppendChild(new RunProperties(new Bold()));
                        runTitle.AppendChild(new CarriageReturn());//Drop down a line
                        runTitle.AppendChild(new Bold());//Add bold to text
                        runTitle.AppendChild(new Text("Title: ") { Space = SpaceProcessingModeValues.Preserve });
                        //Title text
                        Run runTitleActual = paraHaspTitle.AppendChild(new Run());
                        runTitleActual.AppendChild(new Text("Will hold string variable for title here."));
                        //Insert Page Break here
                        Paragraph pageBreakParagraph1 = new Paragraph(new Run(new Break() { Type = BreakValues.Page }));
                        body.Append(pageBreakParagraph1);
                        
                        //Change to Landscape Orientation here
                        body.Append(PageOrientationLandScape());
                        //Add Paragraph
                        Paragraph paraTest = CreatePara(); 
                        body.Append(paraTest);
                    }
            }
            private static Paragraph CreatePara()
            {
                Paragraph para1 = new Paragraph();
                Run run1 = new Run(new Text("This is a paragraph."));
                para1.Append(run1);
                return para1;
            }
            private static SectionProperties PageOrientationPortrait()
            {
                SectionProperties sectionProperties2 = new SectionProperties();
                PageSize pageSize = new PageSize()
                {
                    Width = (UInt32Value)12240U,
                    Height = (UInt32Value)15840U,
                    Orient = PageOrientationValues.Portrait
                };
                PageMargin pageMargin = new PageMargin()
                {
                    Top = 1440,
                    Right = (UInt32Value)1440U,
                    Bottom = 1440,
                    Left = (UInt32Value)1440U,
                    Header = (UInt32Value)720U,
                    Footer = (UInt32Value)720U,
                    Gutter = (UInt32Value)0U
                };
                Columns columns = new Columns() { Space = "720" };
                DocGrid docGrid = new DocGrid() { LinePitch = 360 };
                sectionProperties2.Append(pageSize, pageMargin, columns, docGrid);
                return sectionProperties2;
            }
            private static SectionProperties PageOrientationLandScape()
            {
                SectionProperties sectionProperties = new SectionProperties();
                PageSize pageSize = new PageSize()
                {
                    Width = (UInt32Value)15840U,
                    Height = (UInt32Value)12240U,
                    Orient = PageOrientationValues.Landscape
                };
                PageMargin pageMargin = new PageMargin()
                {
                    Top = 1440,
                    Right = (UInt32Value)1440U,
                    Bottom = 1440,
                    Left = (UInt32Value)1440U,
                    Header = (UInt32Value)720U,
                    Footer = (UInt32Value)720U,
                    Gutter = (UInt32Value)0U
                };
                Columns columns = new Columns() { Space = "720" };
                DocGrid docGrid = new DocGrid() { LinePitch = 360 };
                sectionProperties.Append(pageSize, pageMargin, columns, docGrid);
                return sectionProperties;
            }
            
            private static void AddParts(WordprocessingDocument parent)
            {
                string headerText = "HASP#:                                                                Expiration Date:";
                    var mainDocumentPart = parent.AddMainDocumentPart();
                    GenerateMainDocumentPart().Save(mainDocumentPart);
                    var documentSettingsPart = mainDocumentPart.AddNewPart<DocumentSettingsPart>("rId1");
                    GenerateDocumentSettingsPart().Save(documentSettingsPart);
                    var firstPageHeaderPart = mainDocumentPart.AddNewPart<HeaderPart>("rId2");
                    GeneratePageHeaderPart(headerText).Save(firstPageHeaderPart);
                    var evenPageHeaderPart = mainDocumentPart.AddNewPart<HeaderPart>("rId4");
                    GeneratePageHeaderPart(headerText).Save(evenPageHeaderPart);
                    var oddPageheaderPart = mainDocumentPart.AddNewPart<HeaderPart>("rId6");
                    GeneratePageHeaderPart(headerText).Save(oddPageheaderPart);
            }
            private static Document GenerateMainDocumentPart()
            {
                var element =
                    new Document(
                        new Body(
                            new SectionProperties(
                                new HeaderReference()
                                {
                                    Type = HeaderFooterValues.First,
                                    Id = "rId2"
                                },
                                new HeaderReference()
                                {
                                    Type = HeaderFooterValues.Even,
                                    Id = "rId4"
                                },
                                new HeaderReference()
                                {
                                    Type = HeaderFooterValues.Default,
                                    Id = "rId6"
                                },
                                new PageMargin()
                                {
                                    Top = 1440,
                                    Right = (UInt32Value)1440UL,
                                    Bottom = 1440,
                                    Left = (UInt32Value)1440UL,
                                    Header = (UInt32Value)720UL,
                                    Footer = (UInt32Value)720UL,
                                    Gutter = (UInt32Value)0UL
                                },
                                new TitlePage()
                            )));
                return element;
            }
            private static Header GeneratePageHeaderPart(string HeaderText)
            {
                var element =
                    new Header(
                        new Paragraph(
                            new ParagraphProperties(
                                new ParagraphStyleId() { Val = "Header" }),
                            new Run(
                                new Text(HeaderText))
                        ));
                return element;
            }
            private static Settings GenerateDocumentSettingsPart()
            {
                var element =
                    new Settings(new EvenAndOddHeaders());
                return element;
            }
        }
    }

    Any help is appreciated.

    Thank you,

    Mitch

    Monday, March 24, 2014 8:01 PM

Answers

  • Hi Mitch,

    According to your description, you want to set the landscape orientation for the specific text in Word.
    I suggest you doing it manually and open the document using Open XML SDK productivity tools. You can get the difference from figure below:
     
    It insert a new paragraph with the section and set the orientation of this section to default(Portrait). Then we can modify the code like below:

    public void TestDoc2()
            {
                    string documentPath = @"C:\Users\me\Desktop\PageOrientationTest.docx";
                    using (WordprocessingDocument package = WordprocessingDocument.Create(documentPath, WordprocessingDocumentType.Document))
                    {
                        AddParts(package);
                        MainDocumentPart mainPart = package.MainDocumentPart;
                        Body body = package.MainDocumentPart.Document.Body;
                        //body.Append(PageOrientationPortrait());
                        Paragraph paraDocType = body.AppendChild(new Paragraph());
                        //Create run thats hold text that will be added to the paragraph
                        Run runDocHeader = paraDocType.AppendChild(new Run());
                        RunProperties rpDocHdr = new RunProperties();
                        rpDocHdr.Append(new Bold());
                        FontSize fs = new FontSize();
                        fs.Val = new StringValue("40");
                        rpDocHdr.Append(fs);
                        runDocHeader.AppendChild<RunProperties>(rpDocHdr);
                        //Add text to the run
                        runDocHeader.AppendChild(new Text("Health and Safety Plan"));
                        //Title Area
                        Paragraph paraHaspTitle = body.AppendChild(new Paragraph());
                        Run runTitle = paraHaspTitle.AppendChild(new Run());
                        RunProperties runproperties = runTitle.AppendChild(new RunProperties(new Bold()));
                        runTitle.AppendChild(new CarriageReturn());//Drop down a line
                        runTitle.AppendChild(new Bold());//Add bold to text
                        runTitle.AppendChild(new Text("Title: ") { Space = SpaceProcessingModeValues.Preserve });
                        //Title text
                        Run runTitleActual = paraHaspTitle.AppendChild(new Run());
                        runTitleActual.AppendChild(new Text("Will hold string variable for title here."));
                        //Insert Page Break here
                        Paragraph pageBreakParagraph1 = new Paragraph(new Run(new Break() { Type = BreakValues.Page }));
                        body.Append(pageBreakParagraph1);
                        
                        //
                        Paragraph pageBreakParagraph2 = new Paragraph();
    
                        ParagraphProperties paragraphProperties1 = new ParagraphProperties(PageOrientationPortrait());
                        pageBreakParagraph2.Append(paragraphProperties1);
                        body.Append(pageBreakParagraph2);
    
                        //Add Paragraph
                        Paragraph paraTest = CreatePara();
                        body.Append(paraTest);
                        //Change to Landscape Orientation here
                        body.Append(PageOrientationLandScape());
                      
                    }
            }
    

    Then the code works well for me.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by mjbz Tuesday, March 25, 2014 6:15 PM
    Tuesday, March 25, 2014 11:16 AM
    Moderator

All replies

  • Hi Mitch,

    According to your description, you want to set the landscape orientation for the specific text in Word.
    I suggest you doing it manually and open the document using Open XML SDK productivity tools. You can get the difference from figure below:
     
    It insert a new paragraph with the section and set the orientation of this section to default(Portrait). Then we can modify the code like below:

    public void TestDoc2()
            {
                    string documentPath = @"C:\Users\me\Desktop\PageOrientationTest.docx";
                    using (WordprocessingDocument package = WordprocessingDocument.Create(documentPath, WordprocessingDocumentType.Document))
                    {
                        AddParts(package);
                        MainDocumentPart mainPart = package.MainDocumentPart;
                        Body body = package.MainDocumentPart.Document.Body;
                        //body.Append(PageOrientationPortrait());
                        Paragraph paraDocType = body.AppendChild(new Paragraph());
                        //Create run thats hold text that will be added to the paragraph
                        Run runDocHeader = paraDocType.AppendChild(new Run());
                        RunProperties rpDocHdr = new RunProperties();
                        rpDocHdr.Append(new Bold());
                        FontSize fs = new FontSize();
                        fs.Val = new StringValue("40");
                        rpDocHdr.Append(fs);
                        runDocHeader.AppendChild<RunProperties>(rpDocHdr);
                        //Add text to the run
                        runDocHeader.AppendChild(new Text("Health and Safety Plan"));
                        //Title Area
                        Paragraph paraHaspTitle = body.AppendChild(new Paragraph());
                        Run runTitle = paraHaspTitle.AppendChild(new Run());
                        RunProperties runproperties = runTitle.AppendChild(new RunProperties(new Bold()));
                        runTitle.AppendChild(new CarriageReturn());//Drop down a line
                        runTitle.AppendChild(new Bold());//Add bold to text
                        runTitle.AppendChild(new Text("Title: ") { Space = SpaceProcessingModeValues.Preserve });
                        //Title text
                        Run runTitleActual = paraHaspTitle.AppendChild(new Run());
                        runTitleActual.AppendChild(new Text("Will hold string variable for title here."));
                        //Insert Page Break here
                        Paragraph pageBreakParagraph1 = new Paragraph(new Run(new Break() { Type = BreakValues.Page }));
                        body.Append(pageBreakParagraph1);
                        
                        //
                        Paragraph pageBreakParagraph2 = new Paragraph();
    
                        ParagraphProperties paragraphProperties1 = new ParagraphProperties(PageOrientationPortrait());
                        pageBreakParagraph2.Append(paragraphProperties1);
                        body.Append(pageBreakParagraph2);
    
                        //Add Paragraph
                        Paragraph paraTest = CreatePara();
                        body.Append(paraTest);
                        //Change to Landscape Orientation here
                        body.Append(PageOrientationLandScape());
                      
                    }
            }
    

    Then the code works well for me.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by mjbz Tuesday, March 25, 2014 6:15 PM
    Tuesday, March 25, 2014 11:16 AM
    Moderator
  • I was able to figure it out.  I'm new to the OpenXML Word document and your suggestion helped a lot.  Using the Open XML SDK productivity tools was awkard at first but it made sense after a short time.  First I created what I desired using Word and then I opened in the productivity tool to see how the code appeared.  As well, after getting it wrong a couple of times I went to google and found some additional information on page breaks and section breaks.

    Thank you,

    Mitch

    Tuesday, March 25, 2014 6:15 PM