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

Import Docx to RadRichTextBox

3 Answers 228 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Guillermo
Top achievements
Rank 1
Guillermo asked on 04 Aug 2020, 11:25 AM

Good morning, the binding with RadDocument and Docx drive me crazy. 

When I click in importCommand, fires ImportDocx() correctly and file docx is imported in Rdoc property but in RadRichTextBox don´t display in view. When I clik in keyboard, the aplicacion pause in set.

This is my view model:

private RadDocument rd;
public RadDocument Rdoc
{
get { return rd; }
set {

        rd = value;
              OnPropertyChanged();
       }
}
public void ImportDocx()
{
      RadDocument doc = null;
      DocxFormatProvider provider = new DocxFormatProvider();
      using (Stream inputStream = File.OpenRead(“Any File.docx"))
      {
        doc = provider.Import(inputStream);
      }
      Rdoc=doc;
 }

 

This is my view (Xaml)

<telerik:DocxDataProvider RichTextBox="{Binding ElementName=radRichTextBox}" Docx="{Binding Path=Rdoc, Mode=TwoWay}"/>           
            <telerik:RadRichTextBox x:Name="radRichTextBox"/>.......

3 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 05 Aug 2020, 11:55 AM

Hello Guillermo,

The provided code sample is pretty close to the desired result.

Binding to DOCX document requires that the property must be of type byte[] (or List<byte>). Such a property should look like this:

public byte[] Bytes
{
	get
	{
		return this.bytes;
	}
	set
	{
		this.bytes = value;
		this.OnPropertyChanged();
	}
}

the import method:

public void ImportDocx()
{
	RadDocument doc = new RadDocument();
	DocxFormatProvider provider = new DocxFormatProvider();
	using (Stream inputStream = File.OpenRead("SampleDocument.docx"))
	{
		MemoryStream memoryStream = new MemoryStream();
		inputStream.CopyTo(memoryStream);
		this.Bytes = memoryStream.ToArray();
	}
}

and the XAML:

<telerik:DocxDataProvider RichTextBox="{Binding ElementName=radRichTextBox}" Docx="{Binding Path=Bytes, Mode=TwoWay}"/>
<telerik:RadRichTextBox Grid.Row="0" x:Name="radRichTextBox"/>
<Button Grid.Row="1" Content="Import DOCX" Command="{Binding ClickCommand}"/>

In order to bind a WPF button to a command in the ViewModel, you can implement the ICommand interface. I am attaching a complete example of the described functionality. Please, feel free to modify it in a way closer to your scenario.

Regards,
Martin
Progress Telerik
Dilip
Top achievements
Rank 1
commented on 07 Oct 2021, 07:00 PM

Hello Martin, Ive tried the above steps,

1. Converted my docx to byte[]

2. used docxdataprovider and binded the byte[]

but i can't able to see my docx in radrichtextbox

Kindly help me on this, Thanks in advance!!!

Regards, 

Dilip Kumar

Tanya
Telerik team
commented on 11 Oct 2021, 03:09 PM

Hi Dilip,

The steps you mentioned should be enough to show the document in RadRichTextBox. Can you also confirm that the data context and, respectively, the property the DocxDataProvider is bound to, implement INotifyPropertyChanged? Also, can you share the code you are using so we can locally test it?

0
Guillermo
Top achievements
Rank 1
answered on 05 Aug 2020, 01:15 PM

Thanks, Martin.

I also came to that conclusion. But somewhere does it say that binding should be done like this?

I only found simple examples!!!

Thanks again

0
Accepted
Martin
Telerik team
answered on 10 Aug 2020, 07:17 AM

Hello Guillermo,

This specific exception is mentioned as a note in Data Providers help article ("An exception to this rule is the Docx property of DocxDataProvider which is a byte array."). I understand that it is nor clear enough and I already wrote down in our backlog to provide more information and examples in our documentation about this data binding.

Regards,
Martin
Progress Telerik

Tags
RichTextEditor
Asked by
Guillermo
Top achievements
Rank 1
Answers by
Martin
Telerik team
Guillermo
Top achievements
Rank 1
Share this question
or