CPDFViewerTool.SelectText.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using ComPDFKit.Tool.DrawTool;
  2. using ComPDFKitViewer.Layer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Input;
  9. using System.Windows;
  10. using ComPDFKitViewer.Helper;
  11. using ComPDFKitViewer;
  12. using ComPDFKit.Tool.Help;
  13. using System.Windows.Media;
  14. using System.Xml.Linq;
  15. namespace ComPDFKit.Tool
  16. {
  17. public partial class CPDFViewerTool
  18. {
  19. int selectTextTag = -1;
  20. private void InsertSelectTextView()
  21. {
  22. SelectText createAnnotTool = new SelectText();
  23. int SelectTextindex = PDFViewer.GetMaxViewIndex();
  24. PDFViewer.InsertView(SelectTextindex, createAnnotTool);
  25. selectTextTag = createAnnotTool.GetResTag();
  26. }
  27. public bool IsText()
  28. {
  29. PDFViewer.GetMousePointToPage(out int pageindex, out Point pagepoint);
  30. return PDFHelp.IsTextAtPos(PDFViewer.GetDocument(), pageindex, new Point(DpiHelper.StandardNumToPDFNum(pagepoint.X / PDFViewer.CurrentRenderFrame.ZoomFactor), DpiHelper.StandardNumToPDFNum(pagepoint.Y / PDFViewer.CurrentRenderFrame.ZoomFactor)));
  31. }
  32. public void DrawStartSelectText()
  33. {
  34. if (PDFViewer.CurrentRenderFrame == null)
  35. {
  36. return;
  37. }
  38. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  39. PDFViewer.GetMousePointToPage(out int pageindex, out Point pagepoint);
  40. (baseLayer as SelectText).StartDraw(new Point(DpiHelper.StandardNumToPDFNum(pagepoint.X / PDFViewer.CurrentRenderFrame.ZoomFactor), DpiHelper.StandardNumToPDFNum(pagepoint.Y / PDFViewer.CurrentRenderFrame.ZoomFactor)), pageindex);
  41. }
  42. public void DrawMoveSelectText(bool DoubleClick)
  43. {
  44. if (PDFViewer.CurrentRenderFrame == null)
  45. {
  46. return;
  47. }
  48. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  49. PDFViewer.GetMousePointToPage(out int pageindex, out Point pagepoint);
  50. if(pageindex>=0)
  51. {
  52. (baseLayer as SelectText).MoveDraw(new Point(DpiHelper.StandardNumToPDFNum(pagepoint.X / PDFViewer.CurrentRenderFrame.ZoomFactor), DpiHelper.StandardNumToPDFNum(pagepoint.Y / PDFViewer.CurrentRenderFrame.ZoomFactor)), pageindex, PDFViewer, new Point(10, 10), DoubleClick);
  53. }
  54. }
  55. public void DrawEndSelectText()
  56. {
  57. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  58. (baseLayer as SelectText).EndDraw();
  59. }
  60. public bool GetMousePointToTextSelectInfo()
  61. {
  62. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  63. TextSelectInfo selectTextInfo = (baseLayer as SelectText).GetTextSelectInfo();
  64. PDFViewer.GetMousePointToPage(out int pageindex, out Point pagepoint);
  65. bool PressOnSelectedText = false;
  66. if (selectTextInfo.PageSelectTextRectList.Count <= 0)
  67. {
  68. PressOnSelectedText = false;
  69. return PressOnSelectedText;
  70. }
  71. if (selectTextInfo.PageSelectTextRectList.ContainsKey(pageindex))
  72. {
  73. foreach (TextDrawRect textRect in selectTextInfo.PageSelectTextRectList[pageindex])
  74. {
  75. Rect RawPaintRect = new Rect(
  76. DpiHelper.PDFNumToStandardNum(textRect.DrawRect.Left) * currentZoom,
  77. DpiHelper.PDFNumToStandardNum(textRect.DrawRect.Top) * currentZoom,
  78. DpiHelper.PDFNumToStandardNum(textRect.DrawRect.Width) * currentZoom,
  79. DpiHelper.PDFNumToStandardNum(textRect.DrawRect.Height) * currentZoom);
  80. if (RawPaintRect.Contains(pagepoint))
  81. {
  82. PressOnSelectedText = true;
  83. break;
  84. }
  85. }
  86. }
  87. return PressOnSelectedText;
  88. }
  89. public TextSelectInfo GetTextSelectInfo()
  90. {
  91. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  92. return (baseLayer as SelectText).GetTextSelectInfo();
  93. }
  94. public void ReDrawSelectText()
  95. {
  96. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  97. if ((baseLayer as SelectText).HasSelectTextInfo())
  98. {
  99. (baseLayer as SelectText).Draw(PDFViewer);
  100. }
  101. else
  102. {
  103. (baseLayer as SelectText).CleanDraw(PDFViewer);
  104. }
  105. }
  106. public void ReDrawSearchText()
  107. {
  108. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  109. if ((baseLayer as SelectText).HasSearchInfo())
  110. {
  111. (baseLayer as SelectText).Draw(PDFViewer);
  112. }
  113. else
  114. {
  115. (baseLayer as SelectText).CleanDraw(PDFViewer);
  116. }
  117. }
  118. public void RemoveSelectTextData()
  119. {
  120. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  121. if ((baseLayer as SelectText).HasSelectTextInfo())
  122. {
  123. (baseLayer as SelectText).RemoveSelectDataInfo();
  124. (baseLayer as SelectText).Draw(PDFViewer);
  125. }
  126. }
  127. /// <summary>
  128. /// Set all search results
  129. /// </summary>
  130. /// <param name="searchTexts"></param>
  131. public void SetPageSelectText(List<TextSearchItem> searchTexts)
  132. {
  133. if (searchTexts.Count > 0)
  134. {
  135. TextSelectInfo searchInfo = new TextSelectInfo();
  136. searchInfo.StartPage = searchTexts.Min(x => x.PageIndex);
  137. searchInfo.EndPage = searchTexts.Max(x => x.PageIndex);
  138. searchInfo.PageRotate = searchTexts[0].PageRotate;
  139. searchInfo.RotateRecord = true;
  140. List<int> pageIndexList = (from u in searchTexts select u.PageIndex).Distinct().ToList();
  141. foreach (int pageIndex in pageIndexList)
  142. {
  143. List<TextSearchItem> pageTexts = searchTexts.Where(x => x.PageIndex == pageIndex).ToList();
  144. foreach (TextSearchItem textItem in pageTexts)
  145. {
  146. if (!searchInfo.PageSelectTextRectList.ContainsKey(pageIndex))
  147. {
  148. searchInfo.PageSelectTextRectList[pageIndex] = new List<TextDrawRect>();
  149. }
  150. searchInfo.PageSelectTextRectList[pageIndex].Add(new TextDrawRect()
  151. {
  152. Text = textItem.TextContent,
  153. DrawRect = textItem.TextRect,
  154. PaintBrush = textItem.PaintBrush,
  155. DrawActiveSearch = false,
  156. SearchInfo = textItem
  157. });
  158. }
  159. }
  160. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  161. (baseLayer as SelectText).SetSearchInfo(searchInfo);
  162. }
  163. }
  164. /// <summary>
  165. /// Clear the previously cached search results
  166. /// </summary>
  167. public void CleanSearchInfo()
  168. {
  169. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  170. (baseLayer as SelectText).CleanSearchInfo();
  171. }
  172. public void HighLightSearchText(List<TextSearchItem> selectTexts)
  173. {
  174. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  175. if ((baseLayer as SelectText).HasSearchInfo())
  176. {
  177. TextSelectInfo searchTextInfo = (baseLayer as SelectText).GetSearchInfo();
  178. if (searchTextInfo.PageSelectTextRectList != null)
  179. {
  180. foreach (int pageIndex in searchTextInfo.PageSelectTextRectList.Keys)
  181. {
  182. List<TextDrawRect> drawSearchList = searchTextInfo.PageSelectTextRectList[pageIndex];
  183. foreach (TextDrawRect drawRect in drawSearchList)
  184. {
  185. drawRect.DrawActiveSearch = selectTexts.Contains(drawRect.SearchInfo);
  186. if (drawRect.DrawActiveSearch)
  187. {
  188. drawRect.PaintBrush = drawRect.SearchInfo.PaintBrush;
  189. }
  190. else
  191. {
  192. drawRect.PaintBrush = drawRect.SearchInfo.BorderBrush;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }