OpenXML - Docx document left indent/margin

Posted on 2011-04-28
Last Modified: 2012-05-11

I use OpenXML to generate a word document in SharePoint.

I need to set indentation of my word document, mainly the left and right margins are too wide so I want to shrink it down, but I can't seem to find the function for it. I could only set the left and right margin of a TABLE, but not the whole word document. It's so simple in Word because you can choose margins of Normal, Narrow, Wide, etc.
Attached is a sample of margins for a TABLE.
Is there one for the document?
TableCellMarginDefault margin = new TableCellMarginDefault(); 
margin.TopMargin = new TopMargin { Width = 0, Type = TableWidthUnitValues.Dxa };
margin.BottomMargin = new BottomMargin { Width = 0, Type = TableWidthUnitValues.Dxa };
margin.TableCellLeftMargin = new TableCellLeftMargin { Width = 50, Type = TableWidthValues.Dxa };
margin.TableCellRightMargin = new TableCellRightMargin { Width = 50, Type = TableWidthValues.Dxa };

Open in new window

Question by:SOS_Helpdesk
    LVL 17

    Accepted Solution

    In the Word object model, you would set the margins for the whole document using the pagesetup object - this has the topmargin, bottommargin etc properties.

    I think in OpenXML it's the PageMargin object you need - this snippet generated by the OpenXML SDK illustrates creating one and appending it to the Section:
               SectionProperties sectionProperties1 = new SectionProperties() { RsidR = "00FC093D", RsidSect = "00240B4C" };
                PageSize pageSize1 = new PageSize() { Width = (UInt32Value)16839U, Height = (UInt32Value)11907U, Orient = PageOrientationValues.Landscape, Code = (UInt16Value)9U };
                PageMargin pageMargin1 = new PageMargin() { Top = 406, Right = (UInt32Value)567U, Bottom = 426, Left = (UInt32Value)426U, Header = (UInt32Value)285U, Footer = (UInt32Value)708U, Gutter = (UInt32Value)0U };
                Columns columns1 = new Columns() { Space = "708" };
                DocGrid docGrid1 = new DocGrid() { LinePitch = 360 };

    Open in new window


    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
    This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
    This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now