Find and replace?

4 posts, 0 answers
  1. Isak
    Isak avatar
    3 posts
    Member since:
    Sep 2014

    Posted 01 Sep 2014 Link to this post

    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
  2. Isak
    Isak avatar
    3 posts
    Member since:
    Sep 2014

    Posted 01 Sep 2014 in reply to Isak Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Isak
    Isak avatar
    3 posts
    Member since:
    Sep 2014

    Posted 01 Sep 2014 Link to this post

    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
  5. Petya
    Admin
    Petya avatar
    975 posts

    Posted 03 Sep 2014 Link to this post

    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.
     
Back to Top