AnnotSelector.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.Layer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Media;
  11. namespace ComPDFKit.Tool.DrawTool
  12. {
  13. internal class AnnotSelector:BaseLayer
  14. {
  15. public CPDFViewer PDFViewer { get; set; }
  16. public List<AnnotData> SelectAnnots { get; set; } = new List<AnnotData>();
  17. public Pen DrawPen { get; set; }
  18. public Brush DrawBrush { get; set; }
  19. private List<Point> ControlPoints { get; set; } = new List<Point>();
  20. private double RectPadding = 4;
  21. public AnnotSelector()
  22. {
  23. DrawPen = new Pen(new SolidColorBrush(Color.FromArgb(255, 71, 126, 222)), 2);
  24. DrawBrush = new SolidColorBrush(Color.FromArgb(40, 119, 180, 227));
  25. }
  26. public override void Draw()
  27. {
  28. DrawingContext drawDC = Open();
  29. DrawOutBound(drawDC);
  30. DrawIndividual(drawDC);
  31. DrawControlPoint(drawDC);
  32. Present();
  33. }
  34. public int GetSelectCount()
  35. {
  36. if(SelectAnnots!=null)
  37. {
  38. return SelectAnnots.Count;
  39. }
  40. return 0;
  41. }
  42. public bool AddItem(AnnotData item)
  43. {
  44. if (item == null)
  45. {
  46. return false;
  47. }
  48. List<int> pageIndexList = SelectAnnots.Select(x => x.PageIndex).Distinct().ToList();
  49. if(pageIndexList.Count > 1 )
  50. {
  51. SelectAnnots.Clear();
  52. }
  53. if( pageIndexList.Count==1 && pageIndexList[0]!=item.PageIndex)
  54. {
  55. SelectAnnots.Clear();
  56. }
  57. if (RemoveItem(item))
  58. {
  59. SelectAnnots.Add(item);
  60. return true;
  61. }
  62. return false;
  63. }
  64. public bool HasItem(AnnotData item)
  65. {
  66. if (item == null)
  67. {
  68. return false;
  69. }
  70. if(SelectAnnots!=null && SelectAnnots.Count > 0)
  71. {
  72. return SelectAnnots.Where(x => x.AnnotIndex == item.AnnotIndex && x.PageIndex == item.PageIndex).Count() > 0;
  73. }
  74. return false;
  75. }
  76. public bool RemoveItem(AnnotData item)
  77. {
  78. if (item == null || item.PageIndex < 0 || item.AnnotIndex < 0)
  79. {
  80. return false;
  81. }
  82. List<AnnotData> checkList = SelectAnnots.Where(x => x.AnnotIndex == item.AnnotIndex && x.PageIndex == item.PageIndex).ToList();
  83. if (checkList != null && checkList.Count > 0)
  84. {
  85. foreach (AnnotData removeItem in checkList)
  86. {
  87. SelectAnnots.Remove(removeItem);
  88. }
  89. }
  90. return true;
  91. }
  92. public void ClearItem()
  93. {
  94. SelectAnnots?.Clear();
  95. }
  96. public Rect GetOutBoundRect()
  97. {
  98. if (SelectAnnots.Count > 0)
  99. {
  100. double left = 0;
  101. double top = 0;
  102. double right = 0;
  103. double bottom = 0;
  104. for (int i=0;i< SelectAnnots.Count;i++)
  105. {
  106. AnnotData checkItem= SelectAnnots[i];
  107. if (i == 0)
  108. {
  109. left=checkItem.PaintRect.Left;
  110. top=checkItem.PaintRect.Top;
  111. right=checkItem.PaintRect.Right;
  112. bottom=checkItem.PaintRect.Bottom;
  113. continue;
  114. }
  115. left = Math.Min(left, checkItem.PaintRect.Left);
  116. top = Math.Min(top, checkItem.PaintRect.Top);
  117. right = Math.Max(right, checkItem.PaintRect.Right);
  118. bottom = Math.Max(bottom, checkItem.PaintRect.Bottom);
  119. }
  120. if(right<left || bottom<top)
  121. {
  122. return Rect.Empty;
  123. }
  124. return new Rect(left, top, right-left, bottom-top);
  125. }
  126. return Rect.Empty;
  127. }
  128. public void DrawOutBound(DrawingContext drawDC)
  129. {
  130. Rect boudRect = GetOutBoundRect();
  131. if (boudRect.IsEmpty || boudRect.Width == 0 || boudRect.Height == 0)
  132. {
  133. return;
  134. }
  135. drawDC.DrawRectangle(Brushes.Transparent, DrawPen, boudRect);
  136. }
  137. public void DrawIndividual(DrawingContext drawDC)
  138. {
  139. if(SelectAnnots==null || SelectAnnots.Count==0)
  140. {
  141. return;
  142. }
  143. foreach(AnnotData checkItem in SelectAnnots)
  144. {
  145. drawDC.DrawRectangle(null, DrawPen, checkItem.PaintRect);
  146. }
  147. }
  148. public void DrawControlPoint(DrawingContext drawDC)
  149. {
  150. Rect boudRect = GetOutBoundRect();
  151. ControlPoints.Clear();
  152. double centerX = (boudRect.Left + boudRect.Right) / 2;
  153. double centerY = (boudRect.Top + boudRect.Bottom) / 2;
  154. ControlPoints.Add(new Point(boudRect.Left, boudRect.Top));
  155. ControlPoints.Add(new Point(boudRect.Left, centerY));
  156. ControlPoints.Add(new Point(boudRect.Left, boudRect.Bottom));
  157. ControlPoints.Add(new Point(centerX, boudRect.Bottom));
  158. ControlPoints.Add(new Point(boudRect.Right, boudRect.Bottom));
  159. ControlPoints.Add(new Point(boudRect.Right, centerY));
  160. ControlPoints.Add(new Point(boudRect.Right, boudRect.Top));
  161. ControlPoints.Add(new Point(centerX, boudRect.Top));
  162. foreach (Point point in ControlPoints)
  163. {
  164. drawDC.DrawRectangle(Brushes.White, DrawPen, new Rect(point.X- RectPadding,point.Y- RectPadding, RectPadding*2, RectPadding*2));
  165. }
  166. }
  167. public PointControlType GetHitControlIndex(Point point)
  168. {
  169. for (int i = 0; i < ControlPoints.Count; i++)
  170. {
  171. Point checkPoint = ControlPoints[i];
  172. Rect checkBound = new Rect(checkPoint.X - RectPadding, checkPoint.Y - RectPadding, RectPadding * 2, RectPadding * 2);
  173. if (checkBound.Contains(point))
  174. {
  175. return (PointControlType)i;
  176. }
  177. }
  178. Rect outBound = GetOutBoundRect();
  179. if (outBound.Contains(point))
  180. {
  181. return PointControlType.Body;
  182. }
  183. return PointControlType.None;
  184. }
  185. }
  186. }