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