■ RichTextBox 클래스에서 특정 포맷 문자열을 구하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; #region RTF 구하기 - GetRTF(richTextBox) /// <summary> /// RTF 구하기 /// </summary> /// <param name="richTextBox">RichTextBox</param> /// <returns>RTF</returns> public string GetRTF(RichTextBox richTextBox) { return GetString(richTextBox, DataFormats.Rtf); } #endregion #region XAML 구하기 - GetXAML(richTextBox) /// <summary> /// XAML 구하기 /// </summary> /// <param name="richTextBox">RichTextBox</param> /// <returns>XAML</returns> public string GetXAML(RichTextBox richTextBox) { return GetString(richTextBox, DataFormats.Xaml); } #endregion #region 텍스트 구하기 - GetText(richTextBox) /// <summary> /// 텍스트 구하기 /// </summary> /// <param name="richTextBox">RichTextBox</param> /// <returns>텍스트</returns> public string GetText(RichTextBox richTextBox) { return GetString(richTextBox, DataFormats.Text); } #endregion #region 문자열 구하기 - GetString(richTextBox, dataFormat) /// <summary> /// 문자열 구하기 /// </summary> /// <param name="richTextBox">RichTextBox</param> /// <param name="dataFormat">데이터 포맷</param> /// <returns>문자열</returns> private string GetString(RichTextBox richTextBox, string dataFormat) { FlowDocument flowDocument = richTextBox.Document; string result = string.Empty; using(MemoryStream memoryStream = new MemoryStream()) { TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd); textRange.Save(memoryStream, dataFormat); memoryStream.Seek(0, SeekOrigin.Begin); using(StreamReader streamReader = new StreamReader(memoryStream)) { result = streamReader.ReadToEnd(); } } return result; } #endregion |