Document Properties in WPF RichTextBox (SfRichTextBoxAdv)

15 Jul 20213 minutes to read

WPF RichTexBox keep tracking the statistics about your documents. These statistics contains information about word count, paragraph count and pages count.

Word Count

RichTextBox automatically counts the number of words in a document while you type. You can get the words count from WordCount property. The default value of this property is 0.

The following sample code demonstrates how to get the total number of words in the document.

<TextBlock Text="{Binding Path=WordCount, Mode=TwoWay}"  />
int wordCount.Text = richTextBoxAdv.WordCount.ToString();
wordCount.Text = richTextBoxAdv.WordCount.ToString()

Paragraph Count

RichTextBox automatically counts the number of paragraphs in a document while you type. You can get the paragraph count from ParagraphCount property. The default value of this property is 0. Also, it ignores empty paragraphs.

The following sample code demonstrates how to get the total number of paragraphs in the document.

<TextBlock Name="ParagraphCount" Text="{Binding Path=ParagraphCount, Mode=TwoWay}" />
int paragraphCount.Text = richTextBoxAdv.ParagraphCount.ToString();
paragraphCount.Text = richTextBoxAdv.ParagraphCount.ToString()

Page Count

RichTextBox counts the number of pages in a document while you type. You can get the pages count from PageCount property. The default value of this property is 0.

The following sample code demonstrates how to get the total number of pages in the document.

<TextBlock x:Name="PageCount" Grid.Row="0" />
<RichTextBoxAdv:SfRichTextBoxAdv Grid.Row="1" x:Name="richTextBoxAdv" SelectionChanged="RichTextBoxAdv_SelectionChanged"/>
private void RichTextBoxAdv_SelectionChanged(object obj, SelectionChangedEventArgs args)
{
	pageCount.Text = richTextBoxAdv.PageCount.ToString();
}
Private Sub RichTextBoxAdv_SelectionChanged(ByVal obj As Object, ByVal args As SelectionChangedEventArgs)
pageCount.Text = richTextBoxAdv.PageCount.ToString()
End Sub

Current Page number

The CurrentPageNumber property in the RichTextBox control returns the page number where the selection(cursor) is present.

The following sample code demonstrates how to get current page number in the document.

<TextBlock x:Name="CurrentPageNumber" Grid.Row="0" />
<RichTextBoxAdv:SfRichTextBoxAdv Grid.Row="1" x:Name="richTextBoxAdv" SelectionChanged="RichTextBoxAdv_SelectionChanged"/>
private void RichTextBoxAdv_SelectionChanged(object obj, SelectionChangedEventArgs args)
{
	currentPageNumber.Text = richTextBoxAdv.CurrentPageNumber.ToString();
}
Private Sub RichTextBoxAdv_SelectionChanged(ByVal obj As Object, ByVal args As SelectionChangedEventArgs)
    currentPageNumber.Text = richTextBoxAdv.CurrentPageNumber.ToString()
End Sub

NOTE

The above PageCount and CurrentPageNumber properties are not a dependency property. And it is not notifying for dynamic changes. So, get these properties value in
selection changed event.
You can also explore our WPF RichTextBox example to knows how to render and configure the editing tools.