Insert HTML text into existing RadDocument

3 posts, 1 answers
  1. Christophe
    Christophe avatar
    3 posts
    Member since:
    Mar 2015

    Posted 29 Feb Link to this post

    Hi Telerik forum,
    I began to use the RichTextEditor to create programmatically a radDocument (so i use the paragraph, span, section object to create it).
    Once the document is shown, users can insert formatted paragraph to personalize the document. The html text paragraph are stored in database in html format.

    The thing i need is to be able to insert formatted html paragraph into this raddocument already generated.
    My problem is to evaluate all the html text (with differents tags inside it, span, table, tr etc). I use the HtmlFormatProvider that works perfectly but it produce a new raddocument. So i tried to convert the htmlformatprovider document into a DocumentFragment, but i have to use fragment.EnumerateChildrenOfType(Of object) and explicitly specify the object type (what i wouldn't do).
    What's the best approach to insert html text into an existing raddocument ?

    Thanks for your help, hoping that my question is clear.

    Here's my sample code : 

    Dim document As New RadDocument
    Dim section As New Section
     
    Dim p9 As New Paragraph
    Dim presta1 As New RadDocument
    Dim msgHtml = New Html.HtmlFormatProvider
    presta1 = msgHtml.Import("<SPAN><STRONG>Table content :</STRONG></SPAN><TABLE STYLE='border:1px solid black'><TR><TD>Marque</TD><TD>Type</TD><TD>N° série</TD><TD>Fluide</TD></TR><TR><TD>-</TD><TD>-</TD><TD>-</TD><TD>-</TD></TR><TR><TD>-</TD><TD>-</TD><TD>-</TD><TD>-</TD></TR></TABLE>")
     
    Dim fragment = New DocumentFragment(presta1)
    section.Blocks.Add(fragment.EnumerateChildrenOfType(Of Table).ToList.First) 'its working but only with Table tag
     
    'Saut de ligne
    section.Blocks.Add(New Paragraph)
     
    document.Sections.Add(section)
    return document

  2. Answer
    Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 29 Feb Link to this post

    Hi Christophe,

    Thank you for writing.

    You approach to use the HTML format provider is correct, however, to insert the fragment you can create a RadDocumentEditor and use the InsertFragment method:
    Private Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim document As RadDocument = radRichTextEditor1.Document
     
        Dim msgHtml = New HtmlFormatProvider()
        Dim presta1 = msgHtml.Import("<SPAN><STRONG>Table content :</STRONG></SPAN><TABLE STYLE='border:1px solid black'><TR><TD>Marque</TD><TD>Type</TD><TD>N° série</TD><TD>Fluide</TD></TR><TR><TD>-</TD><TD>-</TD><TD>-</TD><TD>-</TD></TR><TR><TD>-</TD><TD>-</TD><TD>-</TD><TD>-</TD></TR></TABLE>")
     
        Dim fragment = New DocumentFragment(presta1)
     
        Dim documentEditor As New RadDocumentEditor(document)
        documentEditor.InsertFragment(fragment)
     
     
    End Sub

    Let me know if I can assist you further.
     
    Regards,
    Dimitar
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Christophe
    Christophe avatar
    3 posts
    Member since:
    Mar 2015

    Posted 29 Feb in reply to Dimitar Link to this post

    It's works like a charm, exactly what i needed.

    Thank you Dimitar :)

Back to Top