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

Free Support Forum - aspose.com

Paragraph.Range.Replace not matching text within paragraph

Hi,

I am facing following issue in Aspose.Words .Net.
I have Base64 of word document. From Base64 will be getting Aspose Document.
Use below code to get paragraphs for the document:

NodeCollection paragraphs = document.GetChildNodes(NodeType.Paragraph, true);

In this paragraph collection I am getting paragraph text containing \r. Also Unicode characters are there.
But if I am trying to find text in that paragraph, it won’t match and not calling my replace evaluator.
Eg. Paragraph_Text = "Professinal services agreement. The consultant should accept all clauses\r"
and Text_To_Strike_Out = "The consultant should accept all clauses\r"
and If I Pass this text to replace evaluator, it is not calling replacing callback method.

FindReplaceOptions options = new FindReplaceOptions
{
    ReplacingCallback = new ReplaceEvaluatorFindAndStrikeThrough(),
    Direction = FindReplaceDirection.Backward,
    FindWholeWordsOnly = true
};
para.Range.Replace(Text_To_Strike_Out, "", options);

Is this because of \r in text??
Can you please provide solution on this?

Thanks
Deepali Shirude

@Deepali_Shirude Could you please ZIP and upload your source code and input document here to reproduce the issue? We will check the issue and provide you more information.

Hi,

Please find the attached source code files and sample doc.

Aspose Docs.zip (110.3 KB)

@Deepali_Shirude
Unfortunately, you cannot use Paragraph.GetText() as a pattern in this case. As follows from GetText() method description “The returned string includes all control and special characters as described in ControlChar”. In your case the search pattern is the text along with field contents
\u0013 HYPERLINK \"https://www.lawinsider.com/contracts/3gUUPIMc3RC\" \u0014AGREEMENT BETWEEN\u0015ÂNOTE
Range.Replace() uses the document text content as a basis, as it is implemented in MS Word. So you can use as a pattern only “AGREEMENT BETWEEN NOTE” in this case.

Hi Vadim,

Thanks for your response.
Any alternative solution for this if I want to find the text in paragraph and strike it out?
Can we remove those control characters and then match?

@Deepali_Shirude

The easiest way to do it is this one.

StringBuilder sb = new StringBuilder();
foreach (Run run in para.Runs)
{
    sb.Append(run.Text);
}
string text_To_Strike_new = sb.ToString();