2
using (MemoryStream generatedDocument = new MemoryStream())
{
   using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
   {
      MainDocumentPart mainPart = package.MainDocumentPart;

      if (mainPart == null)
      {
         mainPart = package.AddMainDocumentPart();
         new DocumentFormat.OpenXml.Wordprocessing.Document(new DocumentFormat.OpenXml.Wordprocessing.Body()).Save(mainPart);
      }

      HtmlConverter converter = new HtmlConverter(mainPart);
      DocumentFormat.OpenXml.Wordprocessing.Body body = mainPart.Document.Body;

      var paragraphs = converter.Parse(docbody);

      for (int y = 0; y < paragraphs.Count; y++)
                        {
                            body.Append(paragraphs[y]);
                        }

      mainPart.Document.Save();
   }

这在用于生成我的 word 文档的代码片段中,文档以纵向模式生成,我希望页面处于横向模式。那么您能否建议我在哪里使用您提供的上述代码。

4

1 回答 1

4

您必须将 SectionProperties 添加到 Body。在部分属性中,您需要设置 PageSize 并将其方向属性设置为 Landscape。

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

body.Append(sectionProperties);
于 2012-11-28T12:24:49.160 回答