■ FlowDocument 객체의 전체 범위를 갖는 TextRange 객체를 만들고 해당 객체의 Load 메소드를 사용해 FlowDoocument 객체로 RTF 데이터를 로드하는 방법을 보여준다.
▶ 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 파일 로드하기" 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 |
using Microsoft.Win32; using System.IO; using System.Text; using System.Windows; using System.Windows.Documents; 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); this.textBox.Text = rtf; FlowDocument document = new FlowDocument(); TextRange range = new TextRange(document.ContentStart, document.ContentEnd); using(var stream = new MemoryStream(Encoding.ASCII.GetBytes(rtf))) { range.Load(stream, DataFormats.Rtf); } this.flowDocumentScrollViewer.Document = document; } } #endregion } } |