繁体   English   中英

如何以编程方式(C#)确定.docx文件的页数

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

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

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

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

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

现代解决方案(基于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);
    }
}

你可以使用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);

            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM