Search code examples
openxmlopenxml-sdktableofcontentspage-numbering

how to get page numbers based on openxmlelement


For a Paragraph object, how can I determine on which page this is located using the Open XML SDK 2.5 ?

I've obtained all child elements in my document and fetched innertext also, using this.

   foreach (var i in mainPart.Document.ChildElements.FirstOrDefault().ChildElements)
        {
            ParagraphElements.Add(i); //openxmlelement list
        }

I want to get page number for corresponding paragraph. for example, I have "this is heading 1" marked as style Heading 1 and this will be updated in TOC. so there I need to pass page number

Thanks in advance


Solution

  • After a lot of ground work, found that, page number cannot be retrieved using openxml element. We can approximate it. But we cannot be sure. Because Page numbers are rendered by word processor layout engine. This happens after all the OpenXML elements are passed to word processor. We can calculate it with LastRenderedPageBreak. But we cannot be sure that location of the element is correct.

    So, I would suggest to go with UpdateFieldsOnOpen or Macro for an easier solution.