Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change page orientation from c# with open xml sdk

I'm creating a word document

using (WordprocessingDocument myDoc = WordprocessingDocument.Create(@"c:\generate\export.docx", WordprocessingDocumentType.Document))          
{
    MainDocumentPart mainPart = myDoc.AddMainDocumentPart(); 
    mainPart.Document = new Document();               
    var body = new Body();               
    var p = new Paragraph(
        new ParagraphProperties(
            new Justification()
            {
                Val = JustificationValues.Center
            }
        ),
        new Run(new Text("test"))
    ); 
    body.Append(p);                
    mainPart.Document.Append(body);                
    // Save changes to the main document part.                 
    mainPart.Document.Save();
}

How do I set page orientation to landscape?

like image 838
Alex Avatar asked Jun 11 '12 14:06

Alex


1 Answers

You have to make a new section properties.

WordprocessingDocument wd = someDoc;

wd.MainDocumentPart.Document.Body.Append(
    new Paragraph(
        new ParagraphProperties(
            new SectionProperties(
                new PageSize() { Width = (UInt32Value)15840U, Height = (UInt32Value)12240U, Orient = PageOrientationValues.Landscape },
                new PageMargin() { Top = 720, Right = Convert.ToUInt32(right * 1440.0), Bottom = 360, Left = Convert.ToUInt32(left * 1440.0), Header = (UInt32Value)450U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U }))));

Where left == left margin and right == right margin (doubles).

like image 60
jn1kk Avatar answered Sep 19 '22 22:09

jn1kk