So I'm writing a project that mimics Word. The setup is relatively simple:
- I get a
nvarchar(max)
from a database. This is to save the rtf content. - I convert the rtf when the
RichTextBox
is loaded.
using (var stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(rtfString)))
{
TargetRichTextBox.Selection.Load(stream, DataFormats.Rtf);
}
- I do my things and save back to the database.
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:
- I get a
nvarchar(max)
from a database. This is to save the rtf content. - I convert the rtf when the
RichTextBox
is loaded.
using (var stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(rtfString)))
{
TargetRichTextBox.Selection.Load(stream, DataFormats.Rtf);
}
- I do my things and save back to the database.
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.
Share Improve this question edited Feb 4 at 11:16 Victor 9,0035 gold badges18 silver badges36 bronze badges asked Feb 2 at 20:15 KorosevarKorosevar 816 bronze badges1 Answer
Reset to default 1Your 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.
Yes, this is ridiculous. Microsoft developed the RFT format in 1987 and not supported it fully in frameworks that it implemented.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745258372a4619094.html
评论列表(0条)