CreateWidgetTool.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFAnnotation.Form;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.Viewer.Layer;
  5. using ComPDFKitViewer.BaseObject;
  6. using ComPDFKitViewer.Helper;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Net.NetworkInformation;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Media;
  15. using ComPDFKit.Tool.SettingParam;
  16. using ComPDFKit.Tool.Help;
  17. using System.Windows.Annotations;
  18. namespace ComPDFKit.Tool.DrawTool
  19. {
  20. internal class CreateWidgetTool : CustomizeLayer
  21. {
  22. protected CPDFAnnotation cPDFAnnotation { get; set; }
  23. /// <summary>
  24. /// End point of the drawing
  25. /// </summary>
  26. protected Point mouseEndPoint { get; set; }
  27. /// <summary>
  28. /// Crop point
  29. /// </summary>
  30. protected Point cropPoint { get; set; }
  31. protected bool hideDraw { get; set; } = false;
  32. /// <summary>
  33. ///Identify whether the annotation is being created
  34. /// </summary>
  35. protected bool isDrawAnnot { get; set; }
  36. /// <summary>
  37. /// Max drawing range rectangle
  38. /// </summary>
  39. protected Rect maxRect { get; set; }
  40. /// <summary>
  41. /// Start drawing point
  42. /// </summary>
  43. protected Point mouseStartPoint { get; set; }
  44. /// <summary>
  45. /// Original page range rectangle (calculate offset in continuous mode)
  46. /// </summary>
  47. protected Rect pageBound { get; set; }
  48. /// <summary>
  49. /// Standard DPI rectangle (without removing half of the pen thickness)
  50. /// </summary>
  51. protected Rect DPIRect { get; set; }
  52. /// <summary>
  53. /// Used to set the current zoom ratio
  54. /// </summary>
  55. private double zoomFactor { get; set; } = 1;
  56. protected DrawingContext drawDC { get; set; }
  57. private double minWidth = 15;
  58. private double minHeight = 15;
  59. private double defaultWidth { get; set; } = 0;
  60. private double defaultHeight { get; set; } = 0;
  61. private double PDFViewerActualWidth { get; set; } = 0;
  62. private double PDFViewerActualHeight { get; set; } = 0;
  63. private C_WIDGET_TYPE currentWidgetType = C_WIDGET_TYPE.WIDGET_UNKNOWN;
  64. protected DefaultDrawParam drawParam = new DefaultDrawParam();
  65. public void SetDrawType(C_WIDGET_TYPE WidgetType)
  66. {
  67. currentWidgetType = WidgetType;
  68. }
  69. public void ReDrawWidget(double zoom)
  70. {
  71. zoomFactor = zoom;
  72. Draw();
  73. }
  74. public CPDFAnnotation StartDraw(Point downPoint, Point cropPoint, CPDFPage cPDFPage, Rect maxRect, Rect pageBound, C_WIDGET_TYPE WidgetType)
  75. {
  76. if (WidgetType== C_WIDGET_TYPE.WIDGET_NONE)
  77. {
  78. return null;
  79. }
  80. mouseStartPoint = downPoint;
  81. isDrawAnnot = true;
  82. this.maxRect = maxRect;
  83. int newIndex=cPDFPage.GetAnnotCount();
  84. cPDFAnnotation = cPDFPage.CreateWidget(WidgetType);
  85. if(cPDFAnnotation!=null)
  86. {
  87. cPDFAnnotation.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  88. cPDFAnnotation.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  89. List<CPDFAnnotation> annotList= cPDFPage.GetAnnotations();
  90. cPDFAnnotation = annotList[newIndex];
  91. }
  92. this.cropPoint = cropPoint;
  93. this.pageBound = pageBound;
  94. DPIRect = new Rect();
  95. return cPDFAnnotation;
  96. }
  97. public void MoveDraw(Point downPoint, double zoom, double width, double height, bool Hide, Rect maxRect)
  98. {
  99. hideDraw = Hide;
  100. PDFViewerActualWidth = width;
  101. PDFViewerActualHeight = height;
  102. mouseEndPoint = downPoint;
  103. zoomFactor = zoom;
  104. if (!isDrawAnnot)
  105. {
  106. this.maxRect = maxRect;
  107. }
  108. Draw();
  109. }
  110. public Rect EndDraw()
  111. {
  112. if (isDrawAnnot)
  113. {
  114. Rect rect = DPIRect;
  115. if (rect.Width<=1&& rect.Height <= 1)
  116. {
  117. rect = new Rect(mouseStartPoint.X, mouseStartPoint.Y, defaultWidth * zoomFactor, defaultHeight * zoomFactor);
  118. }
  119. if (rect.Width < minWidth)
  120. {
  121. rect.Width = defaultWidth * zoomFactor;
  122. }
  123. if (rect.Height < minHeight)
  124. {
  125. rect.Height = defaultHeight * zoomFactor;
  126. }
  127. rect.Intersect(maxRect);
  128. Rect StandardRect = new Rect(
  129. (rect.Left - pageBound.X + (cropPoint.X * zoomFactor)) / zoomFactor,
  130. (rect.Top - pageBound.Y + (cropPoint.Y * zoomFactor)) / zoomFactor,
  131. rect.Width / zoomFactor, rect.Height / zoomFactor);
  132. isDrawAnnot = false;
  133. mouseStartPoint = new Point();
  134. mouseEndPoint = new Point();
  135. pageBound = new Rect();
  136. DPIRect = new Rect();
  137. cPDFAnnotation = null;
  138. return DpiHelper.StandardRectToPDFRect(StandardRect);
  139. }
  140. return new Rect();
  141. }
  142. public override void Draw()
  143. {
  144. Dispatcher.Invoke(() =>
  145. {
  146. drawDC = Open();
  147. if (hideDraw||currentWidgetType == C_WIDGET_TYPE.WIDGET_NONE||(cPDFAnnotation == null && isDrawAnnot))
  148. {
  149. Present();
  150. return;
  151. }
  152. Point DrawPoint = new Point();
  153. if (isDrawAnnot)
  154. {
  155. DrawPoint = mouseStartPoint;
  156. }
  157. else
  158. {
  159. DrawPoint = mouseEndPoint;
  160. }
  161. if (!maxRect.Contains(DrawPoint))
  162. {
  163. Present();
  164. return;
  165. }
  166. drawDC?.DrawLine(drawParam.CreateWidgetPen, new Point(0, DrawPoint.Y), new Point(PDFViewerActualWidth, DrawPoint.Y));
  167. drawDC?.DrawLine(drawParam.CreateWidgetPen, new Point(DrawPoint.X, 0), new Point(DrawPoint.X, PDFViewerActualHeight));
  168. if (!isDrawAnnot)
  169. {
  170. switch (currentWidgetType)
  171. {
  172. case C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  173. DefaultPushButton();
  174. break;
  175. case C_WIDGET_TYPE.WIDGET_CHECKBOX:
  176. case C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
  177. DefaultRadioButtonOrCheckBox();
  178. break;
  179. case C_WIDGET_TYPE.WIDGET_TEXTFIELD:
  180. case C_WIDGET_TYPE.WIDGET_COMBOBOX:
  181. DefaultTextBoxOrComboBox();
  182. break;
  183. case C_WIDGET_TYPE.WIDGET_LISTBOX:
  184. DefaultListBox();
  185. break;
  186. case C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
  187. DefaultSign();
  188. break;
  189. case C_WIDGET_TYPE.WIDGET_UNKNOWN:
  190. break;
  191. default:
  192. break;
  193. }
  194. Rect rect = new Rect(mouseEndPoint.X, mouseEndPoint.Y, defaultWidth * zoomFactor, defaultHeight * zoomFactor);
  195. DPIRect = rect;
  196. drawDC?.DrawRectangle(null, drawParam.CreateWidgetPen, rect);
  197. }
  198. else
  199. {
  200. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  201. double mLeft = rect.Left;
  202. double mRight = rect.Right;
  203. double mUp = rect.Top;
  204. double mDown = rect.Bottom;
  205. if (rect.Left < maxRect.Left)
  206. {
  207. mLeft = maxRect.Left;
  208. }
  209. if (rect.Right > maxRect.Right)
  210. {
  211. mRight = maxRect.Right;
  212. }
  213. if (rect.Top < maxRect.Top)
  214. {
  215. mUp = maxRect.Top;
  216. }
  217. if (rect.Bottom > maxRect.Bottom)
  218. {
  219. mDown = maxRect.Bottom;
  220. }
  221. DPIRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  222. drawDC?.DrawRectangle(null, drawParam.CreateWidgetPen, DPIRect);
  223. }
  224. Present();
  225. });
  226. }
  227. private void DefaultRadioButtonOrCheckBox()
  228. {
  229. defaultWidth = 30;
  230. defaultHeight = 30;
  231. }
  232. private void DefaultTextBoxOrComboBox()
  233. {
  234. defaultWidth = 200;
  235. defaultHeight = 40;
  236. }
  237. private void DefaultListBox()
  238. {
  239. defaultWidth = 200;
  240. defaultHeight = 130;
  241. }
  242. private void DefaultPushButton()
  243. {
  244. defaultWidth = 200;
  245. defaultHeight = 50;
  246. }
  247. private void DefaultSign()
  248. {
  249. defaultWidth = 200;
  250. defaultHeight = 80;
  251. }
  252. public virtual void ClearDraw()
  253. {
  254. Open();
  255. Present();
  256. }
  257. }
  258. }