123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Office.Model;
- using PDF_Office.Model.BOTA;
- using PDF_Office.Properties;
- 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;
- using System.Windows.Data;
- using System.Windows.Media;
- using System.Windows.Threading;
- namespace PDF_Office.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<SearchItem> searchItemList;
- public ObservableCollection<SearchItem> SearchItemList
- {
- get { return searchItemList; }
- set
- {
- SetProperty(ref searchItemList, value);
- }
- }
- private PDFTextSearch textSearch;
- private CPDFViewer PDFViewer;
- private ViewContentViewModel viewContentViewModel;
- private string CurrentSearchText = "";
- private ObservableCollection<string> myVar;
- public ObservableCollection<string> 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<object> 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<SearchItem>();
- MyProperty = new ObservableCollection<string>() { "1", "2" };
- SearchChangedCommand = new DelegateCommand<object>(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<TextSearchItem> pageTextList = new List<TextSearchItem>();
- 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();
- });
- }
- /// <summary>
- /// 文字搜索
- /// </summary>
- 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);
- }
- else
- {
- textSearch.SearchText(Text, C_Search_Options.Search_Case_Sensitive);
- }
- CurrentSearchText = Text;
- }
- /// <summary>
- /// 根据UI的Tag与绑定的对象来创建注释的方法
- /// </summary>
- 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<Rect>();
- 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<Rect>();
- 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<Rect>();
- strikeoutAnnotArgs.PagesStrikeoutRectList[searchItem.TextProperty.PageIndex].Add(searchItem.TextProperty.TextRect);
- PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, strikeoutAnnotArgs);
- break;
- default:
- break;
- }
- }
- /// <summary>
- /// 选项改变时,跳转并且高亮搜索内容
- /// </summary>
- private void SearchChanged(object obj)
- {
- if (PDFViewer != null && textSearch != null)
- {
- SearchItem currentItem = obj as SearchItem;
- if (currentItem != null && PDFViewer != null)
- {
- List<TextSearchItem> pageTextList = new List<TextSearchItem>();
- currentItem.TextProperty.SearchItem.BorderBrush = new SolidColorBrush(Color.FromArgb(0xFF, 0x17, 0x70, 0xF4));
- currentItem.TextProperty.SearchItem.Padding = 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<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- if (PDFViewer != null)
- {
- textSearch.TextSearchDocument = PDFViewer.Document;
- }
- navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
- }
- }
- }
|