프로젝트

일반

사용자정보

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

markus / KCOM / Controls / SearchPanel.xaml.cs @ 6af42ff0

이력 | 보기 | 이력해설 | 다운로드 (14.1 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 79f3f21a djkim
        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
60 79f3f21a djkim
            /*
61
            if (Common.ViewerDataModel.Instance.searchPDF ==null)
62
            {
63
64
            }
65
            else
66
            {
67
                border_Overlap.Visibility = Visibility.Collapsed;
68
                tbSearch.Focus();
69
                
70
            }
71
            */
72
73 6af42ff0 taeseongkim
            if (ViewerDataModel.Instance.IsDownloadOriginal)
74 79f3f21a djkim
            {
75 6af42ff0 taeseongkim
                SetSerachPDFFile(ViewerDataModel.Instance.OriginalTempFile);
76 79f3f21a djkim
            }
77 787a4489 KangIngu
        }
78
79 136851cb djkim
        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
80
        {
81
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
82 29010418 ljiyeon
            {
83 6af42ff0 taeseongkim
                btnSearch.Content = "Search";
84
                IsCancelWork = false;
85
86
                this.searchIndicator.IsIndeterminate = false;
87 136851cb djkim
                this.DataContext = this;
88 6af42ff0 taeseongkim
                //tbSearch.Text = "";
89 136851cb djkim
                tbSearch.Focus();
90
91 6af42ff0 taeseongkim
                txtSearchMsg.Text = "search has completed.";
92
            }));
93 136851cb djkim
        }
94
        //private System.Collections.ObjectModel.ObservableCollection<SearchText> searchTexts = null;
95
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
96
        {
97
            string searchString = string.Empty;
98
            bool pageoption = false;
99
100
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
101
            {
102
                SearchSet.Clear();
103
                searchString = tbSearch.Text;
104
                pageoption = chkCurrentPageOnly.IsChecked.Value;
105
            }));
106 29010418 ljiyeon
107 136851cb djkim
            extractor.LoadDocumentFromFile(Common.ViewerDataModel.Instance.searchPDF.FullName);
108 223975f7 taeseongkim
            //ocrAnalyzer.LoadDocumentFromFile(Common.ViewerDataModel.Instance.searchPDF.FullName);
109 136851cb djkim
110
            if (pageoption)
111
            {
112
                var currentPage = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.CurrentPage.PageNumber - 1;
113
114 223975f7 taeseongkim
                //Bytescout.PDFExtractor.OCRAnalysisResults analysisResults = ocrAnalyzer.AnalyzeByOCRConfidence(currentPage);
115
                //ocrAnalyzer.ApplyResults(analysisResults, extractor);
116 136851cb djkim
117
                if (extractor.Find(currentPage, searchString, false))
118
                    do
119
                    {
120
                        Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
121
                        {
122
                            SearchSet.Add(new SearchText { PageNo = currentPage + 1, searchResult = extractor.FoundText });
123 29010418 ljiyeon
                        }));
124 136851cb djkim
                    }
125
                    while (extractor.FindNext());
126
            }
127
            else
128
            {
129
                int pageCount = extractor.GetPageCount();
130 6af42ff0 taeseongkim
131 136851cb djkim
                for (int i = 0; i < pageCount; i++)
132
                {
133 6af42ff0 taeseongkim
                    txtSearchMsg.Dispatcher.Invoke(()=> txtSearchMsg.Text = $"Search Pages {i + 1}/{pageCount}");
134
135
                    //Bytescout.PDFExtractor.OCRAnalysisResults analysisResults = ocrAnalyzer.AnalyzeByOCRConfidence(i);
136 223975f7 taeseongkim
                    //ocrAnalyzer.ApplyResults(analysisResults, extractor);
137 136851cb djkim
                    if (extractor.Find(i, searchString, false))
138
                        do
139
                        {
140
                            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
141
                            {
142
                                SearchSet.Add(new SearchText { PageNo = i + 1, searchResult = extractor.FoundText });
143 29010418 ljiyeon
                            }));
144 6af42ff0 taeseongkim
145
                            if(IsCancelWork)
146
                            {
147
                                break;
148
                            }
149 136851cb djkim
                        }
150
                        while (extractor.FindNext());
151 6af42ff0 taeseongkim
152
                    if (IsCancelWork)
153
                    {
154
                        break;
155
                    }
156 136851cb djkim
                }
157 29010418 ljiyeon
            }
158 136851cb djkim
        }
159
160 992a98b4 KangIngu
        private void tbSearch_KeyDown(object sender, KeyEventArgs e)
161 787a4489 KangIngu
        {
162 992a98b4 KangIngu
            if (e.Key == Key.Enter)
163
            {
164
                SearchMethod();
165
            }
166 787a4489 KangIngu
        }
167 79f3f21a djkim
        private void LibInit()
168
        {
169 136851cb djkim
            try
170
            {
171
                extractor = new Bytescout.PDFExtractor.TextExtractor(RegistrationName, RegistrationKey);
172
                ocrAnalyzer = new Bytescout.PDFExtractor.OCRAnalyzer(RegistrationName, RegistrationKey);
173
                extractor.OCRMode = Bytescout.PDFExtractor.OCRMode.Auto;
174
                extractor.OCRLanguage = "eng";
175 90a0bfa9 djkim
                extractor.OCRLanguageDataFolder = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tessdata");
176 6af42ff0 taeseongkim
177 136851cb djkim
                extractor.OCRResolution = 300;
178
179
                ocrAnalyzer.OCRLanguage = "eng";
180 90a0bfa9 djkim
                ocrAnalyzer.OCRLanguageDataFolder = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tessdata");
181 136851cb djkim
182
            }
183
            catch (Exception ex)
184
            {
185
                Logger.sendResLog("LibInit", ex.Message, 0);
186
            }
187 787a4489 KangIngu
188 79f3f21a djkim
        }
189 6af42ff0 taeseongkim
190 787a4489 KangIngu
        private void SearchPanel_Loaded(object sender, RoutedEventArgs e)
191
        {
192 6af42ff0 taeseongkim
            ////
193
            /// 이벤트 안 들어옴
194
195
196 0c997b99 ljiyeon
            //측정
197
            //Stopwatch sw = new Stopwatch();
198
            //sw.Start();
199
            InitializeComponent();
200
            //System.Diagnostics.Debug.WriteLine("SearchPanel() : " + sw.ElapsedMilliseconds.ToString() + "ms");
201 136851cb djkim
202 0c997b99 ljiyeon
            btnSearch.Click += BtnSearch_Click;
203
            btnSearchPDFDownload.Click += BtnSearchPDFDownload_Click;
204
            SearchSet = new System.Collections.ObjectModel.ObservableCollection<SearchText>();
205
            extractor = new Bytescout.PDFExtractor.TextExtractor(RegistrationName, RegistrationKey);
206
            tbSearch.KeyDown += tbSearch_KeyDown;
207 6af42ff0 taeseongkim
208 0c997b99 ljiyeon
            this.DataContext = this;
209
210 992a98b4 KangIngu
        }
211
212
        private void BtnSearchPDFDownload_Click(object sender, RoutedEventArgs e)
213
        {
214 136851cb djkim
            try
215 992a98b4 KangIngu
            {
216 6af42ff0 taeseongkim
                Common.ViewerDataModel.Instance.IsDownloadCancel = true;
217
218
219 136851cb djkim
                using (System.Net.WebClient client = new System.Net.WebClient())
220
                {
221
                    string instnaceFile = System.IO.Path.GetTempFileName().Replace(".pdf", ".tmp");
222
                    client.DownloadFileCompleted += Client_DownloadFileCompleted;
223
                    client.DownloadProgressChanged += Client_DownloadProgressChanged;
224 223975f7 taeseongkim
                    string downloadurl = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.GetOriginalPDFURL();
225 6af42ff0 taeseongkim
226
                    client.DownloadFileAsync(new Uri(downloadurl), instnaceFile);
227 136851cb djkim
                }
228
            }
229
            catch (Exception ex)
230
            {
231 6af42ff0 taeseongkim
                Logger.sendResLog("DownloadOriginalFileAsync", ex.Message, 0);
232 992a98b4 KangIngu
            }
233
        }
234
235 6af42ff0 taeseongkim
236 992a98b4 KangIngu
        private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
237
        {
238 6af42ff0 taeseongkim
            SetSerachPDFFile(e.UserState.ToString());
239
        }
240
241
        public void SetSerachPDFFile(string FileFullPath)
242
        {
243 992a98b4 KangIngu
            border_Overlap.Visibility = Visibility.Collapsed;
244 6af42ff0 taeseongkim
            Common.ViewerDataModel.Instance.searchPDF = new System.IO.FileInfo(FileFullPath);
245
            tbSearch.Focus();
246 992a98b4 KangIngu
        }
247
248
        private void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
249
        {
250
            tlDonwloadState.Text = "문서를 다운로드 중입니다 : " + e.ProgressPercentage;
251 136851cb djkim
252 992a98b4 KangIngu
        }
253
254
        private void BtnSearch_Click(object sender, RoutedEventArgs e)
255
        {
256
            SearchMethod();
257
        }
258 136851cb djkim
259 992a98b4 KangIngu
        private void SearchMethod()
260
        {
261 6af42ff0 taeseongkim
            string _strCancel = "Cancel";  // 나중에 정리 
262
263 136851cb djkim
            try
264 992a98b4 KangIngu
            {
265 6af42ff0 taeseongkim
                if (btnSearch.Content.ToString() == _strCancel)
266
                {
267
                    IsCancelWork = true;
268
                }
269
270 136851cb djkim
                if (!worker.IsBusy)
271 992a98b4 KangIngu
                {
272 136851cb djkim
                    SearchSet.Clear();
273 6af42ff0 taeseongkim
274
                    this.searchIndicator.IsIndeterminate = true;
275
                    btnSearch.Content = _strCancel;
276
277 136851cb djkim
                    worker.RunWorkerAsync();
278 992a98b4 KangIngu
                }
279
            }
280 136851cb djkim
            catch (Exception ex)
281
            {
282
                Logger.sendResLog("SearchMethod", ex.Message, 0);
283
            }
284 992a98b4 KangIngu
285
        }
286 136851cb djkim
        private void DialogMessage_Alert(string Msg)
287
        {
288
            RadWindow CheckPop = new RadWindow();
289
            Alert check = new Alert(Msg);
290 992a98b4 KangIngu
291 136851cb djkim
            CheckPop = new RadWindow
292
            {
293
                Owner = Application.Current.MainWindow,
294
                MinWidth = 400,
295
                MinHeight = 100,
296
                //Closed = (obj, args) => this.MarkupNamePromptClose(MarkupData, args),
297
                Header = "Alert",
298
                Content = check,
299
                ResizeMode = System.Windows.ResizeMode.NoResize,
300
                WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen,
301
                IsTopmost = true,
302
            };
303
            StyleManager.SetTheme(CheckPop, new Office2013Theme());
304
            CheckPop.ShowDialog();
305
        }
306 992a98b4 KangIngu
        private void chkSelectionChangeEvent(object sender, RoutedEventArgs e)
307
        {
308 136851cb djkim
            try
309 992a98b4 KangIngu
            {
310 136851cb djkim
                if (Common.ViewerDataModel.Instance.searchPDF != null)
311 992a98b4 KangIngu
                {
312 136851cb djkim
                    if (sender is RadioButton)
313 992a98b4 KangIngu
                    {
314 136851cb djkim
                        switch ((sender as RadioButton).Tag.ToString())
315
                        {
316
                            case "0":
317
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.None;
318
                                break;
319
                            case "1":
320
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.ExactMatch;
321
                                break;
322
                            case "2":
323
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.SmartMatch;
324
                                break;
325
                            default:
326
                                break;
327
                        }
328 992a98b4 KangIngu
                    }
329
                }
330
            }
331 136851cb djkim
            catch (Exception ex)
332
            {
333
                Logger.sendResLog("chkSelectionChangeEvent", ex.Message, 0);
334
            }
335
336 992a98b4 KangIngu
        }
337
338
        private void lstSearchWord_SelectionChanged(object sender, SelectionChangedEventArgs e)
339
        {
340 136851cb djkim
            try
341 992a98b4 KangIngu
            {
342 136851cb djkim
                if (e.AddedItems.Count > 0)
343
                {
344
                    SearchText item = (e.AddedItems[0] as SearchText);
345 992a98b4 KangIngu
346 6af42ff0 taeseongkim
                    if (Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.CurrentPage.PageNumber == item.PageNo)
347
                    {
348
                        GotoPosition(item);
349
                    }
350
                    else
351 136851cb djkim
                    {
352 6af42ff0 taeseongkim
                        Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.GotoPage(item.PageNo);
353 136851cb djkim
354 6af42ff0 taeseongkim
                        Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.PageChanged += (snd, evt) =>
355
                        {
356
                            GotoPosition(item);
357
                        };
358
                    }
359 136851cb djkim
                }
360 992a98b4 KangIngu
            }
361 136851cb djkim
            catch (Exception ex)
362
            {
363
                Logger.sendResLog("lstSearchWord_SelectionChanged", ex.Message, 0);
364
            }
365
366 787a4489 KangIngu
        }
367 6af42ff0 taeseongkim
368
        private void GotoPosition(SearchText item)
369
        {
370
            Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.Dispatcher.Invoke(() => {
371
372
                var width = Convert.ToDouble(Common.ViewerDataModel.Instance.Document_Info[item.PageNo - 1].PAGE_WIDTH);
373
                var height = Convert.ToDouble(Common.ViewerDataModel.Instance.Document_Info[item.PageNo - 1].PAGE_HEIGHT);
374
                var widthScale = width / Convert.ToDouble(extractor.GetPageRect_Width(item.PageNo - 1));
375
                var heightScale = height / Convert.ToDouble(extractor.GetPageRect_Height(item.PageNo - 1));
376
377
                Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.zoomAndPanControl.ZoomTo(new Rect
378
                {
379
                    X =item.searchResult.Left * widthScale,
380
                    Y = item.searchResult.Top * heightScale,
381
                    Width = ( item.searchResult.Width * widthScale *7),
382
                    Height = (item.searchResult.Height * heightScale *7)
383
                });
384
            });
385
        }
386 787a4489 KangIngu
    }
387
}
클립보드 이미지 추가 (최대 크기: 500 MB)