프로젝트

일반

사용자정보

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

markus / KCOM / Controls / SearchPanel.xaml.cs @ 223975f7

이력 | 보기 | 이력해설 | 다운로드 (12.7 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 223975f7 taeseongkim
            //ocrAnalyzer.LoadDocumentFromFile(Common.ViewerDataModel.Instance.searchPDF.FullName);
103 136851cb djkim
104
            if (pageoption)
105
            {
106
                var currentPage = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.CurrentPage.PageNumber - 1;
107
108 223975f7 taeseongkim
                //Bytescout.PDFExtractor.OCRAnalysisResults analysisResults = ocrAnalyzer.AnalyzeByOCRConfidence(currentPage);
109
                //ocrAnalyzer.ApplyResults(analysisResults, extractor);
110 136851cb djkim
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 223975f7 taeseongkim
                    //ocrAnalyzer.ApplyResults(analysisResults, extractor);
128 136851cb djkim
                    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 223975f7 taeseongkim
                    string downloadurl = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.GetOriginalPDFURL();
202
                    client.DownloadFileAsync(new Uri(downloadurl), instnaceFile, instnaceFile);
203 136851cb djkim
                }
204
            }
205
            catch (Exception ex)
206
            {
207
                Logger.sendResLog("BtnSearchPDFDownload_Click", ex.Message, 0);
208 992a98b4 KangIngu
            }
209
        }
210
211
        private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
212
        {
213
            border_Overlap.Visibility = Visibility.Collapsed;
214
            Common.ViewerDataModel.Instance.searchPDF = new System.IO.FileInfo(e.UserState.ToString());
215
        }
216
217
        private void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
218
        {
219
            tlDonwloadState.Text = "문서를 다운로드 중입니다 : " + e.ProgressPercentage;
220 136851cb djkim
221 992a98b4 KangIngu
        }
222
223
        private void BtnSearch_Click(object sender, RoutedEventArgs e)
224
        {
225
            SearchMethod();
226
        }
227 136851cb djkim
228 992a98b4 KangIngu
        private void SearchMethod()
229
        {
230 136851cb djkim
            try
231 992a98b4 KangIngu
            {
232 136851cb djkim
                if (!worker.IsBusy)
233 992a98b4 KangIngu
                {
234 136851cb djkim
                    SearchSet.Clear();
235
                    this.searchIndicator.IsBusy = true;
236
                    worker.RunWorkerAsync();
237 992a98b4 KangIngu
                }
238
            }
239 136851cb djkim
            catch (Exception ex)
240
            {
241
                Logger.sendResLog("SearchMethod", ex.Message, 0);
242
            }
243 992a98b4 KangIngu
244
        }
245 136851cb djkim
        private void DialogMessage_Alert(string Msg)
246
        {
247
            RadWindow CheckPop = new RadWindow();
248
            Alert check = new Alert(Msg);
249 992a98b4 KangIngu
250 136851cb djkim
            CheckPop = new RadWindow
251
            {
252
                Owner = Application.Current.MainWindow,
253
                MinWidth = 400,
254
                MinHeight = 100,
255
                //Closed = (obj, args) => this.MarkupNamePromptClose(MarkupData, args),
256
                Header = "Alert",
257
                Content = check,
258
                ResizeMode = System.Windows.ResizeMode.NoResize,
259
                WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen,
260
                IsTopmost = true,
261
            };
262
            StyleManager.SetTheme(CheckPop, new Office2013Theme());
263
            CheckPop.ShowDialog();
264
        }
265 992a98b4 KangIngu
        private void chkSelectionChangeEvent(object sender, RoutedEventArgs e)
266
        {
267 136851cb djkim
            try
268 992a98b4 KangIngu
            {
269 136851cb djkim
                if (Common.ViewerDataModel.Instance.searchPDF != null)
270 992a98b4 KangIngu
                {
271 136851cb djkim
                    if (sender is RadioButton)
272 992a98b4 KangIngu
                    {
273 136851cb djkim
                        switch ((sender as RadioButton).Tag.ToString())
274
                        {
275
                            case "0":
276
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.None;
277
                                break;
278
                            case "1":
279
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.ExactMatch;
280
                                break;
281
                            case "2":
282
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.SmartMatch;
283
                                break;
284
                            default:
285
                                break;
286
                        }
287 992a98b4 KangIngu
                    }
288
                }
289
            }
290 136851cb djkim
            catch (Exception ex)
291
            {
292
                Logger.sendResLog("chkSelectionChangeEvent", ex.Message, 0);
293
            }
294
295 992a98b4 KangIngu
        }
296
297
        private void lstSearchWord_SelectionChanged(object sender, SelectionChangedEventArgs e)
298
        {
299 136851cb djkim
            try
300 992a98b4 KangIngu
            {
301 136851cb djkim
                if (e.AddedItems.Count > 0)
302
                {
303
                    SearchText item = (e.AddedItems[0] as SearchText);
304 992a98b4 KangIngu
305 136851cb djkim
                    Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.GotoPage(item.PageNo);
306
                    var width = Convert.ToDouble(Common.ViewerDataModel.Instance.Document_Info[item.PageNo - 1].PAGE_WIDTH);
307
                    var height = Convert.ToDouble(Common.ViewerDataModel.Instance.Document_Info[item.PageNo - 1].PAGE_HEIGHT);
308
                    var widthScale = width / Convert.ToDouble(extractor.GetPageRect_Width(item.PageNo - 1));
309
                    var heightScale = height / Convert.ToDouble(extractor.GetPageRect_Height(item.PageNo - 1));
310 992a98b4 KangIngu
311 136851cb djkim
                    Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.zoomAndPanControl.ZoomTo(new Rect
312
                    {
313
                        X = item.searchResult.Left * widthScale,
314
                        Y = item.searchResult.Top * heightScale,
315
                        Width = item.searchResult.Width * widthScale,
316
                        Height = item.searchResult.Height * heightScale
317
                    });
318
319
                }
320 992a98b4 KangIngu
            }
321 136851cb djkim
            catch (Exception ex)
322
            {
323
                Logger.sendResLog("lstSearchWord_SelectionChanged", ex.Message, 0);
324
            }
325
326 787a4489 KangIngu
        }
327
    }
328
}
클립보드 이미지 추가 (최대 크기: 500 MB)