CPDFViewerTool.SelectText.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. (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);
  51. }
  52. public void DrawEndSelectText()
  53. {
  54. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  55. (baseLayer as SelectText).EndDraw();
  56. }
  57. public bool GetMousePointToTextSelectInfo()
  58. {
  59. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  60. TextSelectInfo selectTextInfo = (baseLayer as SelectText).GetTextSelectInfo();
  61. PDFViewer.GetMousePointToPage(out int pageindex, out Point pagepoint);
  62. bool PressOnSelectedText = false;
  63. if (selectTextInfo.PageSelectTextRectList.Count <= 0)
  64. {
  65. PressOnSelectedText = false;
  66. return PressOnSelectedText;
  67. }
  68. if (selectTextInfo.PageSelectTextRectList.ContainsKey(pageindex))
  69. {
  70. foreach (TextDrawRect textRect in selectTextInfo.PageSelectTextRectList[pageindex])
  71. {
  72. Rect RawPaintRect = new Rect(
  73. DpiHelper.PDFNumToStandardNum(textRect.DrawRect.Left) * currentZoom,
  74. DpiHelper.PDFNumToStandardNum(textRect.DrawRect.Top) * currentZoom,
  75. DpiHelper.PDFNumToStandardNum(textRect.DrawRect.Width) * currentZoom,
  76. DpiHelper.PDFNumToStandardNum(textRect.DrawRect.Height) * currentZoom);
  77. if (RawPaintRect.Contains(pagepoint))
  78. {
  79. PressOnSelectedText = true;
  80. break;
  81. }
  82. }
  83. }
  84. return PressOnSelectedText;
  85. }
  86. public TextSelectInfo GetTextSelectInfo()
  87. {
  88. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  89. return (baseLayer as SelectText).GetTextSelectInfo();
  90. }
  91. public void ReDrawSelectText()
  92. {
  93. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  94. if ((baseLayer as SelectText).HasSelectTextInfo())
  95. {
  96. (baseLayer as SelectText).Draw(PDFViewer);
  97. }
  98. else
  99. {
  100. (baseLayer as SelectText).CleanDraw(PDFViewer);
  101. }
  102. }
  103. public void ReDrawSearchText()
  104. {
  105. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  106. if ((baseLayer as SelectText).HasSearchInfo())
  107. {
  108. (baseLayer as SelectText).Draw(PDFViewer);
  109. }
  110. else
  111. {
  112. (baseLayer as SelectText).CleanDraw(PDFViewer);
  113. }
  114. }
  115. public void RemoveSelectTextData()
  116. {
  117. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  118. if ((baseLayer as SelectText).HasSelectTextInfo())
  119. {
  120. (baseLayer as SelectText).RemoveSelectDataInfo();
  121. (baseLayer as SelectText).Draw(PDFViewer);
  122. }
  123. }
  124. /// <summary>
  125. /// Set all search results
  126. /// </summary>
  127. /// <param name="searchTexts"></param>
  128. public void SetPageSelectText(List<TextSearchItem> searchTexts)
  129. {
  130. if (searchTexts.Count > 0)
  131. {
  132. TextSelectInfo searchInfo = new TextSelectInfo();
  133. searchInfo.StartPage = searchTexts.Min(x => x.PageIndex);
  134. searchInfo.EndPage = searchTexts.Max(x => x.PageIndex);
  135. searchInfo.PageRotate = searchTexts[0].PageRotate;
  136. searchInfo.RotateRecord = true;
  137. List<int> pageIndexList = (from u in searchTexts select u.PageIndex).Distinct().ToList();
  138. foreach (int pageIndex in pageIndexList)
  139. {
  140. List<TextSearchItem> pageTexts = searchTexts.Where(x => x.PageIndex == pageIndex).ToList();
  141. foreach (TextSearchItem textItem in pageTexts)
  142. {
  143. if (!searchInfo.PageSelectTextRectList.ContainsKey(pageIndex))
  144. {
  145. searchInfo.PageSelectTextRectList[pageIndex] = new List<TextDrawRect>();
  146. }
  147. searchInfo.PageSelectTextRectList[pageIndex].Add(new TextDrawRect()
  148. {
  149. Text = textItem.TextContent,
  150. DrawRect = textItem.TextRect,
  151. PaintBrush = textItem.PaintBrush,
  152. DrawActiveSearch = false,
  153. SearchInfo = textItem
  154. });
  155. }
  156. }
  157. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  158. (baseLayer as SelectText).SetSearchInfo(searchInfo);
  159. }
  160. }
  161. /// <summary>
  162. /// Clear the previously cached search results
  163. /// </summary>
  164. public void CleanSearchInfo()
  165. {
  166. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  167. (baseLayer as SelectText).CleanSearchInfo();
  168. }
  169. public void HighLightSearchText(List<TextSearchItem> selectTexts)
  170. {
  171. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectTextTag);
  172. if ((baseLayer as SelectText).HasSearchInfo())
  173. {
  174. TextSelectInfo searchTextInfo = (baseLayer as SelectText).GetSearchInfo();
  175. if (searchTextInfo.PageSelectTextRectList != null)
  176. {
  177. foreach (int pageIndex in searchTextInfo.PageSelectTextRectList.Keys)
  178. {
  179. List<TextDrawRect> drawSearchList = searchTextInfo.PageSelectTextRectList[pageIndex];
  180. foreach (TextDrawRect drawRect in drawSearchList)
  181. {
  182. drawRect.DrawActiveSearch = selectTexts.Contains(drawRect.SearchInfo);
  183. if (drawRect.DrawActiveSearch)
  184. {
  185. drawRect.PaintBrush = drawRect.SearchInfo.PaintBrush;
  186. }
  187. else
  188. {
  189. drawRect.PaintBrush = drawRect.SearchInfo.BorderBrush;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }