프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / Controls / SearchPanel.xaml.cs @ 71f5a3be

이력 | 보기 | 이력해설 | 다운로드 (12.6 KB)

1 e0cfc73c ljiyeon
using KCOM.Common;
2
using MarkupToPDF.Common;
3 787a4489 KangIngu
using System;
4
using System.Collections.Generic;
5 136851cb djkim
using System.ComponentModel;
6 0c997b99 ljiyeon
using System.Diagnostics;
7 787a4489 KangIngu
using System.Linq;
8
using System.Text;
9 136851cb djkim
using System.Threading;
10 787a4489 KangIngu
using System.Windows;
11
using System.Windows.Controls;
12
using System.Windows.Data;
13
using System.Windows.Documents;
14
using System.Windows.Input;
15
using System.Windows.Media;
16
using System.Windows.Media.Imaging;
17
using System.Windows.Navigation;
18
using System.Windows.Shapes;
19 136851cb djkim
using Telerik.Windows.Controls;
20 787a4489 KangIngu
21
namespace KCOM.Controls
22
{
23 992a98b4 KangIngu
    public class SearchText
24
    {
25
        public Bytescout.PDFExtractor.ISearchResult searchResult { get; set; }
26
        public int PageNo { get; set; }
27
    }
28 787a4489 KangIngu
    /// <summary>
29
    /// Interaction logic for SearchPanel.xaml
30
    /// </summary>
31
    public partial class SearchPanel : UserControl
32
    {
33 992a98b4 KangIngu
        //List<HoneyheadTEXT> txtSet = new List<HoneyheadTEXT>();
34
        public System.Collections.ObjectModel.ObservableCollection<SearchText> SearchSet { get; set; }
35
        string RegistrationKey = "193B-FD48-42F4-68E6-2D35-4C94-D07";
36
        string RegistrationName = "license@doftech.co.kr";
37
        Bytescout.PDFExtractor.TextExtractor extractor;
38 79f3f21a djkim
        Bytescout.PDFExtractor.OCRAnalyzer ocrAnalyzer;
39 136851cb djkim
        private BackgroundWorker worker = new BackgroundWorker();
40 787a4489 KangIngu
        public SearchPanel()
41
        {
42 e0cfc73c ljiyeon
            App.splashString(ISplashMessage.SEARCHPANEL);
43 79f3f21a djkim
            InitializeComponent();
44
            LibInit();
45
            //this.Loaded += SearchPanel_Loaded;
46
            btnSearch.Click += BtnSearch_Click;
47
            btnSearchPDFDownload.Click += BtnSearchPDFDownload_Click;
48
            SearchSet = new System.Collections.ObjectModel.ObservableCollection<SearchText>();
49 136851cb djkim
50 79f3f21a djkim
            tbSearch.KeyDown += tbSearch_KeyDown;
51
            this.DataContext = this;
52
53 136851cb djkim
            worker.DoWork += Worker_DoWork;
54
            worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
55 79f3f21a djkim
56
            /*
57
            if (Common.ViewerDataModel.Instance.searchPDF ==null)
58
            {
59
60
            }
61
            else
62
            {
63
                border_Overlap.Visibility = Visibility.Collapsed;
64
                tbSearch.Focus();
65
                
66
            }
67
            */
68
69
            if (Common.ViewerDataModel.Instance.searchPDF != null)
70
            {
71
                border_Overlap.Visibility = Visibility.Collapsed;
72
                tbSearch.Focus();
73
            }
74 787a4489 KangIngu
        }
75
76 136851cb djkim
        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
77
        {
78
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
79 29010418 ljiyeon
            {
80 136851cb djkim
                this.searchIndicator.IsBusy = false;
81
                this.DataContext = this;
82
                tbSearch.Text = "";
83
                tbSearch.Focus();
84
                DialogMessage_Alert("검색이 완료되었습니다.");
85
            }));
86
87
        }
88
        //private System.Collections.ObjectModel.ObservableCollection<SearchText> searchTexts = null;
89
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
90
        {
91
            string searchString = string.Empty;
92
            bool pageoption = false;
93
94
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
95
            {
96
                SearchSet.Clear();
97
                searchString = tbSearch.Text;
98
                pageoption = chkCurrentPageOnly.IsChecked.Value;
99
            }));
100 29010418 ljiyeon
101 136851cb djkim
            extractor.LoadDocumentFromFile(Common.ViewerDataModel.Instance.searchPDF.FullName);
102
            ocrAnalyzer.LoadDocumentFromFile(Common.ViewerDataModel.Instance.searchPDF.FullName);
103
104
            if (pageoption)
105
            {
106
                var currentPage = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.CurrentPage.PageNumber - 1;
107
108
                Bytescout.PDFExtractor.OCRAnalysisResults analysisResults = ocrAnalyzer.AnalyzeByOCRConfidence(currentPage);
109
                ocrAnalyzer.ApplyResults(analysisResults, extractor);
110
111
                if (extractor.Find(currentPage, searchString, false))
112
                    do
113
                    {
114
                        Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
115
                        {
116
                            SearchSet.Add(new SearchText { PageNo = currentPage + 1, searchResult = extractor.FoundText });
117 29010418 ljiyeon
                        }));
118 136851cb djkim
                    }
119
                    while (extractor.FindNext());
120
            }
121
            else
122
            {
123
                int pageCount = extractor.GetPageCount();
124
                for (int i = 0; i < pageCount; i++)
125
                {
126
                    Bytescout.PDFExtractor.OCRAnalysisResults analysisResults = ocrAnalyzer.AnalyzeByOCRConfidence(i);
127
                    ocrAnalyzer.ApplyResults(analysisResults, extractor);
128
                    if (extractor.Find(i, searchString, false))
129
                        do
130
                        {
131
                            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
132
                            {
133
                                SearchSet.Add(new SearchText { PageNo = i + 1, searchResult = extractor.FoundText });
134 29010418 ljiyeon
                            }));
135 136851cb djkim
                        }
136
                        while (extractor.FindNext());
137
                }
138 29010418 ljiyeon
            }
139 136851cb djkim
        }
140
141 992a98b4 KangIngu
        private void tbSearch_KeyDown(object sender, KeyEventArgs e)
142 787a4489 KangIngu
        {
143 992a98b4 KangIngu
            if (e.Key == Key.Enter)
144
            {
145
                SearchMethod();
146
            }
147 787a4489 KangIngu
        }
148 79f3f21a djkim
        private void LibInit()
149
        {
150 136851cb djkim
            try
151
            {
152
                extractor = new Bytescout.PDFExtractor.TextExtractor(RegistrationName, RegistrationKey);
153
                ocrAnalyzer = new Bytescout.PDFExtractor.OCRAnalyzer(RegistrationName, RegistrationKey);
154
                extractor.OCRMode = Bytescout.PDFExtractor.OCRMode.Auto;
155
                extractor.OCRLanguage = "eng";
156 90a0bfa9 djkim
                extractor.OCRLanguageDataFolder = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tessdata");
157
                    
158 136851cb djkim
                extractor.OCRResolution = 300;
159
160
                ocrAnalyzer.OCRLanguage = "eng";
161 90a0bfa9 djkim
                ocrAnalyzer.OCRLanguageDataFolder = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tessdata");
162 136851cb djkim
163
            }
164
            catch (Exception ex)
165
            {
166
                Logger.sendResLog("LibInit", ex.Message, 0);
167
            }
168 787a4489 KangIngu
169 79f3f21a djkim
        }
170 787a4489 KangIngu
        private void SearchPanel_Loaded(object sender, RoutedEventArgs e)
171
        {
172 0c997b99 ljiyeon
            //측정
173
            //Stopwatch sw = new Stopwatch();
174
            //sw.Start();
175
            InitializeComponent();
176
            //System.Diagnostics.Debug.WriteLine("SearchPanel() : " + sw.ElapsedMilliseconds.ToString() + "ms");
177 136851cb djkim
178 0c997b99 ljiyeon
            btnSearch.Click += BtnSearch_Click;
179
            btnSearchPDFDownload.Click += BtnSearchPDFDownload_Click;
180
            SearchSet = new System.Collections.ObjectModel.ObservableCollection<SearchText>();
181
            extractor = new Bytescout.PDFExtractor.TextExtractor(RegistrationName, RegistrationKey);
182
            tbSearch.KeyDown += tbSearch_KeyDown;
183
            this.DataContext = this;
184
185
            if (Common.ViewerDataModel.Instance.searchPDF != null)
186
            {
187
                border_Overlap.Visibility = Visibility.Collapsed;
188
                tbSearch.Focus();
189
            }
190 992a98b4 KangIngu
        }
191
192
        private void BtnSearchPDFDownload_Click(object sender, RoutedEventArgs e)
193
        {
194 136851cb djkim
            try
195 992a98b4 KangIngu
            {
196 136851cb djkim
                using (System.Net.WebClient client = new System.Net.WebClient())
197
                {
198
                    string instnaceFile = System.IO.Path.GetTempFileName().Replace(".pdf", ".tmp");
199
                    client.DownloadFileCompleted += Client_DownloadFileCompleted;
200
                    client.DownloadProgressChanged += Client_DownloadProgressChanged;
201
                    client.DownloadFileAsync(new Uri(Common.ViewerDataModel.Instance.SystemMain.dzMainMenu._DocInfo.ORIGINAL_FILE), instnaceFile, instnaceFile);
202
                }
203
            }
204
            catch (Exception ex)
205
            {
206
                Logger.sendResLog("BtnSearchPDFDownload_Click", ex.Message, 0);
207 992a98b4 KangIngu
            }
208
        }
209
210
        private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
211
        {
212
            border_Overlap.Visibility = Visibility.Collapsed;
213
            Common.ViewerDataModel.Instance.searchPDF = new System.IO.FileInfo(e.UserState.ToString());
214
        }
215
216
        private void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
217
        {
218
            tlDonwloadState.Text = "문서를 다운로드 중입니다 : " + e.ProgressPercentage;
219 136851cb djkim
220 992a98b4 KangIngu
        }
221
222
        private void BtnSearch_Click(object sender, RoutedEventArgs e)
223
        {
224
            SearchMethod();
225
        }
226 136851cb djkim
227 992a98b4 KangIngu
        private void SearchMethod()
228
        {
229 136851cb djkim
            try
230 992a98b4 KangIngu
            {
231 136851cb djkim
                if (!worker.IsBusy)
232 992a98b4 KangIngu
                {
233 136851cb djkim
                    SearchSet.Clear();
234
                    this.searchIndicator.IsBusy = true;
235
                    worker.RunWorkerAsync();
236 992a98b4 KangIngu
                }
237
            }
238 136851cb djkim
            catch (Exception ex)
239
            {
240
                Logger.sendResLog("SearchMethod", ex.Message, 0);
241
            }
242 992a98b4 KangIngu
243
        }
244 136851cb djkim
        private void DialogMessage_Alert(string Msg)
245
        {
246
            RadWindow CheckPop = new RadWindow();
247
            Alert check = new Alert(Msg);
248 992a98b4 KangIngu
249 136851cb djkim
            CheckPop = new RadWindow
250
            {
251
                Owner = Application.Current.MainWindow,
252
                MinWidth = 400,
253
                MinHeight = 100,
254
                //Closed = (obj, args) => this.MarkupNamePromptClose(MarkupData, args),
255
                Header = "Alert",
256
                Content = check,
257
                ResizeMode = System.Windows.ResizeMode.NoResize,
258
                WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen,
259
                IsTopmost = true,
260
            };
261
            StyleManager.SetTheme(CheckPop, new Office2013Theme());
262
            CheckPop.ShowDialog();
263
        }
264 992a98b4 KangIngu
        private void chkSelectionChangeEvent(object sender, RoutedEventArgs e)
265
        {
266 136851cb djkim
            try
267 992a98b4 KangIngu
            {
268 136851cb djkim
                if (Common.ViewerDataModel.Instance.searchPDF != null)
269 992a98b4 KangIngu
                {
270 136851cb djkim
                    if (sender is RadioButton)
271 992a98b4 KangIngu
                    {
272 136851cb djkim
                        switch ((sender as RadioButton).Tag.ToString())
273
                        {
274
                            case "0":
275
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.None;
276
                                break;
277
                            case "1":
278
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.ExactMatch;
279
                                break;
280
                            case "2":
281
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.SmartMatch;
282
                                break;
283
                            default:
284
                                break;
285
                        }
286 992a98b4 KangIngu
                    }
287
                }
288
            }
289 136851cb djkim
            catch (Exception ex)
290
            {
291
                Logger.sendResLog("chkSelectionChangeEvent", ex.Message, 0);
292
            }
293
294 992a98b4 KangIngu
        }
295
296
        private void lstSearchWord_SelectionChanged(object sender, SelectionChangedEventArgs e)
297
        {
298 136851cb djkim
            try
299 992a98b4 KangIngu
            {
300 136851cb djkim
                if (e.AddedItems.Count > 0)
301
                {
302
                    SearchText item = (e.AddedItems[0] as SearchText);
303 992a98b4 KangIngu
304 136851cb djkim
                    Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.GotoPage(item.PageNo);
305
                    var width = Convert.ToDouble(Common.ViewerDataModel.Instance.Document_Info[item.PageNo - 1].PAGE_WIDTH);
306
                    var height = Convert.ToDouble(Common.ViewerDataModel.Instance.Document_Info[item.PageNo - 1].PAGE_HEIGHT);
307
                    var widthScale = width / Convert.ToDouble(extractor.GetPageRect_Width(item.PageNo - 1));
308
                    var heightScale = height / Convert.ToDouble(extractor.GetPageRect_Height(item.PageNo - 1));
309 992a98b4 KangIngu
310 136851cb djkim
                    Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.zoomAndPanControl.ZoomTo(new Rect
311
                    {
312
                        X = item.searchResult.Left * widthScale,
313
                        Y = item.searchResult.Top * heightScale,
314
                        Width = item.searchResult.Width * widthScale,
315
                        Height = item.searchResult.Height * heightScale
316
                    });
317
318
                }
319 992a98b4 KangIngu
            }
320 136851cb djkim
            catch (Exception ex)
321
            {
322
                Logger.sendResLog("lstSearchWord_SelectionChanged", ex.Message, 0);
323
            }
324
325 787a4489 KangIngu
        }
326
    }
327
}
클립보드 이미지 추가 (최대 크기: 500 MB)