프로젝트

일반

사용자정보

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

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

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

1
using KCOM.Common;
2
using MarkupToPDF.Common;
3
using System;
4
using System.Collections.Generic;
5
using System.ComponentModel;
6
using System.Diagnostics;
7
using System.Linq;
8
using System.Text;
9
using System.Threading;
10
using System.Threading.Tasks;
11
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
using Telerik.Windows.Controls;
21

    
22
namespace KCOM.Controls
23
{
24
    public class SearchText
25
    {
26
        public Bytescout.PDFExtractor.ISearchResult searchResult { get; set; }
27
        public int PageNo { get; set; }
28
    }
29
    /// <summary>
30
    /// Interaction logic for SearchPanel.xaml
31
    /// </summary>
32
    public partial class SearchPanel : UserControl
33
    {
34
        //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
        Bytescout.PDFExtractor.OCRAnalyzer ocrAnalyzer;
40
        private BackgroundWorker worker = new BackgroundWorker();
41
        private bool IsCancelWork = false;
42

    
43
        public SearchPanel()
44
        {
45
            App.splashString(ISplashMessage.SEARCHPANEL);
46
            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

    
53
            tbSearch.KeyDown += tbSearch_KeyDown;
54

    
55
            worker.DoWork += Worker_DoWork;
56
            worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
57

    
58
            this.DataContext = this;
59

    
60
            /*
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
            if (ViewerDataModel.Instance.IsDownloadOriginal)
74
            {
75
                SetSerachPDFFile(ViewerDataModel.Instance.OriginalTempFile);
76
            }
77
        }
78

    
79
        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
80
        {
81
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
82
            {
83
                btnSearch.Content = "Search";
84
                IsCancelWork = false;
85

    
86
                this.searchIndicator.IsIndeterminate = false;
87
                this.DataContext = this;
88
                //tbSearch.Text = "";
89
                tbSearch.Focus();
90

    
91
                txtSearchMsg.Text = "search has completed.";
92
            }));
93
        }
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

    
107
            extractor.LoadDocumentFromFile(Common.ViewerDataModel.Instance.searchPDF.FullName);
108
            //ocrAnalyzer.LoadDocumentFromFile(Common.ViewerDataModel.Instance.searchPDF.FullName);
109

    
110
            if (pageoption)
111
            {
112
                var currentPage = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.CurrentPage.PageNumber - 1;
113

    
114
                //Bytescout.PDFExtractor.OCRAnalysisResults analysisResults = ocrAnalyzer.AnalyzeByOCRConfidence(currentPage);
115
                //ocrAnalyzer.ApplyResults(analysisResults, extractor);
116

    
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
                        }));
124
                    }
125
                    while (extractor.FindNext());
126
            }
127
            else
128
            {
129
                int pageCount = extractor.GetPageCount();
130

    
131
                for (int i = 0; i < pageCount; i++)
132
                {
133
                    txtSearchMsg.Dispatcher.Invoke(()=> txtSearchMsg.Text = $"Search Pages {i + 1}/{pageCount}");
134

    
135
                    //Bytescout.PDFExtractor.OCRAnalysisResults analysisResults = ocrAnalyzer.AnalyzeByOCRConfidence(i);
136
                    //ocrAnalyzer.ApplyResults(analysisResults, extractor);
137
                    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
                            }));
144

    
145
                            if(IsCancelWork)
146
                            {
147
                                break;
148
                            }
149
                        }
150
                        while (extractor.FindNext());
151

    
152
                    if (IsCancelWork)
153
                    {
154
                        break;
155
                    }
156
                }
157
            }
158
        }
159

    
160
        private void tbSearch_KeyDown(object sender, KeyEventArgs e)
161
        {
162
            if (e.Key == Key.Enter)
163
            {
164
                SearchMethod();
165
            }
166
        }
167
        private void LibInit()
168
        {
169
            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
                extractor.OCRLanguageDataFolder = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tessdata");
176

    
177
                extractor.OCRResolution = 300;
178

    
179
                ocrAnalyzer.OCRLanguage = "eng";
180
                ocrAnalyzer.OCRLanguageDataFolder = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tessdata");
181

    
182
            }
183
            catch (Exception ex)
184
            {
185
                Logger.sendResLog("LibInit", ex.Message, 0);
186
            }
187

    
188
        }
189

    
190
        private void SearchPanel_Loaded(object sender, RoutedEventArgs e)
191
        {
192
            ////
193
            /// 이벤트 안 들어옴
194

    
195

    
196
            //측정
197
            //Stopwatch sw = new Stopwatch();
198
            //sw.Start();
199
            InitializeComponent();
200
            //System.Diagnostics.Debug.WriteLine("SearchPanel() : " + sw.ElapsedMilliseconds.ToString() + "ms");
201

    
202
            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

    
208
            this.DataContext = this;
209

    
210
        }
211

    
212
        private void BtnSearchPDFDownload_Click(object sender, RoutedEventArgs e)
213
        {
214
            try
215
            {
216
                Common.ViewerDataModel.Instance.IsDownloadCancel = true;
217

    
218

    
219
                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
                    string downloadurl = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.GetOriginalPDFURL();
225

    
226
                    client.DownloadFileAsync(new Uri(downloadurl), instnaceFile);
227
                }
228
            }
229
            catch (Exception ex)
230
            {
231
                Logger.sendResLog("DownloadOriginalFileAsync", ex.Message, 0);
232
            }
233
        }
234

    
235

    
236
        private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
237
        {
238
            SetSerachPDFFile(e.UserState.ToString());
239
        }
240

    
241
        public void SetSerachPDFFile(string FileFullPath)
242
        {
243
            border_Overlap.Visibility = Visibility.Collapsed;
244
            Common.ViewerDataModel.Instance.searchPDF = new System.IO.FileInfo(FileFullPath);
245
            tbSearch.Focus();
246
        }
247

    
248
        private void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
249
        {
250
            tlDonwloadState.Text = "문서를 다운로드 중입니다 : " + e.ProgressPercentage;
251

    
252
        }
253

    
254
        private void BtnSearch_Click(object sender, RoutedEventArgs e)
255
        {
256
            SearchMethod();
257
        }
258

    
259
        private void SearchMethod()
260
        {
261
            string _strCancel = "Cancel";  // 나중에 정리 
262

    
263
            try
264
            {
265
                if (btnSearch.Content.ToString() == _strCancel)
266
                {
267
                    IsCancelWork = true;
268
                }
269

    
270
                if (!worker.IsBusy)
271
                {
272
                    SearchSet.Clear();
273

    
274
                    this.searchIndicator.IsIndeterminate = true;
275
                    btnSearch.Content = _strCancel;
276

    
277
                    worker.RunWorkerAsync();
278
                }
279
            }
280
            catch (Exception ex)
281
            {
282
                Logger.sendResLog("SearchMethod", ex.Message, 0);
283
            }
284

    
285
        }
286
        private void DialogMessage_Alert(string Msg)
287
        {
288
            RadWindow CheckPop = new RadWindow();
289
            Alert check = new Alert(Msg);
290

    
291
            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
        private void chkSelectionChangeEvent(object sender, RoutedEventArgs e)
307
        {
308
            try
309
            {
310
                if (Common.ViewerDataModel.Instance.searchPDF != null)
311
                {
312
                    if (sender is RadioButton)
313
                    {
314
                        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
                    }
329
                }
330
            }
331
            catch (Exception ex)
332
            {
333
                Logger.sendResLog("chkSelectionChangeEvent", ex.Message, 0);
334
            }
335

    
336
        }
337

    
338
        private void lstSearchWord_SelectionChanged(object sender, SelectionChangedEventArgs e)
339
        {
340
            try
341
            {
342
                if (e.AddedItems.Count > 0)
343
                {
344
                    SearchText item = (e.AddedItems[0] as SearchText);
345

    
346
                    if (Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.CurrentPage.PageNumber == item.PageNo)
347
                    {
348
                        GotoPosition(item);
349
                    }
350
                    else
351
                    {
352
                        Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.GotoPage(item.PageNo);
353

    
354
                        Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.PageChanged += (snd, evt) =>
355
                        {
356
                            GotoPosition(item);
357
                        };
358
                    }
359
                }
360
            }
361
            catch (Exception ex)
362
            {
363
                Logger.sendResLog("lstSearchWord_SelectionChanged", ex.Message, 0);
364
            }
365

    
366
        }
367

    
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
    }
387
}
클립보드 이미지 추가 (최대 크기: 500 MB)