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

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

问题描述

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



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


解决方案

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

 使用系统; 
使用的Microsoft.Office.Interop.Word;

命名空间WordDocStats
{
类节目
{
//基于:http://www.dotnetperls.com/word
静态无效的主要(字串[] args)
{
//打开一个doc文件。
VAR应用=新的应用程序();
变种文件= application.Documents.Open(@C:\Users\MyName\Documents\word.docx);

//获取页数。
VAR numberOfPages = document.ComputeStatistics(WdStatistic.wdStatisticPages,FALSE);

//打印出结果。
Console.WriteLine(的String.Format(在文档总页数:{0},numberOfPages));

//关闭字。
application.Quit();
}
}
}



对于这个工作,你需要参考下面的COM对象:




  • 微软Office对象库(15.0版在我的情况)

  • Microsoft Word对象库(在我的情况下,15.0版)



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



有关如何引用正确的组件的详细信息,请参见章节:3.设置工作环境:在:的http://www.c-sharpcorner.com/Uplo​​adFile/amrish_deep/WordAut​​omation05102007223934PM/WordAut​​omation.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


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

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天全站免登陆