如何以编程方式 (C#) 确定 .docx 文件的页数 [英] How to programatically (C#) determine the pages count of .docx files

查看:25
本文介绍了如何以编程方式 (C#) 确定 .docx 文件的页数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

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

>

使用系统;使用 Microsoft.Office.Interop.Word;命名空间 WordDocStats{课程计划{//基于:http://www.dotnetperls.com/word静态无效主(字符串 [] args){//打开一个文档文件.var application = new Application();var document = application.Documents.Open(@"C:UsersMyNameDocumentsword.docx");//获取页数.var numberOfPages = document.ComputeStatistics(WdStatistic.wdStatisticPages, false);//打印结果.Console.WriteLine(String.Format("文档总页数:{0}", numberOfPages));//关闭单词.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/字

-- 更新

有关使您可以访问页数的方法 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

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 documents , and then returns the #pages of each .docx file.

解决方案

To illustrate how this can be done, I have just created a C# console application based on .NET 4.5 and some of the 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:UsersMyNameDocumentsword.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();
        }
    }
}

For this to work 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)

The two COM objects gives you access to the namespaces needed.

For details on how to reference the correct assemblies, please refer to section: "3. Setting Up Work 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

Documentation about the method Document.ComputeStatistics that gives you access to the page count can be found here: http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.computestatistics.aspx

As seen in the documentation, the method takes a WdStatistic enum that enables you to retrieve different kinds of stats, e.g., the total amount of pages. For an overview of the complete range of stats you have access to, please refer to the documentation of the WdStatistic enum, which can be found here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdstatistic.aspx

这篇关于如何以编程方式 (C#) 确定 .docx 文件的页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆