How to find the empty paragraphs in the Word document using Essential DocIO in .net WebForms ?
You may have various requirements to check whether a paragraph is empty or not. A paragraph can be considered as empty if the child entities count is zero, contains only the elements invisible in viewer (like bookmark, field marks, etc.), or text contains only escape characters (like white space, 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 paragraph child entities count is zero. return (paragraph.ChildEntities.Count == 0); }
VB
Private Function IsEmptyParagraph(paragraph As IWParagraph) As Boolean 'Returns true, if 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 Text range 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 text range text contains character 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 Text range 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 text range text contains character other than escape characters. Return False End If Next index Return True End Function
Conclusion
I hope you enjoyed learning about how tofind the empty paragraphs in the Word document using Essential DocIO in .find the empty paragraphs in the Word document using Essential DocIO in .NET WebForms.
You can refer to our .NET WebForms feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET WebForms example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!