프로젝트

일반

사용자정보

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

markus / KCOM / Controls / SearchPanel.xaml.cs @ 29010418

이력 | 보기 | 이력해설 | 다운로드 (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.AppDomain.CurrentDomain.BaseDirectory + "\\tessdata\\";
155
                extractor.OCRResolution = 300;
156

    
157
                ocrAnalyzer.OCRLanguage = "eng";
158
                ocrAnalyzer.OCRLanguageDataFolder = System.AppDomain.CurrentDomain.BaseDirectory + "\\tessdata\\";
159

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

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

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

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

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

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

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

    
217
        }
218

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

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

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

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

    
291
        }
292

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

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

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

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

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