Search code examples
xmlms-worddocx

Why only some page numbers stored in XML of docx file?


I have added some page number in footer. It is visible in document properly. But if i unzip docx and check the footer.xml only some random page numbers are there. so how MS-Word is able to display page numbers properly, And where it store all page numbers?


Solution

  • There's no logic as to which page numbers are stored in a Word Open XML document. Basically, the page numbers you see in the Word Open XML header/footer section are meaningless as they'll be regenerated automatically when the document is opened and may well not reflect the actual paging.

    All dynamic content in Word, such as page numbering, is managed by fields. A field basically has two parts: the field code and the field result. You can see this structure in the Word Open XML.

    Depending on the field type and its location, fields will be updated automatically OR will require explicit updating. PAGE fields in the header/footer area update automatically, pretty much during the entire editing experience in the Word UI.

    For fields that need to be updated explicitly, Word will usually store the last result in the document. But fields that Word updates automatically may or may not store the last result. PLUS there's no guarantee that the stored result will be correct when the document is opened the next time, or if it's edited. Depending on the individual machine, page layout can change fluidly when a document is opened. So any page numbers you see in the Open XML are no guarantee to be in the same place when a document is opened.

    The total number of pages, however, that's stored in the document properties could be correct - also depending on how the page layout is done when the document is next opened.

    The following article contains some relevant information on field updating: https://support.microsoft.com/en-us/help/211629/which-fields-are-updated-when-you-open--repaginate--or-print-document