123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- using ComPDFKit.PDFPage;
- using ComPDFKit.Tool;
- using ComPDFKit.Controls.PDFControlUI;
- using ComPDFKitViewer;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using ComPDFKit.Controls.Helper;
- namespace ComPDFKit.Controls.PDFControl
- {
- public partial class CPDFSearchControl : UserControl
- {
- /// <summary>
- /// PDFViewer
- /// </summary>
- private CPDFViewer pdfView;
- private int currentHighLightIndex { get; set; } = -1;
- private C_Search_Options searchOption = C_Search_Options.Search_Case_Insensitive;
- private PDFTextSearch textSearch;
- private string keyWord;
- private bool isClearResult = false;
- private SolidColorBrush highLightBrush = new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00));
-
- private int ResultCount = 0;
- private PDFViewControl ViewControl;
- private bool _isCaseSensitive = true;
- public bool IsCaseSensitive
- {
- set
- {
- _isCaseSensitive = value;
- if (_isCaseSensitive)
- {
- searchOption &= ~C_Search_Options.Search_Case_Sensitive;
- }
- else
- {
- searchOption |= C_Search_Options.Search_Case_Sensitive;
- }
- ViewControl.PDFViewTool?.StartFindText(keyWord, searchOption);
- }
- get
- {
- return _isCaseSensitive;
- }
- }
- public bool IsMatchWholeWord
- {
- set
- {
- if (value)
- {
- searchOption |= C_Search_Options.Search_Match_Whole_Word;
- }
- else
- {
- searchOption &= ~C_Search_Options.Search_Match_Whole_Word;
- }
- ViewControl?.PDFViewTool.StartFindText(keyWord, searchOption);
- }
- }
- public CPDFSearchControl()
- {
- InitializeComponent();
- Loaded += PDFSearch_Loaded;
- DataContext = this;
- }
- public void InitWithPDFViewer(PDFViewControl newViewControl)
- {
- ViewControl = newViewControl;
- if (ViewControl != null)
- {
- CPDFViewerTool viewTool = ViewControl.PDFViewTool;
- if (viewTool != null)
- {
- CPDFViewer newPDFView = viewTool.GetCPDFViewer();
- if (pdfView != newPDFView)
- {
- ClearSearchResult();
- pdfView = newPDFView;
- }
- }
- }
- else
- {
- ClearSearchResult();
- }
- }
- private void PDFSearch_Loaded(object sender, RoutedEventArgs e)
- {
- textSearch = new PDFTextSearch();
- SearchInput.SearchEvent -= SearchInput_SearchEvent;
- SearchInput.ClearEvent -= SearchInput_ClearEvent;
- textSearch.SearchCompletedHandler -= TextSearch_SearchCompletedHandler;
- SearchResult.SelectionChanged -= SearchResult_SelectionChanged;
- textSearch.SearchPercentHandler -= TextSearch_SearchPercentHandler;
- textSearch.SearchCancelHandler -= TextSearch_SearchCancelHandler;
- SearchInput.SearchEvent += SearchInput_SearchEvent;
- SearchInput.ClearEvent += SearchInput_ClearEvent;
- textSearch.SearchCompletedHandler += TextSearch_SearchCompletedHandler;
- SearchResult.SelectionChanged += SearchResult_SelectionChanged;
- textSearch.SearchPercentHandler += TextSearch_SearchPercentHandler;
- textSearch.SearchCancelHandler += TextSearch_SearchCancelHandler;
- SearchInput.MoveResultEvent -= SearchInput_MoveResultEvent;
- SearchInput.MoveResultEvent += SearchInput_MoveResultEvent;
- SearchTog.IsChecked = true;
- }
- private void SearchInput_MoveResultEvent(object sender, CPDFSearchInputUI.MoveDirection e)
- {
- if (keyWord != SearchInput.SearchKeyWord)
- {
- keyWord = SearchInput.SearchKeyWord;
- ViewControl?.PDFViewTool.StartFindText(keyWord, searchOption);
- SearchResult.ClearSearchResult();
- }
- if (e == CPDFSearchInputUI.MoveDirection.Previous)
- {
- ViewControl?.PDFViewTool.FindPrevious();
- }
- else
- {
- ViewControl?.PDFViewTool.FindNext();
- }
- }
- private void TextSearch_SearchPercentHandler(object sender, TextSearchResult e)
- {
- Dispatcher.Invoke(() =>
- {
- if (!e.Items.ContainsKey(e.CurrentPage))
- {
- return;
- }
- ProgressBar.ProgressValue = e.CurrentPage + 1;
- foreach (var item in e.Items[e.CurrentPage])
- {
- if (item == null)
- {
- continue;
- }
- var result = new BindSearchResult()
- {
- PageIndex = item.PageIndex,
- TextContent = item.TextContent,
- TextRect = item.TextRect,
- SearchWord = keyWord,
- HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00),
- PageRotate = item.PageRotate
- };
- SearchResult.AddSearchResult(result);
- ResultNum.Text = SearchResult.ResultListControl.Items.Count.ToString();
- }
- });
- }
- private void SearchInput_ClearEvent(object sender, EventArgs e)
- {
- if (textSearch.CanDoSearch)
- {
- Dispatcher.Invoke(ClearSearchResult);
- }
- else
- {
- isClearResult = true;
- textSearch.CancleSearch();
- }
- ProgressBorder.Visibility = Visibility.Hidden;
- }
- private void TextSearch_SearchCancelHandler(object sender, TextSearchResult e)
- {
- if (isClearResult)
- {
- Dispatcher.Invoke(ClearSearchResult);
- isClearResult = false;
- }
- }
- private void SearchInput_FindPreviousEvent(object sender, EventArgs e)
- {
- if (currentHighLightIndex > 0)
- {
- currentHighLightIndex--;
- BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
- SearchResult.ClearSearchResult();
- }
- }
- private void SearchInput_FindNextEvent(object sender, EventArgs e)
- {
- currentHighLightIndex++;
- if (currentHighLightIndex >= 0)
- {
- BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
- SearchResult.ClearSearchResult();
- }
- }
- private void SearchResult_SelectionChanged(object sender, int e)
- {
- if (e < 0)
- {
- return;
- }
- currentHighLightIndex = e;
- BindSearchResult result = SearchResult.GetSelectItem();
- HighLightSelectResult(result);
- ResultNum.Text = "";
- ResultText.Text = LanguageHelper.BotaManager.GetString("Text_Result") + (e + 1) + "/" + SearchResult.ResultListControl.Items.Count;
- }
- private void TextSearch_SearchCompletedHandler(object sender, TextSearchResult e)
- {
- Dispatcher.Invoke(() =>
- {
- List<BindSearchResult> resultList = new List<BindSearchResult>();
- List<TextSearchItem> totalItems = new List<TextSearchItem>();
- foreach (int pageIndex in e.Items.Keys)
- {
- List<TextSearchItem> textSearchItems = e.Items[pageIndex];
- foreach (TextSearchItem item in textSearchItems)
- {
- resultList.Add(new BindSearchResult()
- {
- PageIndex = item.PageIndex,
- TextContent = item.TextContent,
- TextRect = item.TextRect,
- SearchWord = keyWord,
- HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00),
- PageRotate = item.PageRotate,
- RefData=item
- });
- totalItems.Add(item);
- }
- }
- SearchResult.SetSearchResult(resultList);
- ResultCount=resultList.Count;
- ViewControl.PDFViewTool?.SetPageSelectText(totalItems);
- });
- }
- private void SearchInput_SearchEvent(object sender, string e)
- {
- if (string.IsNullOrEmpty(e))
- {
- return;
- }
- if (pdfView == null || pdfView.GetDocument() == null)
- {
- return;
- }
- if (textSearch == null || !textSearch.CanDoSearch)
- {
- return;
- }
- if (SearchTog.IsChecked == true)
- {
- if (ViewControl.PDFViewTool.IsDocumentModified)
- {
- if (pdfView.GetDocument().FilePath != string.Empty)
- {
- pdfView.GetDocument().WriteToLoadedPath();
- }
- }
- keyWord = e;
- textSearch.TextSearchDocument = pdfView.GetDocument();
- SearchResult.ClearSearchResult();
- ResultNum.Text = "0";
- ResultText.Text = LanguageHelper.BotaManager.GetString("Tip_Result");
- ProgressBar.ProgressMaxValue = pdfView.GetDocument().PageCount;
- ProgressBorder.Visibility = Visibility.Visible;
-
- keyWord = e;
- textSearch.TextSearchDocument = pdfView.GetDocument();
- C_Search_Options options = C_Search_Options.Search_Case_Insensitive;
- if(!CaseChb.IsChecked)
- {
- options |= C_Search_Options.Search_Case_Sensitive;
- }
- if(MatchChb.IsChecked)
- {
- options |= C_Search_Options.Search_Match_Whole_Word;
- }
- textSearch.SearchText(e, options, ViewControl.Password);
- }
- else if (ReplaceTog.IsChecked == true)
- {
- SearchInput_MoveResultEvent(null, CPDFSearchInputUI.MoveDirection.Next);
- }
- }
- private void HighLightSelectResult(BindSearchResult result)
- {
- if (result == null || result.RefData==null)
- {
- return;
- }
- List<TextSearchItem> selectList = new List<TextSearchItem>();
- TextSearchItem highlightItem=result.RefData as TextSearchItem;
- if(highlightItem != null)
- {
- highlightItem.CreatePaintBrush(highLightBrush.Color);
- selectList.Add(highlightItem);
- ViewControl.PDFToolManager?.HighLightSearchText(selectList);
- }
- }
- private void ClearSearchResult()
- {
- SearchResult?.ClearSearchResult();
- ResultNum.Text = string.Empty;
- ResultText.Text = string.Empty;
- SearchInput.SearchKeyWord = string.Empty;
- }
- private void SearchCancel_Click(object sender, RoutedEventArgs e)
- {
- textSearch.CancleSearch();
- }
- private void SearchTog_Checked(object sender, RoutedEventArgs e)
- {
- if (ViewControl.PDFViewTool.IsDocumentModified)
- {
- if (pdfView.GetDocument().FilePath != string.Empty)
- {
- pdfView.GetDocument().WriteToLoadedPath();
- ViewControl.PDFViewTool.IsDocumentModified = false;
- }
- }
- ReplaceTog.IsChecked = false;
- SearchInput.InputGrid.RowDefinitions[1].Height = new GridLength(0);
- SearchInput.Height = 40;
- ReplaceBorder.Visibility = Visibility.Collapsed;
- SearchResult.Visibility = Visibility.Visible;
- ResultBorder.Visibility = Visibility.Visible;
- }
- private void ReplaceTog_Checked(object sender, RoutedEventArgs e)
- {
- if (ViewControl.PDFViewTool.IsDocumentModified)
- {
- pdfView.GetDocument().WriteToLoadedPath();
- ViewControl.PDFViewTool.IsDocumentModified = false;
- }
- SearchTog.IsChecked = false;
- SearchInput.InputGrid.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Star);
- SearchInput.Height = 80;
- ReplaceBorder.Visibility = Visibility.Visible;
- SearchResult.Visibility = Visibility.Collapsed;
- ResultBorder.Visibility = Visibility.Collapsed;
- keyWord = string.Empty;
- }
- private void ReplaceCurrent_Click(object sender, RoutedEventArgs e)
- {
- ViewControl?.PDFViewTool.ReplaceText(SearchInput.ReplaceWord);
- }
- private void ReplaceAll_Click(object sender, RoutedEventArgs e)
- {
- keyWord = SearchInput.SearchKeyWord;
- ViewControl?.PDFViewTool.StartFindText(keyWord, searchOption);
- ViewControl?.PDFViewTool.ReplaceAllText(SearchInput.ReplaceWord);
- }
- }
- }
|