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

Insert HTML text into existing RadDocument

3 Answers 464 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Christophe
Top achievements
Rank 1
Christophe asked on 29 Feb 2016, 10:01 AM

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

3 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 29 Feb 2016, 11:10 AM
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
0
Christophe
Top achievements
Rank 1
answered on 29 Feb 2016, 12:41 PM

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

Thank you Dimitar :)

0
Vipul
Top achievements
Rank 1
answered on 23 Nov 2017, 06:48 AM
Thank a lot Dimitar for this solution that exactly we are looking! It saves almost a day!!! Superb... 
Tags
RichTextEditor
Asked by
Christophe
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Christophe
Top achievements
Rank 1
Vipul
Top achievements
Rank 1
Share this question
or