How to programatically (C#) determine the pages count of .docx files

后端 未结 4 1069
刺人心
刺人心 2020-12-11 17:22

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 for selecting the folder that contains the

4条回答
  •  醉话见心
    2020-12-11 18:04

    Modern solution (based on Jignesh Thakker's answer): Open XML SDK is no longer there, but it is published on Github and even support .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);
        }
    }
    

提交回复
热议问题