CreateCustomizeTool.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.PDFPage;
  5. using ComPDFKit.Tool.Help;
  6. using ComPDFKit.Tool.UndoManger;
  7. using ComPDFKit.Viewer.Layer;
  8. using ComPDFKitViewer;
  9. using ComPDFKitViewer.Annot;
  10. using ComPDFKitViewer.BaseObject;
  11. using ComPDFKitViewer.Helper;
  12. using ComPDFKitViewer.Layer;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Net;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using System.Windows.Media;
  21. namespace ComPDFKit.Tool.DrawTool
  22. {
  23. public enum CustomizeToolType
  24. {
  25. kUnknown,
  26. kErase,
  27. }
  28. internal class CreateCustomizeTool : CustomizeLayer
  29. {
  30. /// <summary>
  31. /// Start drawing point
  32. /// </summary>
  33. protected Point mouseStartPoint { get; set; }
  34. /// <summary>
  35. /// End drawing point
  36. /// </summary>
  37. protected Point mouseEndPoint { get; set; }
  38. /// <summary>
  39. /// Identifies whether the mouse is pressed
  40. /// </summary>
  41. protected bool isMouseDown { get; set; }
  42. /// <summary>
  43. /// Zoom factor
  44. /// </summary>
  45. private double zoomFactor { get; set; } = 1;
  46. /// <summary>
  47. /// Rect for drawing
  48. /// </summary>
  49. protected Rect drawRect { get; set; }
  50. /// <summary>
  51. /// Max drawing rect
  52. /// </summary>
  53. protected Rect maxRect { get; set; }
  54. /// <summary>
  55. /// Original page range rectangle (calculate offset in continuous mode)
  56. /// </summary>
  57. protected Rect pageBound { get; set; }
  58. /// <summary>
  59. /// Standard DPI rectangle (without removing half of the pen thickness)
  60. /// </summary>
  61. protected Rect DPIRect { get; set; }
  62. public SolidColorBrush FillBrush;
  63. public Pen DrawPen;
  64. public event EventHandler<List<AnnotParam>> DeleteChanged;
  65. protected DrawingContext drawDC { get; set; }
  66. private CustomizeToolType customizeToolType { get; set; } = CustomizeToolType.kUnknown;
  67. private CPDFPage currentPage = null;
  68. private AnnotLayer annotLayer { get; set; } = null;
  69. private double defEraseThickness = 5;
  70. private double eraseZoom = 1;
  71. private SolidColorBrush eraseBrush { get; set; } = new SolidColorBrush(Colors.LightGray);
  72. internal CPDFViewer PDFViewer { get; set; }
  73. public CreateCustomizeTool()
  74. {
  75. }
  76. public void SetDefEraseThickness(double defEraseThickness)
  77. {
  78. this.defEraseThickness = defEraseThickness;
  79. }
  80. public void SetEraseZoom(double eraseZoom)
  81. {
  82. this.eraseZoom = eraseZoom;
  83. }
  84. public void SetEraseBrush(SolidColorBrush drawBrush)
  85. {
  86. if(drawBrush == null)
  87. {
  88. return;
  89. }
  90. eraseBrush = drawBrush;
  91. }
  92. public void SetAnnotLayer(AnnotLayer layer)
  93. {
  94. annotLayer = layer;
  95. }
  96. public void StartDraw(Point downPoint, CPDFPage cPDFPage, Rect maxRect, Rect pageBound, CustomizeToolType ToolType)
  97. {
  98. customizeToolType = ToolType;
  99. mouseStartPoint = downPoint;
  100. isMouseDown = true;
  101. this.maxRect = maxRect;
  102. this.pageBound = pageBound;
  103. DPIRect = new Rect();
  104. currentPage = cPDFPage;
  105. }
  106. public void MoveDraw(Point downPoint, double zoom)
  107. {
  108. if (isMouseDown)
  109. {
  110. mouseEndPoint = downPoint;
  111. zoomFactor = zoom;
  112. DrawTool();
  113. }
  114. }
  115. public void EndDraw()
  116. {
  117. if (isMouseDown)
  118. {
  119. isMouseDown = false;
  120. mouseStartPoint = new Point();
  121. mouseEndPoint = new Point();
  122. pageBound = new Rect();
  123. DPIRect = new Rect();
  124. currentPage = null;
  125. annotLayer = null;
  126. }
  127. }
  128. public void DrawTool()
  129. {
  130. Dispatcher.Invoke(() =>
  131. {
  132. drawDC = Open();
  133. switch (customizeToolType)
  134. {
  135. case CustomizeToolType.kUnknown:
  136. break;
  137. case CustomizeToolType.kErase:
  138. DrawErase(drawDC);
  139. break;
  140. default:
  141. break;
  142. }
  143. Present();
  144. });
  145. }
  146. public void ClearDraw()
  147. {
  148. Open();
  149. Present();
  150. }
  151. /// <summary>
  152. /// Erase the hand-drawn
  153. /// </summary>
  154. /// <returns>
  155. /// 1 delete 0 erase -1 not intersect
  156. /// </returns>
  157. private int ErasePoint(InkAnnot AnnotCore, Rect eraseRect)
  158. {
  159. Rect rawErase = eraseRect;
  160. List<List<Point>> addPointList = new List<List<Point>>();
  161. List<List<Point>> RawPointList = new List<List<Point>>();
  162. CPDFInkAnnotation AnnotInk = (AnnotCore.GetAnnotData().Annot as CPDFInkAnnotation);
  163. bool isErasePoint = false;
  164. if (AnnotInk.InkPath != null)
  165. {
  166. foreach (var Item in AnnotInk.InkPath)
  167. {
  168. List<Point> PointList = new List<Point>();
  169. foreach (var RawPoint in Item)
  170. {
  171. PointList.Add(DpiHelper.PDFPointToStandardPoint(new Point(RawPoint.x, RawPoint.y)));
  172. }
  173. RawPointList.Add(PointList);
  174. }
  175. }
  176. foreach (List<Point> Item in RawPointList)
  177. {
  178. List<Point> addItem = new List<Point>();
  179. foreach (Point checkPoint in Item)
  180. {
  181. if (rawErase.Contains(checkPoint) == false)
  182. {
  183. addItem.Add(checkPoint);
  184. }
  185. else
  186. {
  187. isErasePoint = true;
  188. if (addItem.Count > 2)
  189. {
  190. addPointList.Add(addItem);
  191. }
  192. addItem = new List<Point>();
  193. }
  194. }
  195. if (addItem.Count > 2)
  196. {
  197. addPointList.Add(addItem);
  198. }
  199. }
  200. RawPointList = addPointList;
  201. if (addPointList.Count == 0)
  202. {
  203. //delete annot
  204. return 1;
  205. }
  206. List<List<CPoint>> inkPathList = new List<List<CPoint>>();
  207. CPDFInkAnnotation annotInk = (AnnotCore.GetAnnotData().Annot as CPDFInkAnnotation);
  208. foreach (List<Point> inkNode in RawPointList)
  209. {
  210. List<CPoint> inkPath = new List<CPoint>();
  211. foreach (Point addPoint in inkNode)
  212. {
  213. inkPath.Add(new CPoint((float)DpiHelper.StandardNumToPDFNum(addPoint.X), (float)DpiHelper.StandardNumToPDFNum(addPoint.Y)));
  214. }
  215. inkPathList.Add(inkPath);
  216. }
  217. if (isErasePoint)
  218. {
  219. if (!annotInk.IsValid())
  220. {
  221. return -1;
  222. }
  223. annotInk.SetInkPath(inkPathList);
  224. annotInk.UpdateAp();
  225. AnnotCore.Draw();
  226. }
  227. return isErasePoint ? 0 : -1;
  228. }
  229. private void DrawErase(DrawingContext drawingContext)
  230. {
  231. Rect drawRect = new Rect(mouseEndPoint.X - defEraseThickness* eraseZoom, mouseEndPoint.Y - defEraseThickness * eraseZoom, defEraseThickness * 2* eraseZoom, defEraseThickness * 2* eraseZoom);
  232. Rect eraseRect = new Rect((drawRect.Left - pageBound.Left)/zoomFactor,
  233. (drawRect.Top - pageBound.Top)/zoomFactor,
  234. drawRect.Width / zoomFactor,
  235. drawRect.Height / zoomFactor);
  236. drawingContext?.DrawEllipse(eraseBrush, null, new Point(mouseEndPoint.X, mouseEndPoint.Y), defEraseThickness* eraseZoom, defEraseThickness* eraseZoom);
  237. if (annotLayer==null)
  238. {
  239. return;
  240. }
  241. List<BaseAnnot> annotControlList= annotLayer.GetAnnotListForType(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
  242. GroupHistory historyGroup=new GroupHistory();
  243. CPDFDocument pdfDoc = PDFViewer?.GetDocument();
  244. List<AnnotParam> paramList = new List<AnnotParam>();
  245. foreach (var item in annotControlList)
  246. {
  247. InkAnnot ink = item as InkAnnot;
  248. int Tag = ErasePoint(ink, eraseRect);
  249. if (Tag == 1)
  250. {
  251. CPDFAnnotation delAnnot = item.GetAnnotData().Annot;
  252. AnnotHistory annotHistory = ParamConverter.CreateHistory(delAnnot);
  253. AnnotParam annotParam = null;
  254. if (pdfDoc != null)
  255. {
  256. annotParam = ParamConverter.CPDFDataConverterToAnnotParam(pdfDoc, delAnnot.Page.PageIndex, delAnnot);
  257. annotHistory.CurrentParam = annotParam;
  258. annotHistory.Action=HistoryAction.Remove;
  259. annotHistory.PDFDoc=pdfDoc;
  260. historyGroup.Histories.Add(annotHistory);
  261. }
  262. if (delAnnot.RemoveAnnot())
  263. {
  264. if(annotParam != null)
  265. {
  266. paramList.Add(annotParam);
  267. }
  268. historyGroup.Histories.Add(annotHistory);
  269. }
  270. }
  271. }
  272. if(historyGroup.Histories.Count > 0 && PDFViewer!=null)
  273. {
  274. PDFViewer.UndoManager.AddHistory(historyGroup);
  275. }
  276. if(paramList.Count > 0)
  277. {
  278. DeleteChanged?.Invoke(this, paramList);
  279. }
  280. }
  281. }
  282. }