123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Office.Model;
- using PDF_Office.Views.BOTA;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Data;
- using System.Windows.Media;
- using System.Windows.Threading;
- namespace PDF_Office.ViewModels.BOTA
- {
- class SearchContentViewModel : BindableBase, INavigationAware
- {
- #region 属性
- public PDFTextSearch textSearch;
- private ObservableCollection<TextSearchBindItem> _searchResults = new ObservableCollection<TextSearchBindItem>();
- public ObservableCollection<TextSearchBindItem> searchResults
- {
- get { return _searchResults; }
- set { SetProperty(ref _searchResults, value); }
- }
- public List<TextSearchBindItem> lists;
- private string _searchContent = "";
- public string SearchContent
- {
- get { return _searchContent; }
- set { SetProperty(ref _searchContent, value); }
- }
- private int _searchCount = 0;
- public int SearchCount
- {
- get { return _searchCount; }
- set { SetProperty(ref _searchCount, value); }
- }
- private bool _isEmpty = true;
- public bool IsEmpty
- {
- get { return _isEmpty; }
- set { SetProperty(ref _isEmpty, value); }
- }
- #endregion
- #region 事件
- public DelegateCommand<object> SearchTextCommand { get; set; }
- public DelegateCommand<object> SearchChangedCommand { get; set; }
- #endregion
- public SearchContentViewModel()
- {
- InitVariable();
- InitCommand();
- BindEvent();
- lists = new List<TextSearchBindItem>();
- }
- private void InitVariable()
- {
- textSearch = new PDFTextSearch();
-
- ICollectionView groupView = CollectionViewSource.GetDefaultView(searchResults);
- groupView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(TextSearchBindItem.ShowPageIndex)));
- }
- private void InitCommand()
- {
- SearchTextCommand = new DelegateCommand<object>(SearchText);
- SearchChangedCommand = new DelegateCommand<object>(SearchChanged);
- }
- private void BindEvent()
- {
- textSearch.SearchPercentHandler += TextSearch_SearchPercentHandler;
- textSearch.SearchCompletedHandler += TextSearch_SearchCompletedHandler;
- }
- private void TextSearch_SearchCompletedHandler(object sender, TextSearchResult e)
- {
- issearched = true;
- TotalCount = e.TotalCount;
- }
- bool issearched = true;
- int TotalCount = 0;
- private bool cancelTask = false;
- private void TextSearch_SearchPercentHandler(object sender, TextSearchResult e)
- {
- if (cancelTask == false)
- {
- string keywords = SearchContent;
- if (e.Items.ContainsKey(e.CurrentPage))
- {
- {
- TextSearchBindItem addItem = new TextSearchBindItem();
- addItem.PageCount = e.Items[e.CurrentPage].Count;
- addItem.ShowPageIndex = e.CurrentPage;
- }
- {
- foreach (TextSearchItem item in e.Items[e.CurrentPage])
- {
- TextSearchBindItem addItem = new TextSearchBindItem();
- addItem.BindProperty.PageIndex = item.PageIndex;
- addItem.BindProperty.TextContent = item.TextContent;
- addItem.BindProperty.TextRect = item.TextRect;
- addItem.BindProperty.SearchWord = keywords;
- addItem.BindProperty.HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00);
- addItem.BindProperty.PageRotate = item.PageRotate;
- lists.Add(addItem);
- }
- }
- }
-
- }
- }
- private async void SearchText(object obj)
- {
- if (PDFViewer != null && textSearch != null)
- {
- issearched = false;
- C_Search_Options option = C_Search_Options.Search_Case_Insensitive;
- textSearch.TextSearchDocument = PDFViewer.Document;
- searchResults.Clear();
- lists.Clear();
- textSearch.SearchText(SearchContent, option);
- }
- while (issearched == false)
- await Task.Delay(10);
- foreach (var item in lists)
- searchResults.Add(item);
- SearchCount = TotalCount;
- if (TotalCount == 0)
- IsEmpty = true;
- else
- IsEmpty = false;
- }
- private void SearchChanged(object obj)
- {
- if (PDFViewer != null && textSearch != null)
- {
- TextSearchBindItem currentItem = obj as TextSearchBindItem;
- if (currentItem != null && PDFViewer != null)
- {
- List<TextSearchItem> pageTextList = new List<TextSearchItem>();
- pageTextList.Add(new TextSearchItem()
- {
- PageIndex = currentItem.BindProperty.PageIndex,
- TextRect = currentItem.BindProperty.TextRect,
- TextContent = currentItem.BindProperty.TextContent,
- PageRotate = currentItem.BindProperty.PageRotate
- });
- PDFViewer.SetPageSelectText(pageTextList, new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00)));
- }
- }
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- return;
- }
- private CPDFViewer PDFViewer;
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- if (PDFViewer != null)
- {
- }
- }
- }
- }
|