Category / Section
How to find the empty paragraphs in the Word document using DocIO in WindowsPhone ?
3 mins read
You may have various requirements to check whether a paragraph is empty or not. A paragraph can be considered empty if the child entities count is zero, contains only elements invisible in the viewer (like bookmarks, field marks, etc.), or the text contains only escape characters (like whitespace, new line, etc.).
The following code demonstrates how to check whether the paragraph has no child items.
C#
private bool IsEmptyParagraph(IWParagraph paragraph) { // Returns true if the paragraph child entities count is zero. return (paragraph.ChildEntities.Count == 0); }
VB
Private Function IsEmptyParagraph(paragraph As IWParagraph) As Boolean ' Returns true if the paragraph child entities count is zero. Return (paragraph.ChildEntities.Count = 0) End Function
The following code demonstrates how to check whether the paragraph has no child items that are visible in document viewer.
C#
private bool IsEmptyParagraph(IWParagraph paragraph) { for (int index = 0; index < paragraph.ChildEntities.Count; index++) { // Checks for BookmarkStart, BookmarkEnd, WFieldMark entities. if (paragraph.ChildEntities[index] is BookmarkStart || paragraph.ChildEntities[index] is BookmarkEnd || paragraph.ChildEntities[index] is WFieldMark) continue; else // Returns false, if any other paragraph item exists. return false; } return true; }
VB
Private Function IsEmptyParagraph(paragraph As IWParagraph) As Boolean For index As Integer = 0 To paragraph.ChildEntities.Count - 1 ' Checks for BookmarkStart, BookmarkEnd, WFieldMark entities. If TypeOf paragraph.ChildEntities(index) Is BookmarkStart Or TypeOf paragraph.ChildEntities(index) Is BookmarkEnd Or TypeOf paragraph.ChildEntities(index) Is WFieldMark Then Continue For Else ' Returns false, if any other paragraph item exists. Return False End If Next index Return True End Function
The following code demonstrates how to check whether the paragraph has no valid text except escape characters. You can define the escape characters based on your requirement.
C#
private bool IsEmptyParagraph(IWParagraph paragraph) { char[] trimCharacters = new char[] { ' ', '\n', '\t', '\v' }; for (int index = 0; index < paragraph.ChildEntities.Count; index++) { // Checks if the current entity is TextRange and the text is empty space, tab characters, new line characters. if ((paragraph.ChildEntities[index] is WTextRange) && (paragraph.ChildEntities[index] as WTextRange).Text.Trim(trimCharacters) == string.Empty) continue; else // Returns false if any other paragraph item exists or the TextRange text contains characters other than escape characters. return false; } return true; }
VB
Private Function IsEmptyParagraph(paragraph As IWParagraph) As Boolean Dim trimCharacters() As Char = {" ", "\t", "\n", "\v"}
For index As Integer = 0 To paragraph.ChildEntities.Count - 1
' Checks if the current entity is TextRange and the text is empty space, tab characters, new line characters.
If TypeOf paragraph.ChildEntities(index) Is WTextRange Then
Dim textRange As IWTextRange = paragraph.ChildEntities(index)
If textRange.Text.Trim(trimCharacters) = String.Empty Then
Continue For
End If
Else
' Returns false if any other paragraph item exists or the TextRange text contains characters other than escape characters.
Return False
End If
Next index
Return True
End Function