CPDFViewerTool.SelectedRect.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 ComPDFKit.Viewer.Layer;
  11. using System.Windows.Media;
  12. using ComPDFKit.Tool.Help;
  13. using ComPDFKitViewer;
  14. using ComPDFKit.PDFAnnotation;
  15. namespace ComPDFKit.Tool
  16. {
  17. public partial class CPDFViewerTool
  18. {
  19. bool isDrawSelectRect = false;
  20. int selectedRectViewTag = -1;
  21. private bool isOutSideScaling = false;
  22. public event EventHandler<SelectedAnnotData> SelectedDataChanging;
  23. public event EventHandler<SelectedAnnotData> SelectedDataChanged;
  24. private bool canRotateAnnot;
  25. private void InsertSelectedRectView()
  26. {
  27. int selectedRectViewIndex = PDFViewer.GetMaxViewIndex();
  28. CustomizeLayer customizeLayer = new CustomizeLayer();
  29. SelectedRect selectedRect = new SelectedRect(GetDefaultDrawParam(), SelectedType.Annot);
  30. selectedRect.CanRotate=canRotateAnnot;
  31. selectedRect.SetDrawMoveType(DrawMoveType.kDefault);
  32. customizeLayer.Children.Add(selectedRect);
  33. selectedRect.DataChanged += SelectedRect_DataChanged;
  34. selectedRect.DataChanging += SelectedRect_DataChanging;
  35. PDFViewer.InsertView(selectedRectViewIndex, customizeLayer);
  36. selectedRectViewTag= customizeLayer.GetResTag();
  37. }
  38. /// <summary>
  39. /// Set whether the border can be moved outside the border
  40. /// </summary>
  41. /// <param name="IsOutSideScaling"></param>
  42. public void SetOutSideScaling(bool IsOutSideScaling)
  43. {
  44. isOutSideScaling = IsOutSideScaling;
  45. }
  46. private void SelectedRect_DataChanging(object sender, SelectedAnnotData e)
  47. {
  48. SelectedDataChanging?.Invoke(this, e);
  49. }
  50. private void SelectedRect_DataChanged(object sender, SelectedAnnotData e)
  51. {
  52. SelectedDataChanged?.Invoke(this, e);
  53. }
  54. /// <summary>
  55. /// Start to draw the rectangle
  56. /// </summary>
  57. public void DrawStartSelectedRect()
  58. {
  59. Point point = Mouse.GetPosition(this);
  60. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  61. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  62. if (selectedRect != null)
  63. {
  64. selectedRect.Draw();
  65. selectedRect.OnMouseLeftButtonDown(point);
  66. isDrawSelectRect = true;
  67. }
  68. }
  69. public Cursor GetMoveSelectedRectCursor()
  70. {
  71. Point point = Mouse.GetPosition(this);
  72. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  73. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  74. if (selectedRect != null)
  75. {
  76. return selectedRect.GetCursor(point, this.Cursor);
  77. }
  78. return this.Cursor;
  79. }
  80. /// <summary>
  81. /// Draw the rectangle when dragging
  82. /// </summary>
  83. public bool DrawMoveSelectedRect()
  84. {
  85. bool DrawTag = false;
  86. Point point = Mouse.GetPosition(this);
  87. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  88. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  89. if (selectedRect != null)
  90. {
  91. selectedRect.SetOutSideScaling(isOutSideScaling);
  92. selectedRect.OnMouseMove(point, out DrawTag,PDFViewer.ActualWidth,PDFViewer.ActualHeight);
  93. selectedRect.Draw();
  94. }
  95. return DrawTag;
  96. }
  97. /// <summary>
  98. /// End of drawing the rectangle
  99. /// </summary>
  100. public void DrawEndSelectedRect()
  101. {
  102. Point point = Mouse.GetPosition(this);
  103. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  104. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  105. if (selectedRect != null)
  106. {
  107. selectedRect.OnMouseLeftButtonUp(point);
  108. selectedRect.Draw();
  109. }
  110. }
  111. /// <summary>
  112. /// Clear the rectangle drawing
  113. /// </summary>
  114. public void CleanSelectedRect()
  115. {
  116. Point point = Mouse.GetPosition(this);
  117. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  118. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  119. if (selectedRect != null)
  120. {
  121. selectedRect.ClearDraw();
  122. isDrawSelectRect = false;
  123. }
  124. }
  125. private void SelectedAnnot()
  126. {
  127. if (!isHitTestLink&& cacheHitTestAnnot?.CurrentType== C_ANNOTATION_TYPE.C_ANNOTATION_LINK)
  128. {
  129. return ;
  130. }
  131. if (isHitTestRedact && cacheHitTestAnnot?.CurrentType!= C_ANNOTATION_TYPE.C_ANNOTATION_REDACT)
  132. {
  133. return ;
  134. }
  135. SelectAnnot(cacheHitTestAnnot.GetAnnotData());
  136. }
  137. private void SelectAnnot(AnnotData selectData)
  138. {
  139. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  140. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  141. if (selectedRect != null)
  142. {
  143. selectedRect.SetAnnotData(selectData);
  144. }
  145. }
  146. private void SelectedAnnot(AnnotData annotData)
  147. {
  148. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  149. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  150. if (selectedRect != null)
  151. {
  152. if (annotData==null)
  153. {
  154. selectedRect.ClearDraw();
  155. }
  156. else
  157. {
  158. selectedRect.SetAnnotData(annotData);
  159. }
  160. }
  161. }
  162. /// <summary>
  163. /// Refresh the drawing
  164. /// </summary>
  165. private void DrawSelectedLayer()
  166. {
  167. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  168. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  169. if (selectedRect != null)
  170. {
  171. selectedRect.Draw();
  172. }
  173. }
  174. /// <summary>
  175. /// Identify whether the mouse is on the rectangle
  176. /// </summary>
  177. /// <returns></returns>
  178. private bool DrawSelectRectDownEvent()
  179. {
  180. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  181. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  182. if (selectedRect != null)
  183. {
  184. if (selectedRect.GetHitControlIndex(Mouse.GetPosition(this)) != PointControlType.None)
  185. {
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. public bool SetRotateAnnot(bool canRotate)
  192. {
  193. if (canRotateAnnot != canRotate)
  194. {
  195. canRotateAnnot = canRotate;
  196. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  197. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  198. selectedRect.CanRotate = canRotate;
  199. return true;
  200. }
  201. return false;
  202. }
  203. public bool GetRotateAnnot()
  204. {
  205. return canRotateAnnot;
  206. }
  207. public SelectedRect GetAnnotSelectRect()
  208. {
  209. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  210. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  211. return selectedRect;
  212. }
  213. }
  214. }