Insert Image from RadRichTextBox into microsoft word document

0 Answers 66 Views
RichTextBox
Patrick
Top achievements
Rank 1
Iron
Iron
Iron
Patrick asked on 10 May 2024, 08:11 PM

In the HtmlExportSettings there is no valid ImageExportMode that word is compatible with as it is not able to display Base64 encoded images.

Is there another ImageExportMode that is compatible?

OR

Is there a better solution to insert images from RichTextBox into a Microsoft word document? Possibly, create RadDocument from the word document I have and replace it in the RadDocument and do conversion to Microsoft word document?

Open to suggestions.

Dimitar
Telerik team
commented on 13 May 2024, 07:23 AM

Hi Patrick, 

This is possible, you can use the DocxFormatProvider to import the document, change it, and then export it back DOCX. Detailed information is available here: WPF RichTextBox - Using DocxFormatProvider.

I hope this helps. Should you have any other questions do not hesitate to ask.
Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 13 May 2024, 02:49 PM | edited

Can I add this docxFormatProvider similar to how you add HtmlFormatProvider in the xaml?

I've tried but it does not appear to work. It is not even an applicable item. My options are:

RtfDataProvider
HtmlDataProvider1
XamlDataProvider
TxtDataProvider

Below is my current HtmlDataProvider
<telerik:HtmlDataProvider RichTextBox="{Binding ElementName=MyRadRichTextBox}" Html="{Binding MyText, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}">
    <telerik:HtmlDataProvider.FormatProvider>
        <telerik:HtmlFormatProvider>
            <telerik:HtmlFormatProvider.ExportSettings>
                <telerik:HtmlExportSettings DocumentExportLevel="Fragment"
                ImageExportMode="UriSource"
                StyleRepositoryExportMode="DontExportStyles"
                StylesExportMode="Inline"
                ExportEmptyDocumentAsEmptyString="True"      
                />
            </telerik:HtmlFormatProvider.ExportSettings>
        </telerik:HtmlFormatProvider>
    </telerik:HtmlDataProvider.FormatProvider>
</telerik:HtmlDataProvider>



Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 13 May 2024, 04:34 PM | edited

Ultimately, this is the goal:

Have a word document that acts as a template. This template will have {tokens} throughout it. These tokens will be replaced with:

- Text
- RichText (including tables, and images)

I need to be able to take the template and replace all tokens with the content from the corresponding dynamically generated RadRichTextBox.


Does RadDocument allow for find and replace of only text? Or can I find and replace tokens and replace with RICHText and export as docx?
Dimitar
Telerik team
commented on 14 May 2024, 05:58 AM

Hi Patrick, 

There is no data provider for the DOCX format since it cannot be bound to a string. With this format, you need to perform import/export in the code. 

Yes, you can replace and insert any elements you want. More on this is available here: WPF RichTextBox - Search.

Let me know if you have additional questions.

Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 14 May 2024, 02:12 PM

So is there some way to bind a property to docx? So that I can access it in the view model?

From this forum post below I see this excerpt:
https://www.telerik.com/forums/binding-to-a-raddocument-inside-viewmodel

"Binding to DocX requires that the property must be of type byte []. Other than that, setting up the view model and the data provider is analogical to that for XamlDataProvider."

Can you give me a xaml example of that how that binding will appear?

Dimitar
Telerik team
commented on 15 May 2024, 12:21 PM

Hi Patrick, 

I have attached a project that shows this.

Let me know how this works for you.

Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 15 May 2024, 11:03 PM

Looks like there might be a few issues with that project.

I am not able to verify if it works because of the following 2 errors.



Severity Code Description Project File Line Suppression State
Error CS1061 'DocxDataProvider' does not contain a definition for 'RichTextBox' and no accessible extension method 'RichTextBox' accepting a first argument of type 'DocxDataProvider' could be found (are you missing a using directive or an assembly reference?)

Severity Code Description Project File Line Suppression State
Error CS0029 Cannot implicitly convert type 'Telerik.Windows.Documents.Flow.Model.RadFlowDocument' to 'Telerik.Windows.Documents.Model.RadDocument'

Dimitar
Telerik team
commented on 16 May 2024, 09:15 AM

Hi Patrick,

I have tested the project and it works on my side (see attached). Which version are you using? It seems that the namspaces are wrong on your side. Have you changed them? 

I am looking forward to your reply.

Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 20 May 2024, 05:35 PM

I have not been able to get it to work, but regardless I looked at the code and it isn't quite what I am looking for. You are just importing a docx to display in a radRichTextBox. So let me try again.

Using WPF and MVVM pattern...... I need to get all the XML that makes up content inside of a telerik:RadRichTextBox. I need a two way binding that works for the telerik:RadRichTextBox. Is this a possibility?

Example: User enters text inside of a radRichTextBox, it updates property in viewmodel and on button press, I can view and see all the XML that makes up the user entered content in the RadRichTextBox.
Dimitar
Telerik team
commented on 21 May 2024, 11:31 AM

Hi Patrick, 

This is possible, for example, you can use the DocumentContentChanged event and update the DOCX file. However, the export operation is expensive and this is not recommended. I would suggest creating a Save command to export the document and save it once all changes are made. Please note that the DOCX format is actually an archive that contains a lot of XML files. It cannot be exported to a single XML string. If you need a single string consider using RTF or XAML format providers. 

Let me know if you have additional questions.

Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 21 May 2024, 05:25 PM

Thanks, this is helpful.

Now if I use RTF or XAML format providers, does telerik have converters or resources to get that into OPEN XML format?
Dimitar
Telerik team
commented on 22 May 2024, 04:54 AM

Hi Patrick, 

Yes, you can convert RadDocument to any of the supported formats using the format providers.

Another solution is to use the document processing library (supports the same format without XAML): WordsProcessing - Overview.

Let me know if I can be of further assistance.

No answers yet. Maybe you can help?

Tags
RichTextBox
Asked by
Patrick
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or