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

标签 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);


- 向主文档添加一个新的 Body 并将段落附加到它:

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);


- 向主文档添加一个新的 Body 并直接将 SectionProperties 附加到它:

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);

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

最佳答案

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

下一页分节符开始横向:

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 }))));

下一页分节符开始纵向:

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 }))));

关于c# - 如何使用 Open XML 和 C# 更改单个段落或页面的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15655375/

相关文章:

sql-server - Reporting Services 2008 : Is there a way to change item properties, 取决于报告的呈现方式?

C# 将 Excel 图表作为对象(而不是图片)导出到 Word

c# - 如何在新线程中强制执行

c# - 如何使用与角色连接的附加表来扩展 ASP.NET MVC Identity?

.net - 如何使用 OpenXML 修改 word 超链接

c# - 如何使用 DocumentFormat.OpenXml 在 Word 模板中编辑书签并将其另存为新的 PDF 文件?

c# - 如何将日期插入 Open XML 工作表?

c# - 生成word文档并用html设置方向景观

c# - Oauth2中如何同时撤销RefreshToken和使AccessToken失效

c# - C# 中带有列表的默认字典