We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Need to find the text string and remove the same text string without adding any breaks

Hi All,

Can any one please help me in solving the below problem?

Aspose.Words.Document mainDoc = new Aspose.Words.Document(templatedocpath);
mainDoc.Range.Replace(new Regex("\[#\w+#]"), "");
mainDoc.Save(templatedocpath);

the token strings will be like [#test#] and so on…

What i need is to do is to replace this token with empty and if there is no text following to it on the same line then need to remove that entire line.

Any help on this is most appreciative.

Thanks,

Yashwanth

Hi Yashwanth,

Thanks for your inquiry. I will suggest you following code snippet:

Document doc = new Document("D:/temp/input.docx");
Regex regex = new Regex(@"(?[#(.*?)#])", RegexOptions.IgnoreCase);
doc.Range.Replace(regex, new InsertDocumentAtReplaceHandler(), true);
doc.Save("D:/temp/Out.docx");
public class InsertDocumentAtReplaceHandler : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        String title = e.Match.Groups["title"].Value.Trim();
        //Replace title variable
        e.Replacement = string.Empty;
        //You can customize document according your need (e.MatchNode.Document)
        if (e.MatchNode.ParentNode.ToTxt().Equals(string.Empty))
            e.MatchNode.ParentNode.Remove();
        return ReplaceAction.Replace;
    }
}

I hope this will help. In case of any ambiguity, please let me know.