Free Support Forum - aspose.com

How can I handle document just like the ‘string.trim()' function

I want to handle document just like the ‘string.trim()’ function.
" abc ".trim() = “abc”

I made a recursive method to delete the paragraphs with empty text before and after the document.But if it has image it will be remove also.I can judge if there is a shape node.
But maybe it has another Node which text’s length is empty? How can I solve it.

@zhengkai

Thanks for your inquiry. Please use the following code example to get desired results.

Document doc = new Document(“D:\Temp\input.docx”);

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
 {
    bool otherNodeExist = false;

    foreach (Node node in para.GetChildNodes(NodeType.Any, true))
    {
         if (node.NodeType != NodeType.Run)
         {
             otherNodeExist = true;
              break;
          }
     }

     if (!para.HasChildNodes || (para.ToString(SaveFormat.Text).Trim().Equals("") && !otherNodeExist))
     {
          para.Remove();
     }
}

doc.Save(“D:\Temp\output_18.11.docx”);

Hope, this helps.