markus / KCOM / Controls / SearchPanel.xaml.cs @ e0cfc73c
이력 | 보기 | 이력해설 | 다운로드 (12.6 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 |
client.DownloadFileAsync(new Uri(Common.ViewerDataModel.Instance.SystemMain.dzMainMenu._DocInfo.ORIGINAL_FILE), instnaceFile, instnaceFile); |
202 |
} |
203 |
} |
204 |
catch (Exception ex) |
205 |
{ |
206 |
Logger.sendResLog("BtnSearchPDFDownload_Click", ex.Message, 0); |
207 |
} |
208 |
} |
209 |
|
210 |
private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) |
211 |
{ |
212 |
border_Overlap.Visibility = Visibility.Collapsed; |
213 |
Common.ViewerDataModel.Instance.searchPDF = new System.IO.FileInfo(e.UserState.ToString()); |
214 |
} |
215 |
|
216 |
private void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e) |
217 |
{ |
218 |
tlDonwloadState.Text = "문서를 다운로드 중입니다 : " + e.ProgressPercentage; |
219 |
|
220 |
} |
221 |
|
222 |
private void BtnSearch_Click(object sender, RoutedEventArgs e) |
223 |
{ |
224 |
SearchMethod(); |
225 |
} |
226 |
|
227 |
private void SearchMethod() |
228 |
{ |
229 |
try |
230 |
{ |
231 |
if (!worker.IsBusy) |
232 |
{ |
233 |
SearchSet.Clear(); |
234 |
this.searchIndicator.IsBusy = true; |
235 |
worker.RunWorkerAsync(); |
236 |
} |
237 |
} |
238 |
catch (Exception ex) |
239 |
{ |
240 |
Logger.sendResLog("SearchMethod", ex.Message, 0); |
241 |
} |
242 |
|
243 |
} |
244 |
private void DialogMessage_Alert(string Msg) |
245 |
{ |
246 |
RadWindow CheckPop = new RadWindow(); |
247 |
Alert check = new Alert(Msg); |
248 |
|
249 |
CheckPop = new RadWindow |
250 |
{ |
251 |
Owner = Application.Current.MainWindow, |
252 |
MinWidth = 400, |
253 |
MinHeight = 100, |
254 |
//Closed = (obj, args) => this.MarkupNamePromptClose(MarkupData, args), |
255 |
Header = "Alert", |
256 |
Content = check, |
257 |
ResizeMode = System.Windows.ResizeMode.NoResize, |
258 |
WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen, |
259 |
IsTopmost = true, |
260 |
}; |
261 |
StyleManager.SetTheme(CheckPop, new Office2013Theme()); |
262 |
CheckPop.ShowDialog(); |
263 |
} |
264 |
private void chkSelectionChangeEvent(object sender, RoutedEventArgs e) |
265 |
{ |
266 |
try |
267 |
{ |
268 |
if (Common.ViewerDataModel.Instance.searchPDF != null) |
269 |
{ |
270 |
if (sender is RadioButton) |
271 |
{ |
272 |
switch ((sender as RadioButton).Tag.ToString()) |
273 |
{ |
274 |
case "0": |
275 |
extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.None; |
276 |
break; |
277 |
case "1": |
278 |
extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.ExactMatch; |
279 |
break; |
280 |
case "2": |
281 |
extractor.WordMatchingMode = Bytescout.PDFExtractor.WordMatchingMode.SmartMatch; |
282 |
break; |
283 |
default: |
284 |
break; |
285 |
} |
286 |
} |
287 |
} |
288 |
} |
289 |
catch (Exception ex) |
290 |
{ |
291 |
Logger.sendResLog("chkSelectionChangeEvent", ex.Message, 0); |
292 |
} |
293 |
|
294 |
} |
295 |
|
296 |
private void lstSearchWord_SelectionChanged(object sender, SelectionChangedEventArgs e) |
297 |
{ |
298 |
try |
299 |
{ |
300 |
if (e.AddedItems.Count > 0) |
301 |
{ |
302 |
SearchText item = (e.AddedItems[0] as SearchText); |
303 |
|
304 |
Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.pageNavigator.GotoPage(item.PageNo); |
305 |
var width = Convert.ToDouble(Common.ViewerDataModel.Instance.Document_Info[item.PageNo - 1].PAGE_WIDTH); |
306 |
var height = Convert.ToDouble(Common.ViewerDataModel.Instance.Document_Info[item.PageNo - 1].PAGE_HEIGHT); |
307 |
var widthScale = width / Convert.ToDouble(extractor.GetPageRect_Width(item.PageNo - 1)); |
308 |
var heightScale = height / Convert.ToDouble(extractor.GetPageRect_Height(item.PageNo - 1)); |
309 |
|
310 |
Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.zoomAndPanControl.ZoomTo(new Rect |
311 |
{ |
312 |
X = item.searchResult.Left * widthScale, |
313 |
Y = item.searchResult.Top * heightScale, |
314 |
Width = item.searchResult.Width * widthScale, |
315 |
Height = item.searchResult.Height * heightScale |
316 |
}); |
317 |
|
318 |
} |
319 |
} |
320 |
catch (Exception ex) |
321 |
{ |
322 |
Logger.sendResLog("lstSearchWord_SelectionChanged", ex.Message, 0); |
323 |
} |
324 |
|
325 |
} |
326 |
} |
327 |
} |