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

相关推荐
bu_shuo1 小时前
使用Zotero在Word中插入参考文献
word·zotero
lifallen3 小时前
Word/WPS 制表位分析:如何设置公式居中和编号右对齐
word·wps
spencer_tseng3 小时前
Office 2003 Word [header row duplication]
word
CodeCraft Studio1 天前
纯前端文档编辑组件——Spire.WordJS全新发布
前端·javascript·word·office·spire.wordjs·web文档编辑·在线文档编辑器
伟贤AI之路1 天前
原创分享:Markdown 转 Word 工具,一键导出Word/PDF文档
pdf·word·markdown·markdown转
爱吃山竹的大肚肚1 天前
使用 poi-tl 生成 Word 文档并上传到 Minio
word
我的golang之路果然有问题1 天前
word中latex插入矩阵的语法问题
笔记·学习·矩阵·word·latex·template method·分享
liyayou1 天前
WPS Word根据模板生成不同日期数据页
word
程序员柒叔1 天前
Dify知识库- Word文档处理
大模型·word·workflow·知识库·工作流·dify