Accurate counting of words, characters, paragraphs, lines, and pages is essential in achieving precise document analysis. By meticulously tracking these metrics, writers can gain valuable insights into the length, structure, and overall composition of their work. In this article, we will explain how to count words, characters, paragraphs, lines, and pages in Word documents in C# using Spire.Doc for .NET.
- Count Words, Characters, Paragraphs, Lines, and Pages in a Word Document in C#
- Count Words and Characters in a Specific Paragraph of a Word Document in C#
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Count Words, Characters, Paragraphs, Lines, and Pages in a Word Document in C#
Spire.Doc for .NET provides the BuiltinDocumentProperties class that enables you to retrieve crucial information from your Word documents. By using this class, you can access a wealth of details, including both the built-in and custom properties, as well as the precise counts of words, characters, paragraphs, lines, and pages contained within the document. The detailed steps are as follows.
- Initialize an object of the Document class.
- Load a sample Word document using the Document.LoadFromFile() method.
- Get the BuiltinDocumentProperties object using the Document.BuiltinDocumentProperties property.
- Get the numbers of words, characters, paragraphs, lines, and pages in the document using the WordCount, CharCount, ParagraphCount, LinesCount and PageCount properties of the BuiltinDocumentProperties class.
- Initialize an object of the StringBuilder class and append the results to it using the StringBuilder.AppendLine() method.
- Write the content in the StringBuilder to a text file using the File.WriteAllText() method.
- C#
using Spire.Doc; using System.IO; using System.Text; namespace CountWordsCharactersEtcInWord { internal class Program { static void Main(string[] args) { //Initialize an object of the Document class Document document = new Document(); //Load a sample Word document document.LoadFromFile("Input.docx"); //Get the BuiltinDocumentProperties object BuiltinDocumentProperties properties = document.BuiltinDocumentProperties; //Get the numbers of words, characters, paragraphs, lines, and pages in the document int wordCount = properties.WordCount; int charCount = properties.CharCount; int paraCount = properties.ParagraphCount; int lineCount = properties.LinesCount; int pageCount = properties.PageCount; //Initialize an object of the StringBuilder class StringBuilder sb = new StringBuilder(); //Append the results to the 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); //Write the content of the StringBuilder to a text file File.WriteAllText("result.txt", sb.ToString()); document.Close(); } } }
Count Words and Characters in a Specific Paragraph of a Word Document in C#
In addition to counting the words and characters in an entire Word document, Spire.Doc for .NET enables you to count the words and characters of a specific paragraph by using the Paragraph.WordCount and Paragraph.CharCount properties. The detailed steps are as follows.
- Initialize an object of the Document class.
- Load a sample Word document using the Document.LoadFromFile() method.
- Get a specific paragraph using the Document.Sections[sectionindex].Paragraphs[paragraphIndex] property.
- Get the numbers of words and characters in the paragraph using the Paragraph.WordCount and Paragraph.CharCount properties.
- Initialize an object of the StringBuilder class and append the results to it using the StringBuilder.AppendLine() method.
- Write the content in the StringBuilder to a text file using the File.WriteAllText() method.
- C#
using Spire.Doc; using Spire.Doc.Documents; using System.IO; using System.Text; namespace CountWordsAndCharactersForParagraph { internal class Program { static void Main(string[] args) { //Initialize an object of the Document class Document document = new Document(); //Load a sample Word document document.LoadFromFile("Input.docx"); //Get a specific paragraph Paragraph paragraph = document.Sections[0].Paragraphs[0]; //Get the numbers of words and characters in the paragraph int wordCount = paragraph.WordCount; int charCount = paragraph.CharCount; //Initialize an object of the StringBuilder class StringBuilder sb = new StringBuilder(); //Append the results to the StringBuilder sb.AppendLine("The number of words: " + wordCount); sb.AppendLine("The number of characters: " + charCount); //Write the content of the StringBuilder to a text file File.WriteAllText("result.txt", sb.ToString()); document.Close(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.