我正在使用 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/