프로젝트

일반

사용자정보

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

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

이력 | 보기 | 이력해설 | 다운로드 (12.7 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.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
using Telerik.Windows.Controls;
20

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

    
50
            tbSearch.KeyDown += tbSearch_KeyDown;
51
            this.DataContext = this;
52

    
53
            worker.DoWork += Worker_DoWork;
54
            worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
55

    
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
        }
75

    
76
        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
77
        {
78
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
79
            {
80
                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

    
101
            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
                        }));
118
                    }
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
                            }));
135
                        }
136
                        while (extractor.FindNext());
137
                }
138
            }
139
        }
140

    
141
        private void tbSearch_KeyDown(object sender, KeyEventArgs e)
142
        {
143
            if (e.Key == Key.Enter)
144
            {
145
                SearchMethod();
146
            }
147
        }
148
        private void LibInit()
149
        {
150
            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
                extractor.OCRLanguageDataFolder = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tessdata");
157
                    
158
                extractor.OCRResolution = 300;
159

    
160
                ocrAnalyzer.OCRLanguage = "eng";
161
                ocrAnalyzer.OCRLanguageDataFolder = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tessdata");
162

    
163
            }
164
            catch (Exception ex)
165
            {
166
                Logger.sendResLog("LibInit", ex.Message, 0);
167
            }
168

    
169
        }
170
        private void SearchPanel_Loaded(object sender, RoutedEventArgs e)
171
        {
172
            //측정
173
            //Stopwatch sw = new Stopwatch();
174
            //sw.Start();
175
            InitializeComponent();
176
            //System.Diagnostics.Debug.WriteLine("SearchPanel() : " + sw.ElapsedMilliseconds.ToString() + "ms");
177

    
178
            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
        }
191

    
192
        private void BtnSearchPDFDownload_Click(object sender, RoutedEventArgs e)
193
        {
194
            try
195
            {
196
                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
                    string downloadurl = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.GetOriginalPDFURL();
202
                    client.DownloadFileAsync(new Uri(downloadurl), instnaceFile, instnaceFile);
203
                }
204
            }
205
            catch (Exception ex)
206
            {
207
                Logger.sendResLog("BtnSearchPDFDownload_Click", ex.Message, 0);
208
            }
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

    
221
        }
222

    
223
        private void BtnSearch_Click(object sender, RoutedEventArgs e)
224
        {
225
            SearchMethod();
226
        }
227

    
228
        private void SearchMethod()
229
        {
230
            try
231
            {
232
                if (!worker.IsBusy)
233
                {
234
                    SearchSet.Clear();
235
                    this.searchIndicator.IsBusy = true;
236
                    worker.RunWorkerAsync();
237
                }
238
            }
239
            catch (Exception ex)
240
            {
241
                Logger.sendResLog("SearchMethod", ex.Message, 0);
242
            }
243

    
244
        }
245
        private void DialogMessage_Alert(string Msg)
246
        {
247
            RadWindow CheckPop = new RadWindow();
248
            Alert check = new Alert(Msg);
249

    
250
            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
        private void chkSelectionChangeEvent(object sender, RoutedEventArgs e)
266
        {
267
            try
268
            {
269
                if (Common.ViewerDataModel.Instance.searchPDF != null)
270
                {
271
                    if (sender is RadioButton)
272
                    {
273
                        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
                    }
288
                }
289
            }
290
            catch (Exception ex)
291
            {
292
                Logger.sendResLog("chkSelectionChangeEvent", ex.Message, 0);
293
            }
294

    
295
        }
296

    
297
        private void lstSearchWord_SelectionChanged(object sender, SelectionChangedEventArgs e)
298
        {
299
            try
300
            {
301
                if (e.AddedItems.Count > 0)
302
                {
303
                    SearchText item = (e.AddedItems[0] as SearchText);
304

    
305
                    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

    
311
                    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
            }
321
            catch (Exception ex)
322
            {
323
                Logger.sendResLog("lstSearchWord_SelectionChanged", ex.Message, 0);
324
            }
325

    
326
        }
327
    }
328
}
클립보드 이미지 추가 (최대 크기: 500 MB)