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

相关推荐
STRUGGLE_xlf1 天前
AI大模型生成表格粘贴到 Word 后出现双线边框的原因与解决方案
word
weixin_416660071 天前
2026 年 AI 对话转 Word 工具分析:Pandoc、Typora、aitoword 怎么选
人工智能·word
F_D_Z2 天前
Word Embedding :从分布式假设到神经网络语言模型
分布式·word·embedding
asdzx672 天前
C#:通过模板快速生成 Word 文档
开发语言·c#·word
xinixini2 天前
2026年马年日历模板大全 可编辑Excel/Word/PSD/PDF素材合集
pdf·word·excel·日历
热爱生活的五柒3 天前
md2word工具推荐
word·md
骆驼爱记录3 天前
3步轻松去除WPS段落左侧符号
自动化·word·wps·新人首发
骆驼爱记录3 天前
表格居中无效的5大原因及解决方案
自动化·word·wps·新人首发
醉酒柴柴5 天前
word中没文字地方添加下划线方法
学习·word
热爱生活的五柒6 天前
论文中如何设置图、表格和标题不分页
word·图表