Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get MS Word total pages count using Open XML SDK?

I am using below code to get the page count but it is not giving actual page count(PFA). What is the better way to get the total pages count?

var pageCount = doc.ExtendedFilePropertiesPart.Properties.Pages.Text.Trim();

Wrong page count

Actual pages are 10

Note: We cannot use the Office Primary Interop Assemblies in my Azure web app service

Thanks in advance.

like image 461
Ganapathi D Avatar asked Nov 27 '18 05:11

Ganapathi D


People also ask

What is Open XML SDK 2.5 for Microsoft Office?

The Open XML SDK 2.5 simplifies the task of manipulating Open XML packages and the underlying Open XML schema elements within a package. The Open XML SDK 2.5 encapsulates many common tasks that developers perform on Open XML packages, so that you can perform complex operations with just a few lines of code.

What is Documentformat Openxml XML?

The Open XML SDK provides tools for working with Office Word, Excel, and PowerPoint documents. It supports scenarios such as: - High-performance generation of word-processing documents, spreadsheets, and presentations. - Populating content in Word files from an XML data source.

How do I open an XML document in word-processing?

#1) Open Windows Explorer and browse to the location where the XML file is located. We have browsed to the location of our XML file MySampleXML as seen below. #2) Now right-click over the file and select Open With to choose Notepad or Microsoft Office Word from the list of options available to open the XML file.


2 Answers

In theory, the following property can return that information from the Word Open XML file, using the Open XML SDK:

int pageCount = (int) document.ExtendedFilePropertiesPart.Properties.Pages.Text;

In practice, however, this isn't reliable. It might work, but then again, it might not - it all depends on 1) What Word managed to save in the file before it was closed and 2) what kind of editing may have been done on the closed file.

The only sure way to get a page number or a page count is to open a document in the Word application interface. Page count and number of pages is calculated dynamically, during editing, by Word. When a document is closed, this information is static and not necessarily what it will be when the document is open or printed.

See also https://github.com/OfficeDev/Open-XML-SDK/issues/22 for confirmation.

like image 153
Cindy Meister Avatar answered Nov 15 '22 09:11

Cindy Meister


This code worked for me. It adds "Page X of Y" to the document.

para = new Paragraph(new Run(
       new Text() { Text = "Page ", Space = SpaceProcessingModeValues.Preserve },
       new SimpleField() { Instruction = "PAGE" }, 
       new Text() { Text = " of ", Space = SpaceProcessingModeValues.Preserve },  
       new SimpleField() { Instruction = "NUMPAGES \\*MERGEFORMAT" }));
like image 34
Sohaib Afzal Avatar answered Nov 15 '22 10:11

Sohaib Afzal