【问题标题】:How to programatically (C#) determine the pages count of .docx files如何以编程方式(C#)确定 .docx 文件的页数
【发布时间】:2012-09-09 13:39:30
【问题描述】:

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

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

【问题讨论】:

    标签: c# ms-word


    【解决方案1】:

    为了说明如何做到这一点,我刚刚创建了一个基于 .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

    【讨论】:

      【解决方案2】:

      使用 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

      【讨论】:

      • 我编写了以下代码在 Windows 应用程序中执行任务,但出现错误:
      • @AyaZoghby 你能告诉我错误详情吗?因为在我的电脑中它工作正常。
      • 它适用于 .doc 文件??还是仅使用 .docx(基于 openxml)?
      • @AndersonRissardi 我相信它只适用于 docx。
      • 如果您不想在服务器上安装 MS Office (DLL),这是一个不错的解决方案
      【解决方案3】:

      现代解决方案(基于Jignesh Thakker's answer):不再存在Open XML SDK,而是published on Github,甚至支持.NET Core。服务器/运行机器上不需要 MS Office。

      安装Nuget package:

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

      【讨论】:

        【解决方案4】:

        您可以免费使用 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);
        
                    }
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-08-20
          • 2010-09-16
          • 1970-01-01
          • 2014-01-16
          • 2011-02-01
          • 1970-01-01
          • 2010-09-07
          • 1970-01-01
          相关资源
          最近更新 更多