2012-11-27 53 views
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文檔的代碼片段,該文檔在縱向方向模式下生成,我希望頁面處於橫向模式。所以你可以請我建議我在哪裏使用上面的代碼,你給了。在橫向模式下生成word文檔

回答

3

您必須添加SectionProperties到Body。在部分屬性中,您需要PageSize並將其方向屬性設置爲橫向。

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