Replace text based on formatting

I am currently working on a console app C# to delete a specific instance of the phrase “Case Study” in a document.

My problem is that the phrase “Case Study” appears multiple times throughout the document, so using Document.Range.Replace() method which is in your documentation results in other instances of the phrase being deleted.

Is there any way to search for unique formatting when using the Replace() method? The “Case Study” I want to delete has unique formatting and it’s the only way to pick it out of the documents.

I am coding using c#, and I would greatly appreciate any ideas or assistance.

Hi
Thank you for your interest in Aspose products.

It is true that the Document.Range.Replace() method replace all instances that match from your document, so what I would suggest is to loop through all the Run objects in the the child nodes of the document.

I added some code with a simple example where I look for a sample to match a font and the text is also bold.

Code:

foreach (Run run in doc.GetChildNodes(NodeType.Run, true))
{
    if (run.GetText().ToLowerInvariant().Contains("case study"))
    {
        if (run.Font.Name == "Arial" && run.Font.Bold == true)
        {
            var text = run.GetText();
            run.Remove(); // will delete the text from the document
            break; // since it is a specific search, we do not need to continue searching
        }
    }
}

As you can see the Run.Font object contains a bunch of details properties that you can use to single out the specific formatted instance you are looking for.

Please let me know if you would like to know something else.
Best regards.