■ DocumentVisitorBase 클래스를 사용해 커스텀 문서 방문자를 만드는 방법을 보여준다.
▶ CustomVisitor.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 |
using System.Text; using DevExpress.XtraRichEdit.API.Native; namespace TestProject { /// <summary> /// 커스텀 방문자 /// </summary> public class CustomVisitor : DocumentVisitorBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 문자열 빌더 /// </summary> private readonly StringBuilder stringBuilder; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Private #region 텍스트 - Text /// <summary> /// 텍스트 /// </summary> public string Text { get { return this.stringBuilder.ToString(); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - CustomVisitor() /// <summary> /// 생성자 /// </summary> public CustomVisitor() { this.stringBuilder = new StringBuilder(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 방문하기 - Visit(documentText) /// <summary> /// 방문하기 /// </summary> /// <param name="documentText">문서 텍스트</param> public override void Visit(DocumentText documentText) { string prefix = (documentText.TextProperties.FontBold) ? "**" : ""; this.stringBuilder.Append(prefix); this.stringBuilder.Append(documentText.Text); this.stringBuilder.Append(prefix); } #endregion #region 방문하기 - Visit(documentParagraphEnd) /// <summary> /// 방문하기 /// </summary> /// <param name="documentParagraphEnd">문서 문단 끝</param> public override void Visit(DocumentParagraphEnd documentParagraphEnd) { this.stringBuilder.AppendLine(); } #endregion } } |
▶ MainForm.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 |
using System; using System.Drawing; using DevExpress.XtraEditors; using DevExpress.XtraRichEdit; using DevExpress.XtraRichEdit.API.Native; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 줄 문자열 /// </summary> private const string LINE_STRING = "------------------------------------------------------------------------------------------------------------------------"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.richEditControl.ActiveViewType = RichEditViewType.Simple; this.richEditControl.Options.HorizontalScrollbar.Visibility = RichEditScrollbarVisibility.Hidden; this.richEditControl.Options.VerticalScrollbar.Visibility = RichEditScrollbarVisibility.Auto; this.richEditControl.Document.DefaultCharacterProperties.FontName = "나눔고딕코딩"; this.richEditControl.Document.DefaultCharacterProperties.FontSize = 12f; this.richEditControl.Document.DefaultCharacterProperties.ForeColor = Color.Black; this.richEditControl.Document.ParagraphStyles[0].FontName = "나눔고딕코딩"; this.richEditControl.Document.ParagraphStyles[0].FontSize = 12f; this.richEditControl.Document.ParagraphStyles[0].ForeColor = Color.Black; this.richEditControl.LoadDocument("sample.rtf"); this.parseButton.Click += parseButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 분석 버튼 클릭시 처리하기 - parseButton_Click(sender, e) /// <summary> /// 분석 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void parseButton_Click(object sender, EventArgs e) { CustomVisitor visitor = new CustomVisitor(); DocumentIterator iterator = new DocumentIterator(this.richEditControl.Document, true); while(iterator.MoveNext()) { iterator.Current.Accept(visitor); } this.memoEdit.Text = visitor.Text; } #endregion } } |