Articles in this section
Category / Section

How to find the empty paragraphs in the Word document using DocIO in .NET WebForms ?

3 mins read

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

 

 

Note:

A new version of Essential® Studio® for ASP.NET is available. Versions prior to the release of Essential® Studio® 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential® Studio® for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential® Studio® for ASP.NET. Although Syncfusion® will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!





Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied