How to programmatically (C #) determine the number of pages of .docx files

I have about 400 files in .docx format, and I need to determine the length of each in #pages.

So, I want to write C # code to select the folder containing the documents, and then return the #pages of each .docx file.

+5
source share
4 answers

To illustrate how this can be done, I just created a C # console application based on .NET 4.5 and some Microsoft Office 2013 COM objects.

using System; using Microsoft.Office.Interop.Word; namespace WordDocStats { class Program { // Based on: http://www.dotnetperls.com/word static void Main(string[] args) { // Open a doc file. var application = new Application(); var document = application.Documents.Open(@"C:\Users\MyName\Documents\word.docx"); // Get the page count. var numberOfPages = document.ComputeStatistics(WdStatistic.wdStatisticPages, false); // Print out the result. Console.WriteLine(String.Format("Total number of pages in document: {0}", numberOfPages)); // Close word. application.Quit(); } } } 

To do this, you need to reference the following COM objects:

  • Microsoft Office Object Library (version 15.0 in my case)
  • Microsoft Word Object Library (version 15.0 in my case)

Two COM objects give you access to the necessary namespaces.

For more information on how to reference the correct assemblies, see the section "3. Setting up the working environment:" at: http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx

For a quick and more general introduction to Word automation through C # see http://www.dotnetperls.com/word

- UPDATE

The documentation for the Document.ComputeStatistics method, which gives you access to the page account, can be found here: http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.computestatistics.aspx

As you can see from the documentation, the method accepts the WdStatistic enumeration, which allows you to get various types of statistics, for example, the total number of pages. For an overview of the full feature set that you have access to, see the WdStatistic enumeration WdStatistic , which can be found here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdstatistic. aspx

+17
source

use DocumentFormat.OpenXml.dll, you can find the dll in C: \ Program Files \ Open XML SDK \ V2.0 \ lib

Code example:

 DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(docxPath, false); MessageBox.Show(doc.ExtendedFilePropertiesPart.Properties.Pages.InnerText.ToString()); 

To use the DocumentFormat.OpenXml.Packaging.WordprocessingDocument class, you need to add the following links to your project

DocumentFormat.OpenXml.dll & WindowsBase.dll

+3
source

Modern solution (based on Jignesh Thakker answer ): The Open XML SDK no longer exists, but it is published on Github and even supports .NET Core. You do not need MS Office on the server / running machine.

Install the Nuget package :

 Install-Package DocumentFormat.OpenXml 

The code:

 using DocumentFormat.OpenXml.Packaging; private int CountWordPage(string filePath) { using (var wordDocument = WordprocessingDocument.Open(filePath, false)) { return int.Parse(wordDocument.ExtendedFilePropertiesPart.Properties.Pages.Text); } } 
0
source

You can use the number of pages Spire.Doc for free :)

 using Spire.Doc; public sealed class TestNcWorker { [TestMethod] public void DocTemplate3851PageCount() { var docTemplate3851 = Resource.DocTemplate3851; using (var ms = new MemoryStream()) { ms.Write(docTemplate3851, 0, docTemplate3851.Length); Document document = new Document(); document.LoadFromStream(ms, FileFormat.Docx); Assert.AreEqual(2,document.PageCount); } var barCoder = new BarcodeAttacher("8429053", "1319123", "HR3514"); var barcoded = barCoder.AttachBarcode(docTemplate3851).Value; using (var ms = new MemoryStream()) { ms.Write(barcoded, 0, barcoded.Length); Document document = new Document(); document.LoadFromStream(ms, FileFormat.Docx); Assert.AreEqual( 3, document.PageCount); } } } 
0
source

Source: https://habr.com/ru/post/1215141/


All Articles