0
votes

I want to change a page orientation of word document using openXML. I am replacing a new paragraph and adding a table. After that i need to chnage the orientation of that page to Landscape

This is the code I am using

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(mem, true, openSettings))
                {
                    HtmlConverter converter = new HtmlConverter(wordDoc.MainDocumentPart);
                    var newParagraphs = converter.Parse(GetTable1HTML());

                    var body = wordDoc.MainDocumentPart.Document.Body;

                    //styling
                    var para1 = new Wordprocessing.Paragraph(new Wordprocessing.Run(newParagraphs)) { RsidParagraphAddition = "00BA2F0F", RsidParagraphProperties = "00BA2F0F", RsidRunAdditionDefault = "00BA2F0F" };
                    Wordprocessing.ParagraphProperties paragraphProperties1 = new Wordprocessing.ParagraphProperties();
                    Wordprocessing.SectionProperties sectionProp1 = new Wordprocessing.SectionProperties(new Wordprocessing.PageSize()
                    { Width = (UInt32Value)35840U, Height = (UInt32Value)22240U, Orient = Wordprocessing.PageOrientationValues.Landscape })
                    { RsidR = "00BA2F0F" };
                    paragraphProperties1.Append(sectionProp1);

                    //Table1
                    var paragraphsTable1 = body.Descendants<Wordprocessing.Paragraph>().
                        Where(e1 => e1.InnerText.Contains("<text to search>")).FirstOrDefault();
                    if (paragraphsTable1 == null)
                    {
                        return null;
                    }

                    //para1.Append(paragraphProperties1);
                    paragraphsTable1.InsertBeforeSelf<Wordprocessing.Paragraph>(para1).Append(paragraphProperties1);
                    paragraphsTable1.Remove();}
1

1 Answers

0
votes

You have to add SectionProperties to Body (or get if there any), not ParagraphProperties. Also don't know why do you set Rsid properties here. Your code could will looks like

// styling
Wordprocessing.SectionProperties sectionProp1 = 
               body.Descendants<SectionProperties>()?.FirstOrDefault() ?? 
               body.AppendChild(new SectionProperties());

var pageSize = sectionProp1.Descendants<PageSize>()?.FirstOrDefault() ?? 
               sectionProp1.AppendChild(new Wordprocessing.PageSize());
pageSize.Width = 35840;
pageSize.Height = 22240;
pageSize.Orient = Wordprocessing.PageOrientationValues.Landscape;

If you will want to change properties again, you have to use something like

body.AppendChild(
     new Paragraph(
             new Run(
             new Break() { Type = BreakValues.Page })));
body.AppendChild(new SectionProperties());

Also youd try add SectionType in your example into SectionProperties, not sure, but may be that will works aswell.

Wordprocessing.SectionProperties sectionProp1 = new Wordprocessing.SectionProperties(
    new SectionType() { Val = SectionMarkValues.NextPage },
    new Wordprocessing.PageSize()
                    { Width = (UInt32Value)35840U, Height = (UInt32Value)22240U, Orient = Wordprocessing.PageOrientationValues.Landscape })
                    { RsidR = "00BA2F0F" };