OpenXML - Docx document left indent/margin


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

Who is Participating?
andrewssd3Connect With a Mentor Commented:
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

SOS_HelpdeskAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.