How to count the number of words, pages and symbols in a document using C# and VB.Net

Counting words, pages and characters in a DOCX using C# and VB.Net

Counting the number of words, pages and symbols in the document without using third-party programs and services.
If you need to determine exactly the quality of any characters in the document, simply use the optioin: СalculateStats ();

You must select the document in which the character count will be calculated:


               //Load a DOCX file
            string filePath = @"d:\Tempos\words.docx";
            DocumentCore dc = DocumentCore.Load(filePath);
The next step is call the option to update and count the number of characters:

             dc.CalculateStats();

As a result of the count, Document.Net provides you with full statistics:


You may find extra info about Document Properties.

Well done!

 

Complete code

using System;
using SautinSoft.Document;
using SautinSoft.Document.Drawing;
using System.IO;
using System.Linq;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            CalculateStatistics();
        }
		
        /// <summary>
        /// Calculates the number of words, pages and characters in a document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/examples/counting-words-paragraphs-net-csharp-vb.php
        /// </remarks>
        static void CalculateStatistics()
        {
            // Load a DOCX file.
            string filePath = @"..\..\words.docx";

            DocumentCore dc = DocumentCore.Load(filePath);

            // Update and count the number of words and pages in the file.
            dc.CalculateStats();

            // Show statistics.
            Console.WriteLine("Pages: {0}", dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Pages]);
            Console.WriteLine("Paragraphs: {0}", dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Paragraphs]);
            Console.WriteLine("Words: {0}", dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Words]);
            Console.WriteLine("Characters: {0}", dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Characters]);
            Console.WriteLine("Characters with spaces: {0}", dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.CharactersWithSpaces]);
        }
    }
}

Download.

        
Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        CalculateStatistics()
    End Sub

    ''' <summary>
    ''' Calculates the number of words, pages and characters in a document.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/examples/counting-words-paragraphs-net-csharp-vb.php
    ''' </remarks>
    Sub CalculateStatistics()
        ' Load a DOCX file.
        Dim filePath As String = "..\words.docx"

        Dim dc As DocumentCore = DocumentCore.Load(filePath)

        ' Update and count the number of words and pages in the file.
        dc.CalculateStats()

        ' Show statistics.
        Console.WriteLine("Pages: {0}", dc.Document.Properties.BuiltIn(BuiltInDocumentProperty.Pages))
        Console.WriteLine("Paragraphs: {0}", dc.Document.Properties.BuiltIn(BuiltInDocumentProperty.Paragraphs))
        Console.WriteLine("Words: {0}", dc.Document.Properties.BuiltIn(BuiltInDocumentProperty.Words))
        Console.WriteLine("Characters: {0}", dc.Document.Properties.BuiltIn(BuiltInDocumentProperty.Characters))
        Console.WriteLine("Characters with spaces: {0}", dc.Document.Properties.BuiltIn(BuiltInDocumentProperty.CharactersWithSpaces))
    End Sub
End Module

Download.

© SautinSoft 2002 - 2019