■ FlowDocument 클래스를 사용해 RTF 파일을 XAML로 변환하는 방법을 보여준다.
▶ MainWindow.xaml
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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="FlowDocument 클래스 : RTF 파일을 XAML로 변환하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="10" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="10" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="10" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="10" /> <RowDefinition Height="*" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> <RowDefinition Height="10" /> </Grid.RowDefinitions> <TextBox Name="textBox" Grid.Row="1" Grid.Column="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" IsReadOnly="True" /> <Border Grid.Row="1" Grid.Column="3" BorderThickness="1" BorderBrush="Gray"> <FlowDocumentScrollViewer Name="flowDocumentScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" /> </Border> <Button Name="openFileButton" Grid.Row="3" Grid.Column="3" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="100" Height="30" Content="파일 열기" /> </Grid> </Window> |
▶ MainWindow.xaml.cs
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
using Microsoft.Win32; using System.IO; using System.Text; using System.Windows; using System.Windows.Documents; using System.Windows.Markup; using System.Xml; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 파일 열기 대화 상자 /// </summary> private OpenFileDialog openFileDialog; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.openFileDialog = new OpenFileDialog(); this.openFileDialog.Filter = "RTF 파일 (*.rtf)|*.rtf"; this.openFileDialog.FilterIndex = 1; this.openFileDialog.DefaultExt = ".rtf"; this.openFileButton.Click += openFileButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 파일 열기 버튼 클릭시 처리하기 - openFileButton_Click(sender, e) /// <summary> /// 파일 열기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void openFileButton_Click(object sender, RoutedEventArgs e) { bool result = this.openFileDialog.ShowDialog(this).GetValueOrDefault(); if(result) { string rtf = File.ReadAllText(this.openFileDialog.FileName, Encoding.UTF8); string xaml = GetXAML(rtf); FlowDocument document = GetDocument(xaml); this.flowDocumentScrollViewer.Document = document; this.textBox.Text = xaml; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region XAML 구하기 - GetXAML(rtf) /// <summary> /// XAML 구하기 /// </summary> /// <param name="rtf">RTF</param> /// <returns>XAML</returns> private string GetXAML(string rtf) { FlowDocument document = new FlowDocument(); TextRange range = new TextRange(document.ContentStart, document.ContentEnd); using(MemoryStream sourceStream = new MemoryStream(Encoding.UTF8.GetBytes(rtf))) { range.Load(sourceStream, DataFormats.Rtf); using(MemoryStream targetStream = new MemoryStream()) { range.Save(targetStream, DataFormats.Xaml); targetStream.Position = 0; using(StreamReader reader = new StreamReader(targetStream)) { string xaml = reader.ReadToEnd(); return xaml; } } } } #endregion #region 문서 구하기 - GetDocument(xaml) /// <summary> /// 문서 구하기 /// </summary> /// <param name="xaml">XAML</param> /// <returns>문서</returns> private FlowDocument GetDocument(string xaml) { StringReader stringReader = new StringReader(xaml); XmlReader xmlReader = XmlReader.Create(stringReader); Section section = XamlReader.Load(xmlReader) as Section; FlowDocument document = new FlowDocument(); while(section.Blocks.Count > 0) { Block block = section.Blocks.FirstBlock; section.Blocks.Remove(block); document.Blocks.Add(block); } return document; } #endregion } } |
※ 이미지가 포함된 RTF 파일 로드시 이미지는 누락된다.