CPDFViewerTool.PageSelected.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.Tool.DrawTool;
  5. using ComPDFKit.Tool.Help;
  6. using ComPDFKit.Viewer.Layer;
  7. using ComPDFKitViewer;
  8. using ComPDFKitViewer.Helper;
  9. using ComPDFKitViewer.Layer;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Reflection;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Input;
  19. namespace ComPDFKit.Tool
  20. {
  21. public class PageSelectedData
  22. {
  23. public int PageIndex { get; set; }
  24. public Rect DrawRect { get; set; }
  25. public Rect RawRect { get; set; }
  26. public Rect SelectRect { get; set; }
  27. }
  28. public partial class CPDFViewerTool
  29. {
  30. int pageSelectedRectViewTag = -1;
  31. int pageIndex = -1;
  32. public event EventHandler<PageSelectedData> PageSelectedChanging;
  33. public event EventHandler<PageSelectedData> PageSelectedChanged;
  34. private void InsertPageSelectedRectView()
  35. {
  36. int selectedRectViewIndex = PDFViewer.GetMaxViewIndex();
  37. CustomizeLayer customizeLayer = new CustomizeLayer();
  38. PageSelectedRect pageSelectedRect = new PageSelectedRect(GetDefaultDrawParam());
  39. pageSelectedRect.SetDrawMoveType(DrawMoveType.kReferenceLine);
  40. customizeLayer.Children.Add(pageSelectedRect);
  41. pageSelectedRect.DataChanged += PageSelectedRect_DataChanged;
  42. pageSelectedRect.DataChanging += PageSelectedRect_DataChanging; ;
  43. PDFViewer.InsertView(selectedRectViewIndex, customizeLayer);
  44. pageSelectedRectViewTag = customizeLayer.GetResTag();
  45. }
  46. private void PageSelectedRect_DataChanging(object sender, Rect e)
  47. {
  48. PageSelectedRect pageSelectedRect = CommonHelper.FindVisualChild<PageSelectedRect>(PDFViewer.GetViewForTag(pageSelectedRectViewTag));
  49. if (pageSelectedRect != null)
  50. {
  51. PageSelectedData pageSelectedData = new PageSelectedData();
  52. pageSelectedData.PageIndex = pageIndex;
  53. pageSelectedData.DrawRect = e;
  54. PageSelectedChanged?.Invoke(this, pageSelectedData);
  55. }
  56. }
  57. private void PageSelectedRect_DataChanged(object sender, Rect e)
  58. {
  59. PageSelectedRect pageSelectedRect = CommonHelper.FindVisualChild<PageSelectedRect>(PDFViewer.GetViewForTag(pageSelectedRectViewTag));
  60. if (pageSelectedRect != null)
  61. {
  62. Canvas canvas = CommonHelper.FindVisualChild<Canvas>(pageSelectedRect);
  63. if (canvas != null)
  64. {
  65. UserControl userControl = CommonHelper.FindVisualChild<UserControl>(canvas);
  66. if (userControl != null)
  67. {
  68. userControl.Visibility = Visibility.Collapsed;
  69. if (e.Width > 0 && e.Height > 0)
  70. {
  71. userControl.Visibility = Visibility.Visible;
  72. userControl.Measure(pageSelectedRect.GetMaxRect().Size);
  73. Size desireSize = userControl.DesiredSize;
  74. if (e.Bottom + desireSize.Height + 3 > Math.Min(pageSelectedRect.GetMaxRect().Bottom, PDFViewer.ViewportHeight))
  75. {
  76. userControl.SetValue(Canvas.LeftProperty, e.Right - desireSize.Width);
  77. double topPos = (int)Math.Max(e.Top - desireSize.Height - 3, 0);
  78. userControl.SetValue(Canvas.TopProperty, topPos);
  79. }
  80. else
  81. {
  82. userControl.SetValue(Canvas.LeftProperty, e.Right - desireSize.Width);
  83. userControl.SetValue(Canvas.TopProperty, e.Bottom + 3);
  84. }
  85. if (e.Right - desireSize.Width < 0)
  86. {
  87. userControl.SetValue(Canvas.LeftProperty, 3D);
  88. }
  89. }
  90. }
  91. PageSelectedData pageSelectedData = new PageSelectedData();
  92. pageSelectedData.PageIndex = pageIndex;
  93. pageSelectedData.DrawRect = e;
  94. pageSelectedData.RawRect = Rect.Empty;
  95. pageSelectedData.SelectRect = Rect.Empty;
  96. try
  97. {
  98. Rect maxRect = pageSelectedRect.GetMaxRect();
  99. Rect pageRect = new Rect(e.X - maxRect.X, e.Y - maxRect.Y, e.Width, e.Height);
  100. pageSelectedData.SelectRect = pageRect;
  101. double zoom = PDFViewer.GetZoom();
  102. pageRect = new Rect(
  103. pageRect.X / zoom / 96D * 72D,
  104. pageRect.Y / zoom / 96D * 72D,
  105. pageRect.Width / zoom / 96D * 72D,
  106. pageRect.Height / zoom / 96D * 72D);
  107. pageSelectedData.RawRect = pageRect;
  108. }
  109. catch (Exception ex)
  110. {
  111. }
  112. PageSelectedChanged?.Invoke(this, pageSelectedData);
  113. }
  114. }
  115. }
  116. public void DrawStartPageSelectedRect()
  117. {
  118. PageSelectedRect pageSelectedRect = CommonHelper.FindVisualChild<PageSelectedRect>(PDFViewer.GetViewForTag(pageSelectedRectViewTag));
  119. if (pageSelectedRect != null)
  120. {
  121. Point point = Mouse.GetPosition(this);
  122. pageSelectedRect.Draw();
  123. pageSelectedRect.OnMouseLeftButtonDown(point);
  124. PDFViewer.CanHorizontallyScroll = false;
  125. PDFViewer.CanVerticallyScroll = false;
  126. PDFViewer.EnableZoom(false);
  127. }
  128. }
  129. public void DrawMovePageSelectedRect()
  130. {
  131. PageSelectedRect pageSelectedRect = CommonHelper.FindVisualChild<PageSelectedRect>(PDFViewer.GetViewForTag(pageSelectedRectViewTag));
  132. if (pageSelectedRect != null)
  133. {
  134. Point point = Mouse.GetPosition(this);
  135. pageSelectedRect.OnMouseMove(point, out bool Tag, PDFViewer.ActualWidth, PDFViewer.ActualHeight);
  136. }
  137. }
  138. public void DrawEndPageSelectedRect()
  139. {
  140. PageSelectedRect pageSelectedRect = CommonHelper.FindVisualChild<PageSelectedRect>(PDFViewer.GetViewForTag(pageSelectedRectViewTag));
  141. if (pageSelectedRect != null)
  142. {
  143. Point point = Mouse.GetPosition(this);
  144. pageSelectedRect.OnMouseLeftButtonUp(point);
  145. }
  146. }
  147. public void SelectedPageSelectedRect(Rect selectedRects, Rect MaxRect)
  148. {
  149. PageSelectedRect pageSelectedRect = CommonHelper.FindVisualChild<PageSelectedRect>(PDFViewer.GetViewForTag(pageSelectedRectViewTag));
  150. if (pageSelectedRect != null)
  151. {
  152. pageSelectedRect.SetRect(selectedRects);
  153. pageSelectedRect.SetMaxRect(MaxRect);
  154. pageSelectedRect.SetPDFViewerActualSize(PDFViewer.ActualWidth, PDFViewer.ActualHeight);
  155. pageSelectedRect.Draw();
  156. }
  157. }
  158. public bool HitTestPageSelectedRect()
  159. {
  160. PageSelectedRect pageSelectedRect = CommonHelper.FindVisualChild<PageSelectedRect>(PDFViewer.GetViewForTag(pageSelectedRectViewTag));
  161. if (pageSelectedRect != null)
  162. {
  163. if (pageSelectedRect.GetHitControlIndex(Mouse.GetPosition(this)) != PointControlType.None)
  164. {
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. public void CleanPageSelectedRect()
  171. {
  172. PageSelectedRect pageSelectedRect = CommonHelper.FindVisualChild<PageSelectedRect>(PDFViewer.GetViewForTag(pageSelectedRectViewTag));
  173. if (pageSelectedRect != null)
  174. {
  175. pageSelectedRect.ClearDraw();
  176. Canvas canvas = CommonHelper.FindVisualChild<Canvas>(pageSelectedRect);
  177. if (canvas != null)
  178. {
  179. UserControl userControl = CommonHelper.FindVisualChild<UserControl>(canvas);
  180. if (userControl != null)
  181. {
  182. userControl.Visibility=Visibility.Collapsed;
  183. }
  184. }
  185. pageIndex = -1;
  186. PDFViewer.CanHorizontallyScroll = true;
  187. PDFViewer.CanVerticallyScroll = true;
  188. PDFViewer.EnableZoom(true);
  189. }
  190. }
  191. public void CreatePageSelectdRect()
  192. {
  193. PageSelectedRect pageSelectedRect = CommonHelper.FindVisualChild<PageSelectedRect>(PDFViewer.GetViewForTag(pageSelectedRectViewTag));
  194. if (pageSelectedRect != null)
  195. {
  196. Point point = Mouse.GetPosition(this);
  197. BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
  198. PDFViewer.GetPointPageInfo(point, out int index, out Rect paintRect, out Rect pageBound);
  199. if (index < 0)
  200. {
  201. return;
  202. }
  203. pageIndex = index;
  204. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  205. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  206. Point cropPoint = new Point();
  207. if (PDFViewer.GetIsCrop())
  208. {
  209. CRect cRect = cPDFPage.GetCropBounds();
  210. cropPoint.X = DpiHelper.PDFNumToStandardNum(cRect.left);
  211. cropPoint.Y = DpiHelper.PDFNumToStandardNum(cRect.top);
  212. }
  213. pageSelectedRect.CreateRect(point, cropPoint, pageBound, PDFViewer.ActualWidth, PDFViewer.ActualHeight);
  214. pageSelectedRect.Draw();
  215. PDFViewer.CanHorizontallyScroll = false;
  216. PDFViewer.CanVerticallyScroll = false;
  217. PDFViewer.EnableZoom(false);
  218. }
  219. }
  220. public void SetPageSelectdUserControl(UserControl control)
  221. {
  222. PageSelectedRect pageSelectedRect = CommonHelper.FindVisualChild<PageSelectedRect>(PDFViewer.GetViewForTag(pageSelectedRectViewTag));
  223. if (pageSelectedRect != null)
  224. {
  225. pageSelectedRect.Children.Clear();
  226. Canvas popCanvas = new Canvas();
  227. pageSelectedRect.Children.Add(popCanvas);
  228. popCanvas.Children.Add(control);
  229. control.Visibility = Visibility.Collapsed;
  230. pageSelectedRect.Arrange();
  231. }
  232. }
  233. public void RemovePageSelectdUserControl()
  234. {
  235. PageSelectedRect pageSelectedRect = CommonHelper.FindVisualChild<PageSelectedRect>(PDFViewer.GetViewForTag(pageSelectedRectViewTag));
  236. if (pageSelectedRect != null)
  237. {
  238. pageSelectedRect.Children.Clear();
  239. pageIndex = -1;
  240. }
  241. }
  242. }
  243. }