使用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 天前
WPS页码设置:第X页共Y-1页
自动化·word·excel·wps·新人首发
2301_816997882 天前
Word 清除格式的方法
word
微光feng3 天前
毕业论文word引用操作汇总
word·目录·公式·毕业论文·交叉引用·题注
2301_816997883 天前
Word 功能区与快速访问工具栏
word
halen3333 天前
Hellowordl: The Masters Tool for Word Puzzle Enthusiasts
word
lpfasd1233 天前
Markdown 导出 Word 文档技术方案
开发语言·c#·word
Cxiaomu3 天前
Python 文件解析: Excel / Word / PDF 的解析、处理、预览与下载
python·word·excel
bu_shuo3 天前
Word中插入文本内容控件并交叉引用
word·内容控件
缺点内向3 天前
C#中如何创建目录(TOC):使用Spire.Doc for .NET实现Word TOC自动化
c#·自动化·word·.net
2301_816997883 天前
Word 创建打开与保存文档
c#·word·xhtml