This is a migrated thread and some comments may be shown as answers.

Find and replace?

3 Answers 287 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Isak
Top achievements
Rank 1
Isak asked on 01 Sep 2014, 06:47 AM
Hello, sorry if this is posted in the wrong section of the forum.

Now, my question is simple.
I want to find and replace text(strings) in a document, I've tried using the following code:

private void ReplaceAllMatches(RadDocument document, string toSearch, string toReplaceWith)
{
       DocumentTextSearch search = new DocumentTextSearch(document);
       List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
       foreach (var textRange in search.FindAll(toSearch))
       {
                TextRange newRange = new TextRange(new DocumentPosition(textRange.StartPosition, true),
                new documentPosition(textRange.EndPosition, true));
                rangesTrackingDocumentChanges.Add(newRange);
            }
 
            RadDocumentEditor docEdit = new RadDocumentEditor(document);
            foreach (var textRange in rangesTrackingDocumentChanges)
            {
                document.CaretPosition.MoveToPosition(textRange.StartPosition);               
                document.DeleteRange(textRange.StartPosition, textRange.EndPosition);
                 
                StyleDefinition style = new StyleDefinition();
                style.SetPropertyValue(Span.ForeColorProperty, Colors.Red);
                                               
                document.Insert(toReplaceWith, style);
 
                textRange.StartPosition.Dispose();
                textRange.EndPosition.Dispose();
            }
        }

But I get two errors, "document.DeleteRange() is obsolete use RadDocumentEditor.Delete() instead." and "document.Insert() is obsolete use RadDocumentEditor.InsertInline() instead.".
I can't figure out how to use these functions instead so that's why I need help!

I've never used this library before so all help is very appreciated.
Best regards
Isak

3 Answers, 1 is accepted

Sort by
0
Isak
Top achievements
Rank 1
answered on 01 Sep 2014, 07:49 AM
I also discovered that I'm not using a RadDocument in my whole solution.
So now I have a third problem which is that I some how need to convert from a RadFlowDocument to a RadDocument.
Is this possible?

/ Isak
0
Isak
Top achievements
Rank 1
answered on 01 Sep 2014, 02:05 PM
I found a solution.
I have to load the .docx as a RadDocument, edit it and save it as a .docx again.
Then later in the code I can load it as any of the types.

I'll post my code so everyone else with this problem can see the solution.
This code solves the obsolete problem.

private static RadDocument ReplaceAllMatches(RadDocument document, string toSearch, string toReplaceWith)
{
    bool deleted = false;
    DocumentTextSearch search = new DocumentTextSearch(document);
    List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
 
    foreach (var textRange in search.FindAll(toSearch))
    {
        TextRange newRange = new TextRange(new DocumentPosition(textRange.StartPosition, true), new DocumentPosition(textRange.EndPosition, true));
        rangesTrackingDocumentChanges.Add(newRange);
    }
 
    RadDocumentEditor docEdit;
    foreach (var textRange in rangesTrackingDocumentChanges)
    {
        document.CaretPosition.MoveToPosition(textRange.StartPosition);
 
        document.Selection.AddSelectionStart(textRange.StartPosition);
        document.Selection.AddSelectionEnd(textRange.EndPosition);
        docEdit = new RadDocumentEditor(document);
        docEdit.Delete(deleted);
        document.Selection.Clear();
 
        StyleDefinition style = new StyleDefinition();
        style.SetPropertyValue(Span.ForeColorProperty, Colors.Red);
 
        docEdit.Insert(toReplaceWith);
 
        textRange.StartPosition.Dispose();
        textRange.EndPosition.Dispose();
    }
    return document;
}
//
Isak
0
Petya
Telerik team
answered on 03 Sep 2014, 03:42 PM
Hello Isak,

I can see you already achieved the desired result, but wanted to leave some additional comments for anyone else who might stumble upon this thread.

RadDocument is the root element of the document model used by the RadRichTextBox control. RadFlowDocument, on the other hand, is the root of the RadWordsProcessing library's document model. The latter is a component we introduced recently and it does not yet expose a Find/ Find and Replace functionality. This is already in our backlog and you can track the feature here in order to be notified when it is available.

Regarding the obsolete methods of RadDocument, we are planning to remove those soon. Equivalent  methods are exposed by the RadRichTextBox class and for the cases when a document is not shown in the control we introduced the RadDocumentEditor class.

I hope this information is useful.

Regards,
Petya
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
General Discussions
Asked by
Isak
Top achievements
Rank 1
Answers by
Isak
Top achievements
Rank 1
Petya
Telerik team
Share this question
or