使用C#代碼統(tǒng)計Word文檔的單詞、字符、段落、行數(shù)和頁數(shù)
引言
對單詞、字符、段落、行數(shù)和頁數(shù)進(jìn)行準(zhǔn)確統(tǒng)計,對于實現(xiàn)精確的文檔分析至關(guān)重要。通過細(xì)致地追蹤這些指標(biāo),作者可以更深入地了解文檔的長度、結(jié)構(gòu)和整體構(gòu)成。在本文中,我們將介紹如何使用 Spire.Doc for .NET 在 C# 中統(tǒng)計 Word 文檔中的單詞、字符、段落、行數(shù)和頁數(shù)。
安裝 Spire.Doc for .NET
首先,您需要將 Spire.Doc for .NET 包中包含的 DLL 文件添加為 .NET 項目的引用。您可以通過下載鏈接獲取這些 DLL 文件,或通過 NuGet 進(jìn)行安裝。
PM> Install-Package Spire.Doc
在 C# 中統(tǒng)計 Word 文檔中的單詞、字符、段落、行數(shù)和頁數(shù)
Spire.Doc for .NET 提供了 BuiltinDocumentProperties 類,允許您從 Word 文檔中獲取關(guān)鍵信息。通過使用該類,您可以訪問豐富的文檔屬性,包括內(nèi)置和自定義屬性,以及文檔中單詞、字符、段落、行數(shù)和頁數(shù)的精確統(tǒng)計數(shù)據(jù)。
示例代碼如下:
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;
//獲取文檔中的單詞、字符、段落、行數(shù)和頁數(shù)
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();
//將結(jié)果添加到 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 的內(nèi)容寫入文本文件
File.WriteAllText("result.txt", sb.ToString());
document.Close();
}
}
}在 C# 中統(tǒng)計 Word 文檔中某個特定段落的單詞和字符數(shù)量
除了統(tǒng)計整個 Word 文檔中的單詞和字符數(shù)量外,Spire.Doc for .NET 還允許您通過 Paragraph.WordCount 和 Paragraph.CharCount 屬性來統(tǒng)計某個特定段落的單詞數(shù)和字符數(shù)。
示例代碼如下:
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];
//獲取段落中的單詞數(shù)和字符數(shù)
int wordCount = paragraph.WordCount;
int charCount = paragraph.CharCount;
//初始化 StringBuilder 類的對象
StringBuilder sb = new StringBuilder();
//將結(jié)果追加到 StringBuilder
sb.AppendLine("The number of words: " + wordCount);
sb.AppendLine("The number of characters: " + charCount);
//將 StringBuilder 的內(nèi)容寫入文本文件
File.WriteAllText("result.txt", sb.ToString());
document.Close();
}
}
}到此這篇關(guān)于使用C#代碼統(tǒng)計Word文檔的單詞、字符、段落、行數(shù)和頁數(shù)的文章就介紹到這了,更多相關(guān)C#統(tǒng)計Word文檔關(guān)鍵信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Winform實現(xiàn)調(diào)用asp.net數(shù)據(jù)接口實例
這篇文章主要介紹了Winform實現(xiàn)調(diào)用asp.net數(shù)據(jù)接口的方法,以實例的形式講述了數(shù)據(jù)接口及反射辨別響應(yīng)的實現(xiàn)方法,具有一定的參考借鑒價值,需要的朋友可以參考下2014-10-10
C#創(chuàng)建Windows Service(Windows 服務(wù))的方法步驟
本文介紹了如何用C#創(chuàng)建、安裝、啟動、監(jiān)控、卸載簡單的Windows Service 的內(nèi)容步驟和注意事項,具有一定的參考價值,感興趣的可以了解一下2023-11-11
深入淺析c#靜態(tài)多態(tài)性與動態(tài)多態(tài)性
多態(tài)就是多種形態(tài),也就是對不同對象發(fā)送同一個消息,不同對象會做出不同的響應(yīng)。這篇文章主要介紹了c#靜態(tài)多態(tài)性與動態(tài)多態(tài)性的相關(guān)知識,需要的朋友可以參考下2018-09-09
如何讓C#、VB.NET實現(xiàn)復(fù)雜的二進(jìn)制操作
VB.NET和C#屬于高級語言,對二進(jìn)制位操作的支持不是很好,比如沒有了移位運(yùn)算等,用的時候確實很不方便,所以在閑暇之余我重新封裝了一個用于C#、VB.NET的位操作類庫,通過該類庫可以實現(xiàn)數(shù)據(jù)移位、循環(huán)移位、轉(zhuǎn)換為二進(jìn)制、將二進(jìn)制轉(zhuǎn)換為數(shù)據(jù)等2013-07-07
RSA密鑰--JAVA和C#的區(qū)別及聯(lián)系
這篇文章主要介紹了關(guān)于RSA密鑰事件JAVA和C#的區(qū)別及聯(lián)系,文章從RSA語法介紹開始展開詳細(xì)介紹了C#轉(zhuǎn)JAVA及JAVA轉(zhuǎn)C#,需要的小伙伴可以可以參考一下2021-10-10

