在横向模式下生成word文档

时间:2012-11-27 12:26:57

标签: c#-4.0 ms-word

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文档,文档以纵向方式生成,我希望页面处于横向模式。所以,请你建议我在哪里使用你给出的上述代码。

1 个答案:

答案 0 :(得分:3)

您必须将SectionProperties添加到Body。在剖面属性中,您需要使用PageSize并将其orientation属性设置为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);
相关问题