프로젝트

일반

사용자정보

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

markus / KCOM / Controls / SearchPanel.xaml.cs @ 90a0bfa9

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

1
using MarkupToPDF.Common;
2
using System;
3
using System.Collections.Generic;
4
using System.ComponentModel;
5
using System.Diagnostics;
6
using System.Linq;
7
using System.Text;
8
using System.Threading;
9
using System.Windows;
10
using System.Windows.Controls;
11
using System.Windows.Data;
12
using System.Windows.Documents;
13
using System.Windows.Input;
14
using System.Windows.Media;
15
using System.Windows.Media.Imaging;
16
using System.Windows.Navigation;
17
using System.Windows.Shapes;
18
using Telerik.Windows.Controls;
19

    
20
namespace KCOM.Controls
21
{
22
    public class SearchText
23
    {
24
        public Bytescout.PDFExtractor.ISearchResult searchResult { get; set; }
25
        public int PageNo { get; set; }
26
    }
27
    /// <summary>
28
    /// Interaction logic for SearchPanel.xaml
29
    /// </summary>
30
    public partial class SearchPanel : UserControl
31
    {
32
        //List<HoneyheadTEXT> txtSet = new List<HoneyheadTEXT>();
33
        public System.Collections.ObjectModel.ObservableCollection<SearchText> SearchSet { get; set; }
34
        string RegistrationKey = "193B-FD48-42F4-68E6-2D35-4C94-D07";
35
        string RegistrationName = "license@doftech.co.kr";
36
        Bytescout.PDFExtractor.TextExtractor extractor;
37
        Bytescout.PDFExtractor.OCRAnalyzer ocrAnalyzer;
38
        private BackgroundWorker worker = new BackgroundWorker();
39
        public SearchPanel()
40
        {
41
            InitializeComponent();
42
            LibInit();
43
            //this.Loaded += SearchPanel_Loaded;
44
            btnSearch.Click += BtnSearch_Click;
45
            btnSearchPDFDownload.Click += BtnSearchPDFDownload_Click;
46
            SearchSet = new System.Collections.ObjectModel.ObservableCollection<SearchText>();
47

    
48
            tbSearch.KeyDown += tbSearch_KeyDown;
49
            this.DataContext = this;
50

    
51
            worker.DoWork += Worker_DoWork;
52
            worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
53

    
54
            /*
55
            if (Common.ViewerDataModel.Instance.searchPDF ==null)
56
            {
57

    
58
            }
59
            else
60
            {
61
                border_Overlap.Visibility = Visibility.Collapsed;
62
                tbSearch.Focus();
63
                
64
            }
65
            */
66

    
67
            if (Common.ViewerDataModel.Instance.searchPDF != null)
68
            {
69
                border_Overlap.Visibility = Visibility.Collapsed;
70
                tbSearch.Focus();
71
            }
72
        }
73

    
74
        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
75
        {
76
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
77
            {
78
                this.searchIndicator.IsBusy = false;
79
                this.DataContext = this;
80
                tbSearch.Text = "";
81
                tbSearch.Focus();
82
                DialogMessage_Alert("검색이 완료되었습니다.");
83
            }));
84

    
85
        }
86
        //private System.Collections.ObjectModel.ObservableCollection<SearchText> searchTexts = null;
87
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
88
        {
89
            string searchString = string.Empty;
90
            bool pageoption = false;
91

    
92
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
93
            {
94
                SearchSet.Clear();
95
                searchString = tbSearch.Text;
96
                pageoption = chkCurrentPageOnly.IsChecked.Value;
97
            }));
98

    
99
            extractor.LoadDocumentFromFile(Common.ViewerDataModel.Instance.searchPDF.FullName);
100
            ocrAnalyzer.LoadDocumentFromFile(Common.ViewerDataModel.Instance.searchPDF.FullName);
101

    
102
            if (pageoption)
103
            {
104
                var currentPage = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.CurrentPage.PageNumber - 1;
105

    
106
                Bytescout.PDFExtractor.OCRAnalysisResults analysisResults = ocrAnalyzer.AnalyzeByOCRConfidence(currentPage);
107
                ocrAnalyzer.ApplyResults(analysisResults, extractor);
108

    
109
                if (extractor.Find(currentPage, searchString, false))
110
                    do
111
                    {
112
                        Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
113
                        {
114
                            SearchSet.Add(new SearchText { PageNo = currentPage + 1, searchResult = extractor.FoundText });
115
                        }));
116
                    }
117
                    while (extractor.FindNext());
118
            }
119
            else
120
            {
121
                int pageCount = extractor.GetPageCount();
122
                for (int i = 0; i < pageCount; i++)
123
                {
124
                    Bytescout.PDFExtractor.OCRAnalysisResults analysisResults = ocrAnalyzer.AnalyzeByOCRConfidence(i);
125
                    ocrAnalyzer.ApplyResults(analysisResults, extractor);
126
                    if (extractor.Find(i, searchString, false))
127
                        do
128
                        {
129
                            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
130
                            {
131
                                SearchSet.Add(new SearchText { PageNo = i + 1, searchResult = extractor.FoundText });
132
                            }));
133
                        }
134
                        while (extractor.FindNext());
135
                }
136
            }
137
        }
138

    
139
        private void tbSearch_KeyDown(object sender, KeyEventArgs e)
140
        {
141
            if (e.Key == Key.Enter)
142
            {
143
                SearchMethod();
144
            }
145
        }
146
        private void LibInit()
147
        {
148
            try
149
            {
150
                extractor = new Bytescout.PDFExtractor.TextExtractor(RegistrationName, RegistrationKey);
151
                ocrAnalyzer = new Bytescout.PDFExtractor.OCRAnalyzer(RegistrationName, RegistrationKey);
152
                extractor.OCRMode = Bytescout.PDFExtractor.OCRMode.Auto;
153
                extractor.OCRLanguage = "eng";
154
                extractor.OCRLanguageDataFolder = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tessdata");
155
                    
156
                extractor.OCRResolution = 300;
157

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

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

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

    
176
            btnSearch.Click += BtnSearch_Click;
177
            btnSearchPDFDownload.Click += BtnSearchPDFDownload_Click;
178
            SearchSet = new System.Collections.ObjectModel.ObservableCollection<SearchText>();
179
            extractor = new Bytescout.PDFExtractor.TextExtractor(RegistrationName, RegistrationKey);
180
            tbSearch.KeyDown += tbSearch_KeyDown;
181
            this.DataContext = this;
182

    
183
            if (Common.ViewerDataModel.Instance.searchPDF != null)
184
            {
185
                border_Overlap.Visibility = Visibility.Collapsed;
186
                tbSearch.Focus();
187
            }
188
        }
189

    
190
        private void BtnSearchPDFDownload_Click(object sender, RoutedEventArgs e)
191
        {
192
            try
193
            {
194
                using (System.Net.WebClient client = new System.Net.WebClient())
195
                {
196
                    string instnaceFile = System.IO.Path.GetTempFileName().Replace(".pdf", ".tmp");
197
                    client.DownloadFileCompleted += Client_DownloadFileCompleted;
198
                    client.DownloadProgressChanged += Client_DownloadProgressChanged;
199
                    client.DownloadFileAsync(new Uri(Common.ViewerDataModel.Instance.SystemMain.dzMainMenu._DocInfo.ORIGINAL_FILE), instnaceFile, instnaceFile);
200
                }
201
            }
202
            catch (Exception ex)
203
            {
204
                Logger.sendResLog("BtnSearchPDFDownload_Click", ex.Message, 0);
205
            }
206
        }
207

    
208
        private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
209
        {
210
            border_Overlap.Visibility = Visibility.Collapsed;
211
            Common.ViewerDataModel.Instance.searchPDF = new System.IO.FileInfo(e.UserState.ToString());
212
        }
213

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

    
218
        }
219

    
220
        private void BtnSearch_Click(object sender, RoutedEventArgs e)
221
        {
222
            SearchMethod();
223
        }
224

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

    
241
        }
242
        private void DialogMessage_Alert(string Msg)
243
        {
244
            RadWindow CheckPop = new RadWindow();
245
            Alert check = new Alert(Msg);
246

    
247
            CheckPop = new RadWindow
248
            {
249
                Owner = Application.Current.MainWindow,
250
                MinWidth = 400,
251
                MinHeight = 100,
252
                //Closed = (obj, args) => this.MarkupNamePromptClose(MarkupData, args),
253
                Header = "Alert",
254
                Content = check,
255
                ResizeMode = System.Windows.ResizeMode.NoResize,
256
                WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen,
257
                IsTopmost = true,
258
            };
259
            StyleManager.SetTheme(CheckPop, new Office2013Theme());
260
            CheckPop.ShowDialog();
261
        }
262
        private void chkSelectionChangeEvent(object sender, RoutedEventArgs e)
263
        {
264
            try
265
            {
266
                if (Common.ViewerDataModel.Instance.searchPDF != null)
267
                {
268
                    if (sender is RadioButton)
269
                    {
270
                        switch ((sender as RadioButton).Tag.ToString())
271
                        {
272
                            case "0":
273
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.None;
274
                                break;
275
                            case "1":
276
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.ExactMatch;
277
                                break;
278
                            case "2":
279
                                extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.SmartMatch;
280
                                break;
281
                            default:
282
                                break;
283
                        }
284
                    }
285
                }
286
            }
287
            catch (Exception ex)
288
            {
289
                Logger.sendResLog("chkSelectionChangeEvent", ex.Message, 0);
290
            }
291

    
292
        }
293

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

    
302
                    Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.GotoPage(item.PageNo);
303
                    var width = Convert.ToDouble(Common.ViewerDataModel.Instance.Document_Info[item.PageNo - 1].PAGE_WIDTH);
304
                    var height = Convert.ToDouble(Common.ViewerDataModel.Instance.Document_Info[item.PageNo - 1].PAGE_HEIGHT);
305
                    var widthScale = width / Convert.ToDouble(extractor.GetPageRect_Width(item.PageNo - 1));
306
                    var heightScale = height / Convert.ToDouble(extractor.GetPageRect_Height(item.PageNo - 1));
307

    
308
                    Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.zoomAndPanControl.ZoomTo(new Rect
309
                    {
310
                        X = item.searchResult.Left * widthScale,
311
                        Y = item.searchResult.Top * heightScale,
312
                        Width = item.searchResult.Width * widthScale,
313
                        Height = item.searchResult.Height * heightScale
314
                    });
315

    
316
                }
317
            }
318
            catch (Exception ex)
319
            {
320
                Logger.sendResLog("lstSearchWord_SelectionChanged", ex.Message, 0);
321
            }
322

    
323
        }
324
    }
325
}
클립보드 이미지 추가 (최대 크기: 500 MB)