using ComPDFKit.PDFPage; using ComPDFKitViewer.AnnotEvent; using ComPDFKitViewer.PdfViewer; using PDF_Master.Model; using PDF_Master.Model.BOTA; using PDF_Master.Properties; using PDF_Master.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; using System.Windows.Data; using System.Windows.Media; using System.Windows.Threading; namespace PDF_Master.ViewModels.BOTA { class SearchContentViewModel : BindableBase, INavigationAware { #region 属性 private Visibility completed; public Visibility Completed { get { return completed; } set { SetProperty(ref completed, value); } } private string searchCount; public string SearchCount { get { return searchCount; } set { SetProperty(ref searchCount, value); } } private ObservableCollection searchItemList; public ObservableCollection SearchItemList { get { return searchItemList; } set { SetProperty(ref searchItemList, value); } } private PDFTextSearch textSearch; private CPDFViewer PDFViewer; private ViewContentViewModel viewContentViewModel; private string CurrentSearchText = ""; private ObservableCollection myVar; public ObservableCollection MyProperty { get { return myVar; } set { SetProperty(ref myVar, value); } } private bool caseInsensitive = true; public bool CaseInsensitive { get { return caseInsensitive; } set { SetProperty(ref caseInsensitive, value); } } public DelegateCommand SearchChangedCommand { get; set; } public DelegateCommand CleanCommand { get; set; } private string searchTextContext; public string SearchTextContext { get { return searchTextContext; } set { SetProperty(ref searchTextContext, value); } } #endregion public SearchContentViewModel() { textSearch = new PDFTextSearch(); SearchItemList = new ObservableCollection(); MyProperty = new ObservableCollection() { "1", "2" }; SearchChangedCommand = new DelegateCommand(SearchChanged); CleanCommand = new DelegateCommand(clean); ICollectionView groupView = CollectionViewSource.GetDefaultView(SearchItemList); groupView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SearchItem.ShowPageIndex))); textSearch.SearchPercentHandler += TextSearch_SearchPercentHandler; textSearch.SearchCompletedHandler += TextSearch_SearchCompletedHandler; Completed = Visibility.Collapsed; } private void TextSearch_SearchCompletedHandler(object sender, TextSearchResult e) { App.Current.Dispatcher.Invoke(() => { Completed = Visibility.Visible; SearchCount = e.TotalCount.ToString(); List pageTextList = new List(); foreach (var item in SearchItemList) { item.TextProperty.SearchItem.PaintBrush = new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00)); pageTextList.Add(item.TextProperty.SearchItem); } PDFViewer.SetPageSelectText(pageTextList); }); } private void TextSearch_SearchPercentHandler(object sender, TextSearchResult e) { App.Current.Dispatcher.Invoke(() => { if (e.Items.ContainsKey(e.CurrentPage)) { foreach (TextSearchItem item in e.Items[e.CurrentPage]) { SearchItem addItem = new SearchItem(); addItem.TextProperty = new TextBindProperty(); addItem.TextProperty.PageIndex = item.PageIndex; addItem.TextProperty.TextContent = item.TextContent; addItem.TextProperty.TextRect = item.TextRect; addItem.TextProperty.SearchWord = CurrentSearchText; addItem.TextProperty.HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00); addItem.TextProperty.PageRotate = item.PageRotate; addItem.TextProperty.SearchItem = item; SearchItemList.Add(addItem); } } SearchCount = e.TotalCount.ToString(); }); } /// /// 文字搜索 /// public async void SearchText(string Text) { //防止正在进行搜索,先取消一次。(若没有搜索,该方法不进行操作) textSearch.CancleSearch(); //为了防止死循环,当超过十次,即0.1秒还是不能进行搜索,则取消本次搜索 int counter = 0; while (!textSearch.CanDoSearch) { await Task.Delay(10); if (counter > 10) { return; } } //成功取消后,清除之前的结果,并搜索新内容 SearchItemList.Clear(); Completed = Visibility.Collapsed; if (CaseInsensitive) { textSearch.SearchText(Text, C_Search_Options.Search_Case_Insensitive,viewContentViewModel.PassWord); } else { textSearch.SearchText(Text, C_Search_Options.Search_Case_Sensitive, viewContentViewModel.PassWord); } CurrentSearchText = Text; } /// /// 根据UI的Tag与绑定的对象来创建注释的方法 /// public void CreateAnnotate(object item, string Tag) { SearchItem searchItem = item as SearchItem; if (searchItem == null) { return; } switch (Tag) { case "Round": CircleAnnotArgs circleannotArgs = new CircleAnnotArgs(); circleannotArgs.ClientRect = Helper.DpiHelpers.GetRawRelatedRect(searchItem.TextProperty.TextRect); circleannotArgs.Transparency = 1; circleannotArgs.BgColor = Colors.Transparent; circleannotArgs.LineColor = Colors.Red; circleannotArgs.LineWidth = 2; PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, circleannotArgs); break; case "Rectangle": SquareAnnotArgs squareAnnotArgs = new SquareAnnotArgs(); squareAnnotArgs.ClientRect = Helper.DpiHelpers.GetRawRelatedRect(searchItem.TextProperty.TextRect); squareAnnotArgs.Transparency = 1; squareAnnotArgs.BgColor = Colors.Transparent; squareAnnotArgs.LineColor = Colors.Red; squareAnnotArgs.LineWidth = 2; PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, squareAnnotArgs); break; case "Highlight": TextHighlightAnnotArgs highlightAnnotArgs = new TextHighlightAnnotArgs(); highlightAnnotArgs.Color = Settings.Default.AppProperties.Annotate.HighLightColor; highlightAnnotArgs.Transparency = 1; highlightAnnotArgs.PagesHiglightRectList[searchItem.TextProperty.PageIndex] = new List(); highlightAnnotArgs.PagesHiglightRectList[searchItem.TextProperty.PageIndex].Add(searchItem.TextProperty.TextRect); PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, highlightAnnotArgs); break; case "Underline": TextUnderlineAnnotArgs underlineAnnotArgs = new TextUnderlineAnnotArgs(); underlineAnnotArgs.Color = Settings.Default.AppProperties.Annotate.UnderLineColor; underlineAnnotArgs.Transparency = 1; underlineAnnotArgs.PagesUnderlineRectList[searchItem.TextProperty.PageIndex] = new List(); underlineAnnotArgs.PagesUnderlineRectList[searchItem.TextProperty.PageIndex].Add(searchItem.TextProperty.TextRect); PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, underlineAnnotArgs); break; case "Strikethrough": TextStrikeoutAnnotArgs strikeoutAnnotArgs = new TextStrikeoutAnnotArgs(); strikeoutAnnotArgs.Color = Settings.Default.AppProperties.Annotate.StrikethroughColor; strikeoutAnnotArgs.Transparency = 1; strikeoutAnnotArgs.PagesStrikeoutRectList[searchItem.TextProperty.PageIndex] = new List(); strikeoutAnnotArgs.PagesStrikeoutRectList[searchItem.TextProperty.PageIndex].Add(searchItem.TextProperty.TextRect); PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, strikeoutAnnotArgs); break; default: break; } } /// /// 选项改变时,跳转并且高亮搜索内容 /// private void SearchChanged(object obj) { if (PDFViewer != null && textSearch != null) { SearchItem currentItem = obj as SearchItem; if (currentItem != null && PDFViewer != null) { List pageTextList = new List(); currentItem.TextProperty.SearchItem.BorderBrush = new SolidColorBrush(Color.FromArgb(0xFF, 0x17, 0x70, 0xF4)); currentItem.TextProperty.SearchItem.Padding = new Thickness(3); currentItem.TextProperty.SearchItem.BorderThickness = 1; pageTextList.Add(currentItem.TextProperty.SearchItem); PDFViewer.HighLightSearchText(pageTextList); } } } private void clean() { SearchTextContext = ""; PDFViewer.ClearPageSelectText(); SearchItemList.Clear(); viewContentViewModel.OpenBOTA = false; } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { return; } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); if (PDFViewer != null) { textSearch.TextSearchDocument = PDFViewer.Document; } navigationContext.Parameters.TryGetValue(ParameterNames.ViewContentViewModel, out viewContentViewModel); } } }