如何使用Open XML和C#更改单个段落或页面的方向?

Moh*_*ief 5 c# openxml export-to-word

我正在使用C#和Open XML SDK生成word文档.我想将一些段落及其包含页面的方向更改为横向,同时保持其他段落的纵向方向.
我尝试了一些解决方案,但他们没有实现我想要的东西,而是所有页面的方向都被改为横向除了第一个.这些解决方案包括:
- 将SectionProperties添加到我想要将其方向更改为横向的段落中:

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);
Run Code Online (Sandbox Code Playgroud)


- 将新主体添加到主文档并将段落附加到主文档:

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);
Run Code Online (Sandbox Code Playgroud)


- 在主文档中添加一个新Body并直接向其添加SectionProprties:

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);
Run Code Online (Sandbox Code Playgroud)

那么,有没有办法改变单个段落/页面的方向?

jn1*_*1kk 7

您必须放置下一页分节符以开始新节,并设置该节的方向.完成后,使用默认页面方向开始新部分.

下一页分节开始横向:

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 }))));
Run Code Online (Sandbox Code Playgroud)

下一页分节开始纵向方向:

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 }))));
Run Code Online (Sandbox Code Playgroud)