Getting page margins of a Word document using OpenXML

Viewed 8

Tried different version of word documents and different page margins in them, but nothing I found on the web did the job. Here's my attempt at reading page margins from a .docx file.

var document = WordprocessingDocument.Open(sourceDocxPath, true);
var myMargins = document.MainDocumentPart.Document.GetFirstChild<PageMargin>();
// always null
1 Answers

In the truest programmer fashion, I figured out the answer 5 minutes after posting

var leftMargin = document.MainDocumentPart.Document.Body
    .GetFirstChild<SectionProperties>()
    .GetFirstChild<PageMargin>().Left;

I figured it out by inspecting the outerXml of the document and seeing where things are positioned.

Related