How to renumber pages in a WordProcessingDocument?

Viewed 75

I've created a large (~ 1000 pages) word document using the openxml-sdk. When i open it the first time using the word application, it shows "Word is renumbering the pages of test.docx" in the statusbar and does so for about 15 seconds. I've made a german screenshot of this behaviour. After this step the document is changed and need to be saved. The new version of the file is about two times in size of the original one.

The document is saved the first time by simply calling

document.SaveAs("someFilePath");

What exactly is this behaviour? How can i renumber the pages in (or after) the creation of the document programmatically?

1 Answers

You can't renumber pages in a WordprocessingDocument, because those page numbers are not stored in the WordprocessingDocument but rather created while laying out, or rendering, the document.

A typical document defines page numbers as a complex field that you would find in the Open XML markup contained in the FooterPart (or possibly HeaderPart). Assuming the page number field is stored in the FooterPart, you might have a single FooterPart in your WordprocessingDocument in the simplest case (e.g., document with a single section). Even if you have multiple FooterParts, e.g., because you have multiple sections or you have a different layout for the first, odd, and even pages, you have relatively few FooterParts in your document (at least compared to your 1.000 pages).

When Word renders the document for printing or viewing, it also renders the page numbers based on your FooterParts (still using my example). For 1.000 pages, that takes time since Word is simply not built for documents that large.

Should you want to do Word's job and perform the layout yourself, you need to understand that building a layout engine is extremely complex and requires a lot of effort.

Related