Creating a RadDocument from XAML string

2 Answers 60 Views
RichTextBox
Patrick
Top achievements
Rank 1
Iron
Iron
Iron
Patrick asked on 14 Jun 2024, 05:44 PM
I have a XAMLDataProvider that is binding a string property to a RadRichTextBox.

I want to be able to bind the content of the RadRichTextBox to a string (which I have done) and then once a save button is pressed, I need to run some code similar to this.
Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.DocxFormatProvider provider = new Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.DocxFormatProvider();
RadDocument document = new RadDocument(string myXAMLString);
byte[] output = provider.Export(document);

Essentially I need to be able to bind a property from the ViewModel to RadRichTextBox..... which I am doing with XAML data provider.

Then after a save button is clicked, I take the property which is a string and I need to create a RadDocument from the string that represents that and export that document to a byte[].

Is there an easier and better way to be doing this?

Thanks!

2 Answers, 1 is accepted

Sort by
1
Stenly
Telerik team
answered on 19 Jun 2024, 03:29 PM

Hello Patrick,

The XamlFormatProvider class does not provide such an option via its export functionality. You could use its Export method, which returns a string value from a passed RadDocument instance. You could retrieve the RadDocument of the RadRichTextBox control when clicking on the save button and pass it to the Export method. Then, you could convert the returned string value to a byte array as shown in the following StackOverflow article:

Converting string to byte array in C# - Stack Overflow

Could you give this suggestion a try?

Regards,
Stenly
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 20 Jun 2024, 05:01 PM | edited

I ended up using XamlFormatProvider to import my string into a RadDocument. I then took the DocxFormatProvider and exported myRadDocument into a byte [].

This allowed me to achieve my desired results.

1
Matt
Top achievements
Rank 1
Iron
answered on 20 Jun 2024, 10:41 AM
You can achieve this by using the built-in Load method of RadDocument class that accepts a string argument. Here's the simplified code:
byte[] output;
using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(myXAMLString)))
{
  RadDocument document = RadDocument.Load(memoryStream);
  // rest of your export logic using provider.Export(document)
}
This approach avoids creating a separate RadRichTextBox instance and leverages the RadDocument's loading capabilities directly from your XAML string.
Tags
RichTextBox
Asked by
Patrick
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Stenly
Telerik team
Matt
Top achievements
Rank 1
Iron
Share this question
or