Number of pages

Oct 18, 2011 at 4:15 PM

Hi Cathal,

first of all, congrats on the great project!

I'm working with the latest version of the DocX library and I noticed that it doesn't update the document's property 'number of pages'.

On my project I load an .docx file (using the Load method), replace some text, append some text and then save the document into a new file (using the SaveAs method). Everything works just fine! But when I view this new file on windows explorer the number of pages shown is always 1, regardless of the real number of pages on the document.

This became a problem for me because at a certain point of my project I need to know how many pages the document really has (I do that by reading the docx's XML).

Have you ever noticed that behavior? Is it an know issue or have you any plans on "fixing" that?

Coordinator
Oct 19, 2011 at 9:42 AM
Hi luciocs,

the .docx format handles pages in an odd way. At least its odd when you first think about it. After awhile I began to realize that this is probably the best way to do it.

The key concept is that pages don't really exist. Pages are created for visualization purposes only (printing or onscreen display).
Pages do not actually exist in a document's markup.

The xml file you are talking about. The one which contains a page count. This is created by Word.exe every time that it displays a document.
Word figures out the height of each element.
For text this is done using font height.
For elements like Pictures and possibly Charts the height is specified by the element.
Elements like Paragraphs, Cells, Rows and Tables are more complicated because they can contain children elements.

Every element can also specify its own margins, padding's, offsets and other layout specific attributes.

This is why DocX does not manage page numbers. Supporting page numbers is as difficult as building a document viewer (something I have always wanted to do).

So to answer your question. DocX will not update the page count in the internal xml file.
An awful work around would be to get Word.exe to open and re-save a document after you have created/edited it using DocX.

Kind regards and happy coding,
Cathal
Oct 19, 2011 at 1:55 PM

Hi Cathal,

thanks for the enlightenment.

It really seams odd at a first look. But I get the concept.

I will try to find another way to optimize my documents without counting pages.

Once again, congrats on the project!