使用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 天的试用许可证。

相关推荐
微软Nav/BC专家17 小时前
Microsoft Dynamics 365 Business Central Word Add-in如何安装
word·微软erp
大C聊AI17 小时前
标书高效制作:Word 排版快捷键 + AI 工具组合工作流
word·办公技巧·效率提升·招投标·智标领航
jianwuhuang8218 小时前
豆包输出word
人工智能·ai·chatgpt·word·deepseek·ai导出鸭
chatexcel2 天前
ChatExcel AI文档上线:AI自动生成Word报告的完整工作流
人工智能·word
俊哥工具2 天前
不用安装不收费!多功能U盘修复工具,解决大部分U盘故障
学习·pdf·word·excel·音视频
草丛中的蝈蝈3 天前
word目录中的一级标题编号和标题之间距离很大,但是内容里是正常的
word
Metaphor6923 天前
使用 Python 设置 Word 文档文本的颜色
python·word
usdoc文档预览3 天前
国产化踩坑:Vue3 / React / 小程序如何免插件实现 OFD 及复杂 Office 文档同屏预览
前端·javascript·react.js·小程序·pdf·word·office文件在线预览
一头爱吃肉的牛3 天前
Word转PPT教程:三步用AI工具一键生成
人工智能·word·powerpoint
熟悉的新风景4 天前
word,wps使用技巧
word·wps