Independentsoft
Home
Products
Purchase
Support
Downloads
Company
Contact
ODF .NET
>
Tutorial
> Word count
The example shows you how to open a text document and count number of words and paragraphs.
C# example
using System; using Independentsoft.Odf; namespace Sample { class Program { static void Main(string[] args) { int wordCount = 0; string[] separator = new string[] { " " }; TextDocument doc = new TextDocument("c:\\test.odt"); Text[] texts = doc.GetTexts(); for (int i = 0; i < texts.Length; i++) { string[] words = texts[i].Value.Split(separator, StringSplitOptions.RemoveEmptyEntries); wordCount += words.Length; } Paragraph[] paragraphs = doc.GetParagraphs(); int emptyParagraphCount = 0; for (int j = 0; j < paragraphs.Length; j++) { if(paragraphs[j].Content.Count == 0) { emptyParagraphCount++; } } Console.WriteLine("Paragraphs=" + paragraphs.Length); Console.WriteLine("Empty paragraphs=" + emptyParagraphCount); Console.WriteLine("Words=" + wordCount); Console.Read(); } } }
VB example
Imports System Imports Independentsoft.Odf Module Module1 Sub Main(ByVal args() As String) Dim wordCount As Integer = 0 Dim separator As String() = New String() {" "} Dim doc As New TextDocument("c:\test.odt") Dim texts As Text() = doc.GetTexts() For i As Integer = 0 To texts.Length - 1 Dim words As String() = texts(i).Value.Split(separator, StringSplitOptions.RemoveEmptyEntries) wordCount += words.Length Next Dim paragraphs As Paragraph() = doc.GetParagraphs() Dim emptyParagraphCount As Integer = 0 For j As Integer = 0 To paragraphs.Length - 1 If paragraphs(j).Content.Count = 0 Then emptyParagraphCount += 1 End If Next Console.WriteLine("Paragraphs=" & paragraphs.Length) Console.WriteLine("Empty paragraphs=" & emptyParagraphCount) Console.WriteLine("Words=" & wordCount) Console.Read() End Sub End Module