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

What's the best way to select/replace specified docfragment content inserted programmatically ?

1 Answer 239 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Christophe
Top achievements
Rank 1
Christophe asked on 23 Mar 2016, 10:53 AM

Hi Telerik Forum,
I'd like to put a bookmark (or another marker) on severals fragment doc (inserted programmatically into the radrichtexteditor) in order to easily select them / delete them / replace the whole text/table inside them. I tried to use textrange object in order to do it but without success.
I don't know what's the best approach to do theses actions above.

Some precision about my app :
* my paragraphs text comes from database in html format, so i use htmlprovider to insert them into my final document
* user can modify paragraph with different buttons in order to change the defaults paragraphs generated before

Thx for your help :)

Here's my sample code :

Shared Sub GenerateDocument(rte As RadRichTextEditor)
    Dim document = New RadDocument()
    rte.Document = document
 
    Dim htmlProvider = New HtmlFormatProvider()
    Dim parHtml1 = "<span>Start of Paragrah1</span><br/><table style='border:1px solid black'><tr><td>Desc.</td><td>Value</td></tr><tr><td>Variable1</td><td>10.23</td></tr></table>Some text after the table<br/><span>End of Paragrah1</span><br/>"
    Dim parHtml2 = "<span>Start of Paragrah2</span><br/>Some text before creating table<table style='border:1px solid black'><tr><td>Desc.</td><td>Value</td></tr><tr><td>Variable2</td><td>50.18</td></tr></table><br/><span>End of Paragrah2</span><br/>"
 
    'Converting html to raddoc
    Dim docPar1 = htmlProvider.Import(parHtml1)
    Dim docPar2 = htmlProvider.Import(parHtml2)
 
    'Creating fragments
    Dim fragmentPar1 = New DocumentFragment(docPar1)
    Dim fragmentPar2 = New DocumentFragment(docPar2)
 
    Dim documentEditor As New RadDocumentEditor(document)
 
    'Inserting paragraph 1
    rte.Document.CaretPosition.MoveToLastPositionInDocument()
    documentEditor.InsertBookmark("bookmarkPar1")
    documentEditor.InsertFragment(fragmentPar1)
 
    'Inserting paragraph 2
    rte.Document.CaretPosition.MoveToLastPositionInDocument()
    documentEditor.InsertBookmark("bookmarkPar2")
    documentEditor.InsertFragment(fragmentPar2)
 
    'Trying of selecting / delete / replace all content in specified bookmark (that doesn't work)
    SelectAndUpdateContentBookmark("bookmarkPar1", rte, String.Empty) 'only select
    SelectAndUpdateContentBookmark("bookmarkPar2", rte, "New content") 'select and replace with new text
End Sub
 
Shared Sub SelectAndUpdateContentBookmark(ByVal bookmarkName As String, rte As RadRichTextEditor, newContent As String)
    rte.Focus()
 
    Dim bookmarks() As BookmarkRangeStart = rte.Document.GetAllBookmarks().ToArray()
    Dim posstart As New DocumentPosition(rte.Document)
    Dim posend As New DocumentPosition(rte.Document)
    Dim item = bookmarks.Where(Function(x) x.Name = bookmarkName).FirstOrDefault
 
    If item IsNot Nothing Then
        rte.Document.GoToBookmark(item)
        posstart.MoveToInline(TryCast(item.FirstLayoutBox, InlineLayoutBox), 0)
        posend.MoveToInline(TryCast(item.End.FirstLayoutBox, InlineLayoutBox), 0)
        posstart.MoveToNextInline()
        rte.Document.Selection.SetSelectionStart(posstart)
        rte.Document.Selection.AddSelectionEnd(posend)
 
        'Replacing with new content
        If Not String.IsNullOrEmpty(newContent) Then
            rte.Delete(False)
            rte.Insert(newContent)
        End If
    End If
End Sub

1 Answer, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 24 Mar 2016, 04:12 PM
Hi Christophe,

Thank you for writing.

The bookmarks you are creating at your RadRichTextEditor are missing a start and an end. In order to achieve your task you have two options:
  1. Perform a selection in RadRichTextEditor before adding a bookmark, this way the BookmarkRangeStart  and BookmarkRangeEnd will be set automatically. You can test it this way:
    Shared Sub GenerateDocument(rte As RadRichTextEditor)
        Dim document = New RadDocument()
        rte.Document = document
     
        Dim htmlProvider = New HtmlFormatProvider()
        Dim parHtml1 = "<span>Start of Paragrah1</span><br/><table style='border:1px solid black'><tr><td>Desc.</td><td>Value</td></tr><tr><td>Variable1</td><td>10.23</td></tr></table>Some text after the table<br/><span>End of Paragrah1</span><br/>"
     
        'Converting html to raddoc
        Dim docPar1 = htmlProvider.Import(parHtml1)
     
        'Creating fragments
        Dim fragmentPar1 = New DocumentFragment(docPar1)
     
        Dim documentEditor As New RadDocumentEditor(document)
        documentEditor.InsertFragment(fragmentPar1)
     
        rte.Document.Selection.SelectAll()
        rte.InsertBookmark("bookmarkPar1")
     
        SelectAndUpdateContentBookmark("bookmarkPar1", rte, "New content")
    End Sub
    You might also consider the following documentation article useful: Selection
  2. Define BookmarkRangeStart and BookmarkRangeEnd  object and add the as inline elements to your document.

I hope this information was useful. Should you have further questions please do not hesitate to write back.


Regards,
Hristo Merdjanov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
RichTextEditor
Asked by
Christophe
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Share this question
or