How to move caret pos in radeditor

5 posts, 1 answers
  1. Kenneth
    Kenneth avatar
    101 posts
    Member since:
    Apr 2014

    Posted 13 Aug 2014 Link to this post

    I want to traverse a document and programatically insert merge fields at specific locations.
    Below I wrote some code that does traverse the document, but InsertField always inserts the merge filed at the document start. Pos is being moved but it seems the radeditor document caret pos is not.

    How can I move the caret position of the rededitor to match where the documnetposition class is positioned?

    Also I want to replace keywords in my document with the merge fields, so how do I delete the current word?

    Code:

    Using reader As New StreamReader(fs)
     
        Dim s As String = reader.ReadToEnd
        Dim provider_xaml As New Telerik.Windows.Documents.FormatProviders.Xaml.XamlFormatProvider
        Dim radEditor As New Telerik.Windows.Documents.Model.RadDocumentEditor(provider_xaml.Import(s))
        Dim pos As New Telerik.Windows.Documents.DocumentPosition(radEditor.Document)
     
        pos.MoveToFirstPositionInDocument()
     
        Do Until Telerik.Windows.Documents.DocumentPosition.IsAtDocumentEnd(pos)
     
            pos.MoveToNextWordStart()
            Dim w As String = pos.GetCurrentSpanBox.Text
            If w.StartsWith("&") Then
                Dim field As New Telerik.Windows.Documents.Model.MergeField
                field.DisplayMode = Telerik.Windows.Documents.Model.FieldDisplayMode.Code
                field.PropertyPath = "Name"
                radEditor.InsertField(field)
            End If
     
        Loop
     
    End Using
  2. Kenneth
    Kenneth avatar
    101 posts
    Member since:
    Apr 2014

    Posted 13 Aug 2014 in reply to Kenneth Link to this post

    After looking at my code more closely, it does not traverse the document as I expected.  MoveToNextWordStart seems to move within the current word (perhaps the definition of a word would be useful) moving different positions within the word...so I guess I don't know how to traverse a document...

    Essentially I want to do a find and replace, but the finds is keyed off of a pattern, like '&{some_text]' and the replace is with a merge filed.

    The source document will be rtf, the result will be saved as xaml.  I know how to do everything except the 'find' part.  movetonextwordstart is not acting like I expected
  3. DevCraft banner
  4. Kenneth
    Kenneth avatar
    101 posts
    Member since:
    Apr 2014

    Posted 13 Aug 2014 in reply to Kenneth Link to this post

    I've decided the best way to do this is with the search and regex, so my new code is below.

    Once selected, I want to replace the selected text with a merge filed.  How would I do that?

    Using reader As New StreamReader(fs)
     
        Dim s As String = reader.ReadToEnd
     
        Dim provider_rtf As New Telerik.Windows.Documents.FormatProviders.Rtf.RtfFormatProvider
     
        editor.Document = provider_rtf.Import(s)
        editor.Document.Selection.Clear()
     
        Dim search As New Telerik.Windows.Documents.TextSearch.DocumentTextSearch(editor.Document)
     
        For Each foundText As Telerik.Windows.Documents.TextSearch.TextRange In search.FindAll("&\(.*\)|&\[.*\]|&[0-9]*")
     
            editor.Document.Selection.AddSelectionStart(foundText.StartPosition)
            editor.Document.Selection.AddSelectionEnd(foundText.EndPosition)
     
        Next
     
        editor.Document.MailMergeDataSource.ItemsSource = EmployeeList
     
    End Using
  5. Kenneth
    Kenneth avatar
    101 posts
    Member since:
    Apr 2014

    Posted 13 Aug 2014 in reply to Kenneth Link to this post

    Solved.  There was an interesting problem when my regex matched two of my find strings that were butted up against each other, like this:
    &[123]&[456].  In this case when I used FindAll and looped through the results, after I replaced &[123] then next match was corrupted, like this: 23 Name}&[456]. th next insert threw an exception that inline was not part of the document, which is true, it was replaced.

    I solved this problem my using find instead of find all, that way I'm not changing the document underneath.  Code follows:

    In case you are wondering, I'm converting my old rtf fils with my own brand of mergefield into the xam format.

    Using reader As New StreamReader(fs)
     
          Dim s As String = reader.ReadToEnd
          Dim provider_rtf As New Telerik.Windows.Documents.FormatProviders.Rtf.RtfFormatProvider
     
          editor.Document = provider_rtf.Import(s)
          editor.Document.Selection.Clear()
     
          Dim search As New Telerik.Windows.Documents.TextSearch.DocumentTextSearch(editor.Document)
     
          Do
              Dim foundText As Telerik.Windows.Documents.TextSearch.TextRange = search.Find("&\(.*\)|&\[.*?\]|&[0-9]*")
     
              If foundText Is Nothing Then Exit Do
     
              editor.Document.Selection.AddSelectionStart(foundText.StartPosition)
              editor.Document.Selection.AddSelectionEnd(foundText.EndPosition)
              Dim sel As String = editor.Document.Selection.GetSelectedText
     
              'todo: replace sel with correct merge field
     
              Dim field As New Telerik.Windows.Documents.Model.MergeField
              field.DisplayMode = Telerik.Windows.Documents.Model.FieldDisplayMode.Code
              field.PropertyPath = "Name"
              editor.InsertField(field)
          Loop
     
          editor.Document.MailMergeDataSource.ItemsSource = EmployeeList
     
      End Using


  6. Answer
    Petya
    Admin
    Petya avatar
    975 posts

    Posted 18 Aug 2014 Link to this post

    Hi Kenneth,

    I'm glad to hear you were able to replicate the desired scenario and thank you for sharing your solution with the community. Should you have any additional questions please feel free to get back to us.

    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
DevCraft banner