使用C#代码统计Word 文档的单词、字符、段落、行数和页数

对单词、字符、段落、行数和页数进行准确统计,对于实现精确的文档分析至关重要。通过细致地追踪这些指标,作者可以更深入地了解文档的长度、结构和整体构成。在本文中,我们将介绍如何使用 Spire.Doc for .NET 在 C# 中统计 Word 文档中的单词、字符、段落、行数和页数。

安装 Spire.Doc for .NET

首先,您需要将 Spire.Doc for .NET 包中包含的 DLL 文件添加为 .NET 项目的引用。您可以通过下载链接获取这些 DLL 文件,或通过 NuGet 进行安装。

cs 复制代码
PM> Install-Package Spire.Doc

在 C# 中统计 Word 文档中的单词、字符、段落、行数和页数

Spire.Doc for .NET 提供了 BuiltinDocumentProperties 类,允许您从 Word 文档中获取关键信息。通过使用该类,您可以访问丰富的文档属性,包括内置和自定义属性,以及文档中单词、字符、段落、行数和页数的精确统计数据。

示例代码如下:

cs 复制代码
using Spire.Doc;
using System.IO;
using System.Text;

namespace CountWordsCharactersEtcInWord
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //初始化 Document 类的对象
            Document document = new Document();
            //加载示例 Word 文档
            document.LoadFromFile("Input.docx");

            //获取 BuiltinDocumentProperties 对象
            BuiltinDocumentProperties properties = document.BuiltinDocumentProperties;

            //获取文档中的单词、字符、段落、行数和页数
            int wordCount = properties.WordCount;
            int charCount = properties.CharCount;
            int paraCount = properties.ParagraphCount;
            int lineCount = properties.LinesCount;
            int pageCount = properties.PageCount;

            //初始化 StringBuilder 对象
            StringBuilder sb = new StringBuilder();
            //将结果添加到 StringBuilder
            sb.AppendLine("The number of words: " + wordCount);
            sb.AppendLine("The number of characters: " + charCount);
            sb.AppendLine("The number of paragraphs: " + paraCount);
            sb.AppendLine("The number of lines: " + lineCount);
            sb.AppendLine("The number of pages: " + pageCount);

            //将 StringBuilder 的内容写入文本文件
            File.WriteAllText("result.txt", sb.ToString());
            document.Close();
        }
    }
}

在 C# 中统计 Word 文档中某个特定段落的单词和字符数量

除了统计整个 Word 文档中的单词和字符数量外,Spire.Doc for .NET 还允许您通过 Paragraph.WordCountParagraph.CharCount 属性来统计某个特定段落的单词数和字符数。

示例代码如下:

cs 复制代码
using Spire.Doc;
using Spire.Doc.Documents;
using System.IO;
using System.Text;

namespace CountWordsAndCharactersForParagraph
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //初始化 Document 类的对象
            Document document = new Document();
            //加载一个示例 Word 文档
            document.LoadFromFile("Input.docx");

            //获取特定段落
            Paragraph paragraph = document.Sections[0].Paragraphs[0];

            //获取段落中的单词数和字符数
            int wordCount = paragraph.WordCount;
            int charCount = paragraph.CharCount;
           

            //初始化 StringBuilder 类的对象
            StringBuilder sb = new StringBuilder();
            //将结果追加到 StringBuilder
            sb.AppendLine("The number of words: " + wordCount);
            sb.AppendLine("The number of characters: " + charCount);

            //将 StringBuilder 的内容写入文本文件
            File.WriteAllText("result.txt", sb.ToString());
            document.Close();
        }
    }
}

申请临时许可证

如果您想要去除生成文档中的评估提示,或解除功能限制,请为自己申请一个 30 天的试用许可证。

相关推荐
骆驼爱记录2 天前
Python打包命令全攻略
自动化·word·excel·新人首发
Charlie_lll2 天前
LibreOffice 实现 Word 转 PDF
java·spring boot·pdf·word
我的Doraemon2 天前
word 批量处理公式方案
word
不坑老师2 天前
小工具显出大才能——不坑盒子为教育数字化转型贡献“新方案”
microsoft·word·excel·ppt·office
骆驼爱记录2 天前
Python程序打包全攻略
自动化·word·excel·wps·新人首发
骆驼爱记录3 天前
Excel绿色箭头秒消指南
开发语言·自动化·word·新人首发
wtsolutions3 天前
WPS数据写入Word模版文档,批量生成文档
word·wps
response_L3 天前
PageOffice动态给word插入二维码(或条形码)图片
vue·word·开发工具·二维码·在线编辑
半空扫地僧一枚3 天前
解决word分栏问题
word
jiayong234 天前
Word表格高级应用指南
word