프로젝트

일반

사용자정보

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

markus / KCOM / Controls / SearchPanel.xaml.cs @ 1edefbdf

이력 | 보기 | 이력해설 | 다운로드 (16.9 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 6af42ff0 taeseongkim
using System.Threading.Tasks;
11 787a4489 KangIngu
using System.Windows;
12
using System.Windows.Controls;
13
using System.Windows.Data;
14
using System.Windows.Documents;
15
using System.Windows.Input;
16
using System.Windows.Media;
17
using System.Windows.Media.Imaging;
18
using System.Windows.Navigation;
19
using System.Windows.Shapes;
20 136851cb djkim
using Telerik.Windows.Controls;
21 787a4489 KangIngu
22
namespace KCOM.Controls
23
{
24 992a98b4 KangIngu
    public class SearchText
25
    {
26
        public Bytescout.PDFExtractor.ISearchResult searchResult { get; set; }
27
        public int PageNo { get; set; }
28
    }
29 787a4489 KangIngu
    /// <summary>
30
    /// Interaction logic for SearchPanel.xaml
31
    /// </summary>
32
    public partial class SearchPanel : UserControl
33
    {
34 992a98b4 KangIngu
        //List<HoneyheadTEXT> txtSet = new List<HoneyheadTEXT>();
35
        public System.Collections.ObjectModel.ObservableCollection<SearchText> SearchSet { get; set; }
36
        string RegistrationKey = "193B-FD48-42F4-68E6-2D35-4C94-D07";
37
        string RegistrationName = "license@doftech.co.kr";
38
        Bytescout.PDFExtractor.TextExtractor extractor;
39 401a87f8 taeseongkim
        //Bytescout.PDFExtractor.OCRAnalyzer ocrAnalyzer;
40 136851cb djkim
        private BackgroundWorker worker = new BackgroundWorker();
41 6af42ff0 taeseongkim
        private bool IsCancelWork = false;
42
43 787a4489 KangIngu
        public SearchPanel()
44
        {
45 e0cfc73c ljiyeon
            App.splashString(ISplashMessage.SEARCHPANEL);
46 79f3f21a djkim
            InitializeComponent();
47
            LibInit();
48
            //this.Loaded += SearchPanel_Loaded;
49
            btnSearch.Click += BtnSearch_Click;
50
            btnSearchPDFDownload.Click += BtnSearchPDFDownload_Click;
51
            SearchSet = new System.Collections.ObjectModel.ObservableCollection<SearchText>();
52 136851cb djkim
53 79f3f21a djkim
            tbSearch.KeyDown += tbSearch_KeyDown;
54
55 136851cb djkim
            worker.DoWork += Worker_DoWork;
56
            worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
57 79f3f21a djkim
58 6af42ff0 taeseongkim
            this.DataContext = this;
59 79f3f21a djkim
            /*
60
            if (Common.ViewerDataModel.Instance.searchPDF ==null)
61
            {
62
63
            }
64
            else
65
            {
66
                border_Overlap.Visibility = Visibility.Collapsed;
67
                tbSearch.Focus();
68
                
69
            }
70
            */
71
72 6af42ff0 taeseongkim
            if (ViewerDataModel.Instance.IsDownloadOriginal)
73 79f3f21a djkim
            {
74 6af42ff0 taeseongkim
                SetSerachPDFFile(ViewerDataModel.Instance.OriginalTempFile);
75 79f3f21a djkim
            }
76 787a4489 KangIngu
        }
77
78 136851cb djkim
        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
79
        {
80
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
81 29010418 ljiyeon
            {
82 6af42ff0 taeseongkim
                btnSearch.Content = "Search";
83
                IsCancelWork = false;
84
85
                this.searchIndicator.IsIndeterminate = false;
86 136851cb djkim
                this.DataContext = this;
87 6af42ff0 taeseongkim
                //tbSearch.Text = "";
88 136851cb djkim
                tbSearch.Focus();
89
90 6af42ff0 taeseongkim
                txtSearchMsg.Text = "search has completed.";
91
            }));
92 136851cb djkim
        }
93
        //private System.Collections.ObjectModel.ObservableCollection<SearchText> searchTexts = null;
94
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
95
        {
96
            string searchString = string.Empty;
97
            bool pageoption = false;
98
99
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
100
            {
101
                SearchSet.Clear();
102
                searchString = tbSearch.Text;
103
                pageoption = chkCurrentPageOnly.IsChecked.Value;
104
            }));
105 29010418 ljiyeon
106 136851cb djkim
            extractor.LoadDocumentFromFile(Common.ViewerDataModel.Instance.searchPDF.FullName);
107 401a87f8 taeseongkim
            extractor.RegexSearch = true;
108
109 223975f7 taeseongkim
            //ocrAnalyzer.LoadDocumentFromFile(Common.ViewerDataModel.Instance.searchPDF.FullName);
110 136851cb djkim
111
            if (pageoption)
112
            {
113
                var currentPage = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.CurrentPage.PageNumber - 1;
114
115 223975f7 taeseongkim
                //Bytescout.PDFExtractor.OCRAnalysisResults analysisResults = ocrAnalyzer.AnalyzeByOCRConfidence(currentPage);
116
                //ocrAnalyzer.ApplyResults(analysisResults, extractor);
117 136851cb djkim
118
                if (extractor.Find(currentPage, searchString, false))
119
                    do
120
                    {
121
                        Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
122
                        {
123
                            SearchSet.Add(new SearchText { PageNo = currentPage + 1, searchResult = extractor.FoundText });
124 29010418 ljiyeon
                        }));
125 136851cb djkim
                    }
126
                    while (extractor.FindNext());
127
            }
128
            else
129
            {
130
                int pageCount = extractor.GetPageCount();
131 6af42ff0 taeseongkim
132 136851cb djkim
                for (int i = 0; i < pageCount; i++)
133
                {
134 6af42ff0 taeseongkim
                    txtSearchMsg.Dispatcher.Invoke(()=> txtSearchMsg.Text = $"Search Pages {i + 1}/{pageCount}");
135
136
                    //Bytescout.PDFExtractor.OCRAnalysisResults analysisResults = ocrAnalyzer.AnalyzeByOCRConfidence(i);
137 223975f7 taeseongkim
                    //ocrAnalyzer.ApplyResults(analysisResults, extractor);
138 136851cb djkim
                    if (extractor.Find(i, searchString, false))
139
                        do
140
                        {
141
                            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
142
                            {
143
                                SearchSet.Add(new SearchText { PageNo = i + 1, searchResult = extractor.FoundText });
144 29010418 ljiyeon
                            }));
145 6af42ff0 taeseongkim
146
                            if(IsCancelWork)
147
                            {
148
                                break;
149
                            }
150 136851cb djkim
                        }
151
                        while (extractor.FindNext());
152 6af42ff0 taeseongkim
153
                    if (IsCancelWork)
154
                    {
155
                        break;
156
                    }
157 136851cb djkim
                }
158 29010418 ljiyeon
            }
159 136851cb djkim
        }
160
161 992a98b4 KangIngu
        private void tbSearch_KeyDown(object sender, KeyEventArgs e)
162 787a4489 KangIngu
        {
163 992a98b4 KangIngu
            if (e.Key == Key.Enter)
164
            {
165
                SearchMethod();
166
            }
167 787a4489 KangIngu
        }
168 79f3f21a djkim
        private void LibInit()
169
        {
170 136851cb djkim
            try
171
            {
172
                extractor = new Bytescout.PDFExtractor.TextExtractor(RegistrationName, RegistrationKey);
173 401a87f8 taeseongkim
                //ocrAnalyzer = new Bytescout.PDFExtractor.OCRAnalyzer(RegistrationName, RegistrationKey);
174
                extractor.OCRMode = Bytescout.PDFExtractor.OCRMode.Off;
175
                //extractor.OCRLanguage = "eng";
176
                //extractor.OCRLanguageDataFolder = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tessdata");
177 6af42ff0 taeseongkim
178 136851cb djkim
                extractor.OCRResolution = 300;
179
180 401a87f8 taeseongkim
                //ocrAnalyzer.OCRLanguage = "eng";
181
                //ocrAnalyzer.OCRLanguageDataFolder = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tessdata");
182 136851cb djkim
183
            }
184
            catch (Exception ex)
185
            {
186 664ea2e1 taeseongkim
                //Logger.sendResLog("LibInit", ex.Message, 0);
187 136851cb djkim
            }
188 787a4489 KangIngu
189 79f3f21a djkim
        }
190 6af42ff0 taeseongkim
191 787a4489 KangIngu
        private void SearchPanel_Loaded(object sender, RoutedEventArgs e)
192
        {
193 6af42ff0 taeseongkim
            ////
194
            /// 이벤트 안 들어옴
195
196
197 0c997b99 ljiyeon
            //측정
198
            //Stopwatch sw = new Stopwatch();
199
            //sw.Start();
200
            InitializeComponent();
201
            //System.Diagnostics.Debug.WriteLine("SearchPanel() : " + sw.ElapsedMilliseconds.ToString() + "ms");
202 136851cb djkim
203 0c997b99 ljiyeon
            btnSearch.Click += BtnSearch_Click;
204
            btnSearchPDFDownload.Click += BtnSearchPDFDownload_Click;
205
            SearchSet = new System.Collections.ObjectModel.ObservableCollection<SearchText>();
206
            extractor = new Bytescout.PDFExtractor.TextExtractor(RegistrationName, RegistrationKey);
207
            tbSearch.KeyDown += tbSearch_KeyDown;
208 6af42ff0 taeseongkim
209 0c997b99 ljiyeon
            this.DataContext = this;
210
211 992a98b4 KangIngu
        }
212
213
        private void BtnSearchPDFDownload_Click(object sender, RoutedEventArgs e)
214
        {
215 136851cb djkim
            try
216 992a98b4 KangIngu
            {
217 6af42ff0 taeseongkim
                Common.ViewerDataModel.Instance.IsDownloadCancel = true;
218
219
220 136851cb djkim
                using (System.Net.WebClient client = new System.Net.WebClient())
221
                {
222
                    string instnaceFile = System.IO.Path.GetTempFileName().Replace(".pdf", ".tmp");
223
                    client.DownloadFileCompleted += Client_DownloadFileCompleted;
224
                    client.DownloadProgressChanged += Client_DownloadProgressChanged;
225 223975f7 taeseongkim
                    string downloadurl = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.GetOriginalPDFURL();
226 6af42ff0 taeseongkim
227
                    client.DownloadFileAsync(new Uri(downloadurl), instnaceFile);
228 136851cb djkim
                }
229
            }
230
            catch (Exception ex)
231
            {
232 664ea2e1 taeseongkim
                //Logger.sendResLog("DownloadOriginalFileAsync", ex.Message, 0);
233 992a98b4 KangIngu
            }
234
        }
235
236 6af42ff0 taeseongkim
237 992a98b4 KangIngu
        private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
238
        {
239 6af42ff0 taeseongkim
            SetSerachPDFFile(e.UserState.ToString());
240
        }
241
242
        public void SetSerachPDFFile(string FileFullPath)
243
        {
244 992a98b4 KangIngu
            border_Overlap.Visibility = Visibility.Collapsed;
245 6af42ff0 taeseongkim
            Common.ViewerDataModel.Instance.searchPDF = new System.IO.FileInfo(FileFullPath);
246 401a87f8 taeseongkim
            //OcrAnalyzer();
247
248 6af42ff0 taeseongkim
            tbSearch.Focus();
249 992a98b4 KangIngu
        }
250
251 401a87f8 taeseongkim
        //private void OcrAnalyzer()
252
        //{
253
        //    System.Drawing.RectangleF rectangle = System.Drawing.RectangleF.Empty;
254
255
        //    // Display analysis progress
256
        //    ocrAnalyzer.ProgressChanged += (object sender, string message, double progress, ref bool cancel)=>
257
        //    {
258
        //        System.Diagnostics.Debug.WriteLine(message);
259
        //    };
260
261
        //    // Load document to OCRAnalyzer
262
        //    ocrAnalyzer.LoadDocumentFromFile(Common.ViewerDataModel.Instance.searchPDF.FullName);
263
264
        //    // Set page area for analysis (optional)
265
        //    ocrAnalyzer.SetExtractionArea(rectangle);
266
267
        //    // Perform analysis and get results
268
        //    Bytescout.PDFExtractor.OCRAnalysisResults analysisResults = ocrAnalyzer.AnalyzeByOCRConfidence(0);
269
270
        //    // Now extract the text using detected OCR parameters
271
272
        //    string outputDocument = @".\result.txt";
273
274
        //    // Create TextExtractor instance
275
276
        //    // Load document to TextExtractor
277
        //    extractor.LoadDocumentFromFile(Common.ViewerDataModel.Instance.searchPDF.FullName);
278
279
        //    // Setup TextExtractor
280
        //    extractor.OCRMode = Bytescout.PDFExtractor.OCRMode.TextFromImagesAndVectorsAndFonts;
281
        //    extractor.OCRLanguageDataFolder = ocrAnalyzer.OCRLanguageDataFolder;
282
        //    extractor.OCRLanguage = ocrAnalyzer.OCRLanguage;
283
284
        //        // Apply analysys results to TextExtractor instance
285
        //    ocrAnalyzer.ApplyResults(analysisResults, extractor);
286
287
        //    // Set extraction area (optional)
288
        //    extractor.SetExtractionArea(rectangle);
289
290
        //    // Save extracted text to file
291
        //    extractor.SaveTextToFile(outputDocument);
292
293
        //}
294
295 992a98b4 KangIngu
        private void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
296
        {
297
            tlDonwloadState.Text = "문서를 다운로드 중입니다 : " + e.ProgressPercentage;
298 136851cb djkim
299 992a98b4 KangIngu
        }
300
301
        private void BtnSearch_Click(object sender, RoutedEventArgs e)
302
        {
303
            SearchMethod();
304
        }
305 136851cb djkim
306 992a98b4 KangIngu
        private void SearchMethod()
307
        {
308 6af42ff0 taeseongkim
            string _strCancel = "Cancel";  // 나중에 정리 
309
310 136851cb djkim
            try
311 992a98b4 KangIngu
            {
312 6af42ff0 taeseongkim
                if (btnSearch.Content.ToString() == _strCancel)
313
                {
314
                    IsCancelWork = true;
315 c5519c44 taeseongkim
                    worker.CancelAsync();
316 6af42ff0 taeseongkim
                }
317
318 136851cb djkim
                if (!worker.IsBusy)
319 992a98b4 KangIngu
                {
320 136851cb djkim
                    SearchSet.Clear();
321 6af42ff0 taeseongkim
322
                    this.searchIndicator.IsIndeterminate = true;
323
                    btnSearch.Content = _strCancel;
324
325 136851cb djkim
                    worker.RunWorkerAsync();
326 992a98b4 KangIngu
                }
327
            }
328 136851cb djkim
            catch (Exception ex)
329
            {
330 664ea2e1 taeseongkim
                //Logger.sendResLog("SearchMethod", ex.Message, 0);
331 136851cb djkim
            }
332 992a98b4 KangIngu
333
        }
334 136851cb djkim
        private void DialogMessage_Alert(string Msg)
335
        {
336
            RadWindow CheckPop = new RadWindow();
337
            Alert check = new Alert(Msg);
338 992a98b4 KangIngu
339 136851cb djkim
            CheckPop = new RadWindow
340
            {
341
                Owner = Application.Current.MainWindow,
342
                MinWidth = 400,
343
                MinHeight = 100,
344
                //Closed = (obj, args) => this.MarkupNamePromptClose(MarkupData, args),
345
                Header = "Alert",
346
                Content = check,
347
                ResizeMode = System.Windows.ResizeMode.NoResize,
348
                WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen,
349
                IsTopmost = true,
350
            };
351
            StyleManager.SetTheme(CheckPop, new Office2013Theme());
352
            CheckPop.ShowDialog();
353
        }
354 992a98b4 KangIngu
        private void chkSelectionChangeEvent(object sender, RoutedEventArgs e)
355
        {
356 136851cb djkim
            try
357 992a98b4 KangIngu
            {
358 136851cb djkim
                if (Common.ViewerDataModel.Instance.searchPDF != null)
359 992a98b4 KangIngu
                {
360 136851cb djkim
                    if (sender is RadioButton)
361 992a98b4 KangIngu
                    {
362 136851cb djkim
                        switch ((sender as RadioButton).Tag.ToString())
363
                        {
364
                            case "0":
365
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.None;
366
                                break;
367
                            case "1":
368
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.ExactMatch;
369
                                break;
370
                            case "2":
371
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.SmartMatch;
372
                                break;
373
                            default:
374
                                break;
375
                        }
376 992a98b4 KangIngu
                    }
377
                }
378
            }
379 136851cb djkim
            catch (Exception ex)
380
            {
381 664ea2e1 taeseongkim
                //Logger.sendResLog("chkSelectionChangeEvent", ex.Message, 0);
382 136851cb djkim
            }
383
384 992a98b4 KangIngu
        }
385
386
        private void lstSearchWord_SelectionChanged(object sender, SelectionChangedEventArgs e)
387
        {
388 136851cb djkim
            try
389 992a98b4 KangIngu
            {
390 136851cb djkim
                if (e.AddedItems.Count > 0)
391
                {
392
                    SearchText item = (e.AddedItems[0] as SearchText);
393 992a98b4 KangIngu
394 6af42ff0 taeseongkim
                    if (Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.CurrentPage.PageNumber == item.PageNo)
395
                    {
396
                        GotoPosition(item);
397
                    }
398
                    else
399 136851cb djkim
                    {
400 5a00ee8e taeseongkim
                        var pageNavigator = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator;
401 136851cb djkim
402 eeb0a39c taeseongkim
                        EventHandler<EventArgs> handler = null;
403 5a00ee8e taeseongkim
404
                        handler = (snd, evt) =>
405 6af42ff0 taeseongkim
                        {
406
                            GotoPosition(item);
407 eeb0a39c taeseongkim
                            ViewerDataModel.Instance.PageLoaded -= handler;
408 6af42ff0 taeseongkim
                        };
409 5a00ee8e taeseongkim
410 eeb0a39c taeseongkim
                        ViewerDataModel.Instance.PageLoaded += handler;
411 5a00ee8e taeseongkim
                        pageNavigator.GotoPage(item.PageNo);
412
413 6af42ff0 taeseongkim
                    }
414 136851cb djkim
                }
415 992a98b4 KangIngu
            }
416 136851cb djkim
            catch (Exception ex)
417
            {
418 664ea2e1 taeseongkim
                //Logger.sendResLog("lstSearchWord_SelectionChanged", ex.Message, 0);
419 136851cb djkim
            }
420
421 787a4489 KangIngu
        }
422 6af42ff0 taeseongkim
423
        private void GotoPosition(SearchText item)
424
        {
425
            Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.Dispatcher.Invoke(() => {
426
427 4f017ed3 taeseongkim
                for (int i = 0; i < (Common.ViewerDataModel.Instance.MarkupAngle / 90); i++)
428 5beaf28e taeseongkim
                {
429
                    Common.ViewerDataModel.Instance.SystemMain.dzTopMenu.drawingPannelRotate(true);
430
                }
431
432 6af42ff0 taeseongkim
                var width = Convert.ToDouble(Common.ViewerDataModel.Instance.Document_Info[item.PageNo - 1].PAGE_WIDTH);
433
                var height = Convert.ToDouble(Common.ViewerDataModel.Instance.Document_Info[item.PageNo - 1].PAGE_HEIGHT);
434
                var widthScale = width / Convert.ToDouble(extractor.GetPageRect_Width(item.PageNo - 1));
435
                var heightScale = height / Convert.ToDouble(extractor.GetPageRect_Height(item.PageNo - 1));
436
437 f4ec4218 taeseongkim
                var searchFocus = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.SearchFocusBorder;
438
                searchFocus.Visibility = Visibility;
439
440
                Canvas.SetLeft(searchFocus, item.searchResult.Left * widthScale);
441
                Canvas.SetTop(searchFocus, item.searchResult.Top * heightScale);
442
                searchFocus.Width = item.searchResult.Width * widthScale;
443
                searchFocus.Height = item.searchResult.Height * widthScale;
444
445 6af42ff0 taeseongkim
                Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.zoomAndPanControl.ZoomTo(new Rect
446
                {
447
                    X =item.searchResult.Left * widthScale,
448
                    Y = item.searchResult.Top * heightScale,
449
                    Width = ( item.searchResult.Width * widthScale *7),
450
                    Height = (item.searchResult.Height * heightScale *7)
451
                });
452
            });
453
        }
454 787a4489 KangIngu
    }
455
}
클립보드 이미지 추가 (최대 크기: 500 MB)