So I'm writing a project that mimics Word. The setup is relatively simple:
nvarchar(max)
from a database. This is to save the rtf content.RichTextBox
is loaded.using (var stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(rtfString)))
{
TargetRichTextBox.Selection.Load(stream, DataFormats.Rtf);
}
public string ConvertRichTextBoxToRtf(RichTextBox richTextBox)
{
if (richTextBox == null) throw new ArgumentNullException(nameof(richTextBox));
using (var memoryStream = new System.IO.MemoryStream())
{
var range = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
range.Save(memoryStream, DataFormats.Rtf);
return System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
}
}
The problem:
All the properties that I set with TextRange.ApplyPropertyValue(Inline.propName, value)
work during runtime. But don't seem to be saved to the rtf string.
While all the TextRange.ApplyPropertyValue(TextElement.propName, value)
properties behave as expected.
So should I change to XML or XAML to store my string?
Is my MemoryStream
in the wrong format?
Or am I using the inline properties wrong?
Here is an example of code for an 'inline' property:
public void SetSelectionToSubscript(TextRange range)
{
if (range.IsEmpty) return; // Don't apply formatting to an empty selection
object currentBaseline = range.GetPropertyValue(Inline.BaselineAlignmentProperty);
if (currentBaseline is BaselineAlignment alignment && alignment == BaselineAlignment.Subscript)
{
// Reset to normal text
range.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Baseline);
range.ApplyPropertyValue(TextElement.FontSizeProperty, MyFondSize); // Reset font size
}
else
{
// Apply subscript formatting
range.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Subscript);
range.ApplyPropertyValue(TextElement.FontSizeProperty, MyFondSize - 2); // Adjust font size for subscript effect
}
}
Thanks in advance.
So I'm writing a project that mimics Word. The setup is relatively simple:
nvarchar(max)
from a database. This is to save the rtf content.RichTextBox
is loaded.using (var stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(rtfString)))
{
TargetRichTextBox.Selection.Load(stream, DataFormats.Rtf);
}
public string ConvertRichTextBoxToRtf(RichTextBox richTextBox)
{
if (richTextBox == null) throw new ArgumentNullException(nameof(richTextBox));
using (var memoryStream = new System.IO.MemoryStream())
{
var range = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
range.Save(memoryStream, DataFormats.Rtf);
return System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
}
}
The problem:
All the properties that I set with TextRange.ApplyPropertyValue(Inline.propName, value)
work during runtime. But don't seem to be saved to the rtf string.
While all the TextRange.ApplyPropertyValue(TextElement.propName, value)
properties behave as expected.
So should I change to XML or XAML to store my string?
Is my MemoryStream
in the wrong format?
Or am I using the inline properties wrong?
Here is an example of code for an 'inline' property:
public void SetSelectionToSubscript(TextRange range)
{
if (range.IsEmpty) return; // Don't apply formatting to an empty selection
object currentBaseline = range.GetPropertyValue(Inline.BaselineAlignmentProperty);
if (currentBaseline is BaselineAlignment alignment && alignment == BaselineAlignment.Subscript)
{
// Reset to normal text
range.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Baseline);
range.ApplyPropertyValue(TextElement.FontSizeProperty, MyFondSize); // Reset font size
}
else
{
// Apply subscript formatting
range.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Subscript);
range.ApplyPropertyValue(TextElement.FontSizeProperty, MyFondSize - 2); // Adjust font size for subscript effect
}
}
Thanks in advance.
Your code is correct. Unfortunately, the WPF RichTextBox
control doesn't support many of the formats described in the RTF format specification when saving the document using DataFormats.Rtf
format.
As workaround you can use DataFormats.Xaml
or DataFormats.XamlPackage
formats when saving/loading documents.
Another solution is using some third party library like Telerik UI for WPF RichTextBox.