7

我有大约 400 个 .docx 格式的文件,我需要确定 #pages 中每个文件的长度。

所以,我想编写 C# 代码来选择包含文档的文件夹,然后返回每个 .docx 文件的#pages。

4

4 回答 4

20

为了说明如何做到这一点,我刚刚创建了一个基于 .NET 4.5 和一些 Microsoft Office 2013 COM 对象的 C# 控制台应用程序。

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();
        }
    }
}

为此,您需要引用以下 COM 对象:

  • Microsoft Office 对象库(在我的例子中是 15.0 版)
  • Microsoft Word 对象库(在我的例子中是 15.0 版)

这两个 COM 对象使您可以访问所需的命名空间。

有关如何引用正确程序集的详细信息,请参阅“3. 设置工作环境:”部分:http ://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx

有关通过 C# 进行 Word 自动化的快速和更全面的介绍,请参阅: http: //www.dotnetperls.com/word

- 更新

Document.ComputeStatistics可以在此处找到有关可让您访问页数的方法的文档:http: //msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.computestatistics.aspx

如文档中所见,该方法采用一个WdStatistic枚举,使您能够检索不同类型的统计信息,例如页面总数。有关您可以访问的完整统计信息范围的概述,请参阅WdStatistic枚举的文档,可在此处找到:http: //msdn.microsoft.com/en-us/library/microsoft.office.interop .word.wdstatistic.aspx

于 2012-09-09T13:59:39.700 回答
4

使用 DocumentFormat.OpenXml.dll 你可以在 C:\Program Files\Open XML SDK\V2.0\lib 中找到 dll

示例代码:

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

要使用 DocumentFormat.OpenXml.Packaging.WordprocessingDocument 类,您需要在项目中添加以下引用

DocumentFormat.OpenXml.dll & Windowsbase.dll

于 2012-09-09T14:07:47.713 回答
0

您可以使用 Spire.Doc 页数是免费的 :)

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);

            }
        }
    }
于 2019-06-25T17:16:32.757 回答
0

现代解决方案(基于Jignesh Thakker 的回答):Open XML SDK 不再存在,但它已发布在 Github 上,甚至支持 .NET Core。服务器/运行机器上不需要 MS Office。

安装Nuget 包

Install-Package DocumentFormat.OpenXml

编码:

using DocumentFormat.OpenXml.Packaging;

private int CountWordPage(string filePath)
{
    using (var wordDocument = WordprocessingDocument.Open(filePath, false))
    {
        return int.Parse(wordDocument.ExtendedFilePropertiesPart.Properties.Pages.Text);
    }
}
于 2018-12-17T20:34:54.547 回答