c# - Can I save a TextRange inline property to RTF in WPF? - Stack Overflow

So I'm writing a project that mimics Word.The setup is relatively simple:I get a nvarchar(max) fr

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.


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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信