CreateCustomizeTool.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. internal int ErasePageIndex { get; set; }
  63. public SolidColorBrush FillBrush;
  64. public Pen DrawPen;
  65. public event EventHandler<List<AnnotParam>> DeleteChanged;
  66. protected DrawingContext drawDC { get; set; }
  67. private CustomizeToolType customizeToolType { get; set; } = CustomizeToolType.kUnknown;
  68. private CPDFPage currentPage = null;
  69. private AnnotLayer annotLayer { get; set; } = null;
  70. private double defEraseThickness = 5;
  71. private double eraseZoom = 1;
  72. private SolidColorBrush eraseBrush { get; set; } = new SolidColorBrush(Colors.LightGray);
  73. internal CPDFViewer PDFViewer { get; set; }
  74. public CreateCustomizeTool()
  75. {
  76. }
  77. public void SetDefEraseThickness(double defEraseThickness)
  78. {
  79. this.defEraseThickness = defEraseThickness;
  80. }
  81. public void SetEraseZoom(double eraseZoom)
  82. {
  83. this.eraseZoom = eraseZoom;
  84. }
  85. public void SetEraseBrush(SolidColorBrush drawBrush)
  86. {
  87. if(drawBrush == null)
  88. {
  89. return;
  90. }
  91. eraseBrush = drawBrush;
  92. }
  93. public void SetAnnotLayer(AnnotLayer layer)
  94. {
  95. annotLayer = layer;
  96. }
  97. public void StartDraw(Point downPoint, CPDFPage cPDFPage, Rect maxRect, Rect pageBound, CustomizeToolType ToolType)
  98. {
  99. customizeToolType = ToolType;
  100. mouseStartPoint = downPoint;
  101. isMouseDown = true;
  102. this.maxRect = maxRect;
  103. this.pageBound = pageBound;
  104. DPIRect = new Rect();
  105. currentPage = cPDFPage;
  106. }
  107. public void MoveDraw(Point downPoint, double zoom)
  108. {
  109. if (isMouseDown)
  110. {
  111. mouseEndPoint = downPoint;
  112. zoomFactor = zoom;
  113. DrawTool();
  114. }
  115. }
  116. public void EndDraw()
  117. {
  118. if (isMouseDown)
  119. {
  120. isMouseDown = false;
  121. mouseStartPoint = new Point();
  122. mouseEndPoint = new Point();
  123. pageBound = new Rect();
  124. DPIRect = new Rect();
  125. currentPage = null;
  126. annotLayer = null;
  127. }
  128. }
  129. public void DrawTool()
  130. {
  131. Dispatcher.Invoke(() =>
  132. {
  133. drawDC = Open();
  134. switch (customizeToolType)
  135. {
  136. case CustomizeToolType.kUnknown:
  137. break;
  138. case CustomizeToolType.kErase:
  139. DrawErase(drawDC);
  140. break;
  141. default:
  142. break;
  143. }
  144. Present();
  145. });
  146. }
  147. public void ClearDraw()
  148. {
  149. Open();
  150. Present();
  151. }
  152. /// <summary>
  153. /// Erase the hand-drawn
  154. /// </summary>
  155. /// <returns>
  156. /// 1 delete 0 erase -1 not intersect
  157. /// </returns>
  158. private int ErasePoint(InkAnnot AnnotCore, Rect eraseRect)
  159. {
  160. Rect rawErase = eraseRect;
  161. List<List<Point>> addPointList = new List<List<Point>>();
  162. List<List<Point>> RawPointList = new List<List<Point>>();
  163. CPDFInkAnnotation AnnotInk = (AnnotCore.GetAnnotData().Annot as CPDFInkAnnotation);
  164. bool isErasePoint = false;
  165. if (AnnotInk.InkPath != null)
  166. {
  167. foreach (var Item in AnnotInk.InkPath)
  168. {
  169. List<Point> PointList = new List<Point>();
  170. foreach (var RawPoint in Item)
  171. {
  172. PointList.Add(DpiHelper.PDFPointToStandardPoint(new Point(RawPoint.x, RawPoint.y)));
  173. }
  174. RawPointList.Add(PointList);
  175. }
  176. }
  177. foreach (List<Point> Item in RawPointList)
  178. {
  179. List<Point> addItem = new List<Point>();
  180. foreach (Point checkPoint in Item)
  181. {
  182. if (rawErase.Contains(checkPoint) == false)
  183. {
  184. addItem.Add(checkPoint);
  185. }
  186. else
  187. {
  188. isErasePoint = true;
  189. if (addItem.Count > 2)
  190. {
  191. addPointList.Add(addItem);
  192. }
  193. addItem = new List<Point>();
  194. }
  195. }
  196. if (addItem.Count > 2)
  197. {
  198. addPointList.Add(addItem);
  199. }
  200. }
  201. RawPointList = addPointList;
  202. if (addPointList.Count == 0)
  203. {
  204. //delete annot
  205. return 1;
  206. }
  207. List<List<CPoint>> inkPathList = new List<List<CPoint>>();
  208. CPDFInkAnnotation annotInk = (AnnotCore.GetAnnotData().Annot as CPDFInkAnnotation);
  209. foreach (List<Point> inkNode in RawPointList)
  210. {
  211. List<CPoint> inkPath = new List<CPoint>();
  212. foreach (Point addPoint in inkNode)
  213. {
  214. inkPath.Add(new CPoint((float)DpiHelper.StandardNumToPDFNum(addPoint.X), (float)DpiHelper.StandardNumToPDFNum(addPoint.Y)));
  215. }
  216. inkPathList.Add(inkPath);
  217. }
  218. if (isErasePoint)
  219. {
  220. if (!annotInk.IsValid())
  221. {
  222. return -1;
  223. }
  224. annotInk.SetInkPath(inkPathList);
  225. annotInk.UpdateAp();
  226. AnnotCore.Draw();
  227. }
  228. return isErasePoint ? 0 : -1;
  229. }
  230. private void DrawErase(DrawingContext drawingContext)
  231. {
  232. Rect drawRect = new Rect(mouseEndPoint.X - defEraseThickness* eraseZoom, mouseEndPoint.Y - defEraseThickness * eraseZoom, defEraseThickness * 2* eraseZoom, defEraseThickness * 2* eraseZoom);
  233. Rect eraseRect = new Rect((drawRect.Left - pageBound.Left)/zoomFactor,
  234. (drawRect.Top - pageBound.Top)/zoomFactor,
  235. drawRect.Width / zoomFactor,
  236. drawRect.Height / zoomFactor);
  237. drawingContext?.DrawEllipse(eraseBrush, null, new Point(mouseEndPoint.X, mouseEndPoint.Y), defEraseThickness* eraseZoom, defEraseThickness* eraseZoom);
  238. if (annotLayer==null)
  239. {
  240. return;
  241. }
  242. List<BaseAnnot> annotControlList= annotLayer.GetAnnotListForType(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
  243. GroupHistory historyGroup=new GroupHistory();
  244. CPDFDocument pdfDoc = PDFViewer?.GetDocument();
  245. List<AnnotParam> paramList = new List<AnnotParam>();
  246. List<AnnotParam> paramNewList = new List<AnnotParam>();
  247. PDFViewer.GetPointPageInfo(mouseEndPoint, out int index, out Rect checkpaintRect, out Rect checkpageBound);
  248. foreach (var item in annotControlList)
  249. {
  250. AnnotData coreAnnotData = item.GetAnnotData();
  251. if (coreAnnotData == null || coreAnnotData.PageIndex != ErasePageIndex)
  252. {
  253. continue;
  254. }
  255. InkAnnot ink = item as InkAnnot;
  256. int Tag = ErasePoint(ink, eraseRect);
  257. if (Tag == 1)
  258. {
  259. CPDFAnnotation delAnnot = item.GetAnnotData().Annot;
  260. AnnotHistory annotHistory = ParamConverter.CreateHistory(delAnnot);
  261. AnnotParam annotParam = null;
  262. AnnotParam annotNewParam = null;
  263. if (pdfDoc != null)
  264. {
  265. annotParam = ParamConverter.CPDFDataConverterToAnnotParam(pdfDoc, delAnnot.Page.PageIndex, delAnnot);
  266. annotNewParam= ParamConverter.CPDFDataConverterToAnnotParam(pdfDoc, delAnnot.Page.PageIndex, delAnnot);
  267. annotHistory.CurrentParam = annotParam;
  268. annotHistory.Action=HistoryAction.Remove;
  269. annotHistory.PDFDoc=pdfDoc;
  270. historyGroup.Histories.Add(annotHistory);
  271. }
  272. if (delAnnot.RemoveAnnot())
  273. {
  274. if(annotParam != null)
  275. {
  276. paramList.Add(annotParam);
  277. paramNewList.Add(annotNewParam);
  278. }
  279. historyGroup.Histories.Add(annotHistory);
  280. }
  281. }
  282. }
  283. if(historyGroup.Histories.Count > 0 && PDFViewer!=null)
  284. {
  285. PDFViewer.UndoManager.AddHistory(historyGroup);
  286. }
  287. if(paramList.Count > 0)
  288. {
  289. DeleteChanged?.Invoke(this, paramNewList);
  290. }
  291. }
  292. }
  293. }