SOS: Using WPF RichTextBox to extract RTF, inexplicably dropping Underline style information

Source: Internet
Author: User

The main code is written as follows:

1richTextBox1. SelectAll ();
2richTextBox1. Selection. ApplyPropertyValue (Inline. TextDecorationsProperty, TextDecorations. Underline );
3System. Drawing. FontStyle style = richTextBox1.DrawingFontStyle ();
4MessageBox. Show (style. ToString (); // <-- result: Underline
5
6 string rtf = richTextBox1.RTF ();
7richTextBox1. LoadFromRTF (rtf );
8richTextBox1. SelectAll ();
9 style = richTextBox1.DrawingFontStyle ();
10MessageBox. Show (style. ToString (); // <-- result: Regular
11

Running result:
First display: Underline
Second display: Regular
The font in RichTextBox is always underlined.In my personal analysis, the Underline style is lost when RichTextBox extracts the RTF string. How can I modify it?

Extract the RTF string:

RTF Extension
Public static string RTF (this RichTextBox richTextBox)
{
String rtf = string. Empty;
TextRange textRange = new TextRange (richTextBox. Document. ContentStart, richTextBox. Document. ContentEnd );
Using (MemoryStream MS = new MemoryStream ())
{
TextRange. Save (MS, System. Windows. DataFormats. Rtf );
Ms. Seek (0, SeekOrigin. Begin );
StreamReader sr = new StreamReader (MS );
Rtf = sr. ReadToEnd ();
}

Return rtf;
}

Read RTF:

Load RTF
Public static void LoadFromRTF (this RichTextBox richTextBox, string rtf)
{
If (string. IsNullOrEmpty (rtf ))
{
Throw new ArgumentNullException ();
}
TextRange textRange = new TextRange (richTextBox. Document. ContentStart, richTextBox. Document. ContentEnd );
Using (MemoryStream MS = new MemoryStream ())
{
Using (StreamWriter sw = new StreamWriter (MS ))
{
Sw. Write (rtf );
Sw. Flush ();
Ms. Seek (0, SeekOrigin. Begin );
TextRange. Load (MS, DataFormats. Rtf );
}
}
}

Parsing FontStyle:

Get FontStyle
Public static System. Drawing. FontStyle DrawingFontStyle (this RichTextBox richTextBox)
{
System. Drawing. FontStyle style = System. Drawing. FontStyle. Regular;
Object decoration = richTextBox. Selection. GetPropertyValue (Inline. TextDecorationsProperty );
If (System. Windows. DependencyProperty. UnsetValue! = Decoration)
{
System. Windows. TextDecorationCollection textDecoration = decoration as System. Windows. TextDecorationCollection;
Style = style | textDecoration. ToFontStyle ();
}

Return style;
}

Public static System. Drawing. FontStyle ToFontStyle (this System. Windows. TextDecorationCollection textDecorationCollection)
{
If (textDecorationCollection = System. Windows. TextDecorations. Underline)
Return System. Drawing. FontStyle. Underline;
Else if (textDecorationCollection = System. Windows. TextDecorations. Strikethrough)
Return System. Drawing. FontStyle. Strikeout;

If (textDecorationCollection. Count> 0)
{
System. Drawing. FontStyle style = System. Drawing. FontStyle. Regular;
Foreach (var textDecoration in textDecorationCollection)
{
Switch (textDecoration. Location)
{
Case TextDecorationLocation. Underline:
Style | = System. Drawing. FontStyle. Underline;
Break;
Case TextDecorationLocation. Strikethrough:
Style | = System. Drawing. FontStyle. Strikeout;
Break;
Default:
Break;
}
}

Return style;
}

Return System. Drawing. FontStyle. Regular;
}

 

Complete Source:/Files/andres06/RTFConvertor.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.