123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using ComPDFKit.PDFPage;
- using ComPDFKit.Tool;
- using Compdfkit_Tools.PDFControlUI;
- using ComPDFKitViewer;
- using System;
- using System.Collections.Generic;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- namespace Compdfkit_Tools.PDFControl
- {
- public partial class CPDFSearchControl : UserControl
- {
- /// <summary>
- /// PDFViewer
- /// </summary>
- private CPDFViewer pdfView;
- private int currentHighLightIndex { get; set; } = -1;
- private PDFTextSearch textSearch;
- private string keyWord;
- private SolidColorBrush highLightBrush = new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00));
- private int ResultCount = 0;
- private PDFViewControl ViewControl;
- public CPDFSearchControl()
- {
- InitializeComponent();
- Loaded += PDFSearch_Loaded;
- }
- 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;
- }
- private void SearchInput_ClearEvent(object sender, EventArgs e)
- {
- ClearSearchResult();
- }
- private void SearchInput_FindPreviousEvent(object sender, EventArgs e)
- {
- if (currentHighLightIndex > 0)
- {
- currentHighLightIndex--;
- BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
- HighLightSelectResult(result);
- }
- }
- private void SearchInput_FindNextEvent(object sender, EventArgs e)
- {
- currentHighLightIndex++;
- if (currentHighLightIndex >= 0)
- {
- BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
- HighLightSelectResult(result);
- }
- }
- private void SearchResult_SelectionChanged(object sender, int e)
- {
- currentHighLightIndex = e;
- BindSearchResult result = SearchResult.GetSelectItem();
- HighLightSelectResult(result);
- ResultText.Text = string.Format("Results:{0}/{1}", e+1,ResultCount);
- }
- 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);
- if (resultList==null || resultList.Count==0)
- {
- ResultText.Text= string.Empty;
- }
- else
- {
- ResultText.Text = string.Format("{0} results found", ResultCount);
- }
- });
- }
- 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)
- {
- keyWord = e;
- textSearch.TextSearchDocument = pdfView.GetDocument();
- textSearch.SearchText(e, C_Search_Options.Search_Case_Insensitive);
- }
- }
- 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.PDFViewTool?.HighLightSearchText(selectList);
- }
- }
- private void ClearSearchResult()
- {
- SearchResult?.SetSearchResult(null);
- ResultText.Text = string.Empty;
- SearchInput.SearchKeyWord=string.Empty;
- }
- }
- }
|