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 _searchResults = new ObservableCollection(); public ObservableCollection searchResults { get { return _searchResults; } set { SetProperty(ref _searchResults, value); } } public List 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 SearchTextCommand { get; set; } public DelegateCommand SearchChangedCommand { get; set; } #endregion public SearchContentViewModel() { InitVariable(); InitCommand(); BindEvent(); lists = new List(); } 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(SearchText); SearchChangedCommand = new DelegateCommand(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 pageTextList = new List(); 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(ParameterNames.PDFViewer, out PDFViewer); if (PDFViewer != null) { } } } }