PDFViewControl.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFAnnotation.Form;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.Tool;
  5. using ComPDFKit.Tool.SettingParam;
  6. using Compdfkit_Tools.PDFControlUI;
  7. using ComPDFKitViewer;
  8. using ComPDFKitViewer.BaseObject;
  9. using ComPDFKitViewer.Widget;
  10. using Nager.Country.Currencies;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.Linq;
  15. using System.Reflection;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Input;
  19. using static ComPDFKit.Tool.CPDFToolManager;
  20. using static Compdfkit_Tools.PDFControlUI.CPDFViewModeUI;
  21. using SplitMode = Compdfkit_Tools.PDFControlUI.CPDFViewModeUI.SplitMode;
  22. namespace Compdfkit_Tools.PDFControl
  23. {
  24. public partial class PDFViewControl : UserControl
  25. {
  26. private CPDFViewerTool splitViewerTool;
  27. private CPDFToolManager splitToolManager;
  28. private CPDFViewerTool viewerTool;
  29. private CPDFToolManager toolManager;
  30. public CPDFViewerTool PDFViewTool { get;private set; }
  31. public CPDFToolManager PDFToolManager { get;private set; }
  32. public CPDFViewerTool FocusPDFViewTool { get;private set; }
  33. //public event EventHandler SplitPDFViewToolCreated;
  34. public event EventHandler FocusPDFViewToolChanged;
  35. public event EventHandler<MouseEventObject> MouseLeftButtonDownHandler;
  36. public event EventHandler<MouseEventObject> MouseLeftButtonUpHandler;
  37. public event EventHandler<MouseEventObject> MouseMoveHandler;
  38. public event EventHandler<ComPDFKitViewer.MouseWheelZoomArgs> MouseWheelZoomHandler;
  39. public event EventHandler DrawChanged;
  40. #region Properties
  41. public bool CustomSignHandle { get; set; }
  42. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  43. #endregion
  44. public PDFViewControl()
  45. {
  46. InitializeComponent();
  47. viewerTool = new CPDFViewerTool();
  48. toolManager = new CPDFToolManager(viewerTool);
  49. PDFViewTool = viewerTool;
  50. PDFToolManager = toolManager;
  51. PDFToolManager.SetToolType(CPDFToolManager.ToolType.Viewer);
  52. splitViewerTool = new CPDFViewerTool();
  53. splitToolManager = new CPDFToolManager(splitViewerTool);
  54. splitToolManager.SetToolType(CPDFToolManager.ToolType.Viewer);
  55. FocusPDFViewTool = viewerTool;
  56. PDFView.Child = PDFViewTool;
  57. PDFViewTool.SizeChanged -= PDFViewTool_SizeChanged;
  58. PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler -= PDFViewControl_MouseWheelZoomHandler;
  59. PDFViewTool.DrawChanged -= PDFViewTool_DrawChanged;
  60. PDFToolManager.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  61. PDFToolManager.MouseLeftButtonUpHandler -= PDFToolManager_MouseLeftButtonUpHandler;
  62. PDFToolManager.MouseMoveHandler -= PDFToolManager_MouseMoveHandler;
  63. PDFViewTool.SizeChanged += PDFViewTool_SizeChanged;
  64. PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler += PDFViewControl_MouseWheelZoomHandler;
  65. PDFViewTool.DrawChanged += PDFViewTool_DrawChanged;
  66. PDFToolManager.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
  67. PDFToolManager.MouseLeftButtonUpHandler += PDFToolManager_MouseLeftButtonUpHandler;
  68. PDFToolManager.MouseMoveHandler += PDFToolManager_MouseMoveHandler;
  69. splitViewerTool.SizeChanged -= SplitViewerTool_SizeChanged;
  70. splitViewerTool.GetCPDFViewer().MouseWheelZoomHandler -= SplitPDFViewControl_MouseWheelZoomHandler;
  71. splitViewerTool.DrawChanged -= PDFViewTool_DrawChanged;
  72. splitToolManager.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  73. splitToolManager.MouseLeftButtonUpHandler -= PDFToolManager_MouseLeftButtonUpHandler;
  74. splitToolManager.MouseMoveHandler -= PDFToolManager_MouseMoveHandler;
  75. splitViewerTool.SizeChanged += SplitViewerTool_SizeChanged;
  76. splitViewerTool.GetCPDFViewer().MouseWheelZoomHandler += SplitPDFViewControl_MouseWheelZoomHandler;
  77. splitViewerTool.DrawChanged += PDFViewTool_DrawChanged;
  78. splitToolManager.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
  79. splitToolManager.MouseLeftButtonUpHandler += PDFToolManager_MouseLeftButtonUpHandler;
  80. splitToolManager.MouseMoveHandler += PDFToolManager_MouseMoveHandler;
  81. }
  82. private void PDFViewTool_DrawChanged(object sender, EventArgs e)
  83. {
  84. DrawChanged?.Invoke(sender,e);
  85. }
  86. private void PDFToolManager_MouseMoveHandler(object sender, MouseEventObject e)
  87. {
  88. MouseMoveHandler?.Invoke(this, e);
  89. }
  90. private void PDFToolManager_MouseLeftButtonUpHandler(object sender, MouseEventObject e)
  91. {
  92. MouseLeftButtonUpHandler?.Invoke(sender, e);
  93. }
  94. private void PDFToolManager_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
  95. {
  96. MouseLeftButtonDownHandler?.Invoke(sender,e);
  97. }
  98. private void PDFViewTool_SizeChanged(object sender, SizeChangedEventArgs e)
  99. {
  100. PDFViewTool.GetCPDFViewer().UpDateRenderFrame();
  101. }
  102. private void SplitViewerTool_SizeChanged(object sender, SizeChangedEventArgs e)
  103. {
  104. splitViewerTool.GetCPDFViewer().UpDateRenderFrame();
  105. }
  106. public void InitDocument(string Path)
  107. {
  108. CPDFDocument pdfDoc = CPDFDocument.InitWithFilePath(Path);
  109. if (pdfDoc != null)
  110. {
  111. PDFViewTool.GetCPDFViewer().InitDoc(pdfDoc);
  112. PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitHeight);
  113. PDFViewTool.GetCPDFViewer().SetViewMode(ViewModes.SingleContinuous);
  114. splitViewerTool.GetCPDFViewer().InitDoc(pdfDoc);
  115. splitViewerTool.GetCPDFViewer().SetFitMode(FitModes.FitHeight);
  116. splitViewerTool.GetCPDFViewer().SetViewMode(ViewModes.SingleContinuous);
  117. }
  118. }
  119. private void PDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
  120. {
  121. FocusPDFViewTool = PDFViewTool;
  122. FocusPDFViewToolChanged?.Invoke(this, EventArgs.Empty);
  123. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  124. {
  125. e.IsZoom = true;
  126. PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitZoom);
  127. double zoom = PDFViewTool.GetCPDFViewer().GetZoom();
  128. PDFViewTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior)));
  129. PDFViewTool?.GetCPDFViewer()?.UpDateRenderFrame();
  130. }
  131. MouseWheelZoomHandler?.Invoke(this, e);
  132. }
  133. private void SplitPDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
  134. {
  135. FocusPDFViewTool = splitViewerTool;
  136. FocusPDFViewToolChanged?.Invoke(this, EventArgs.Empty);
  137. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  138. {
  139. e.IsZoom = true;
  140. splitViewerTool.GetCPDFViewer().SetFitMode(FitModes.FitZoom);
  141. double zoom = splitViewerTool.GetCPDFViewer().GetZoom();
  142. splitViewerTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior)));
  143. splitViewerTool?.GetCPDFViewer()?.UpDateRenderFrame();
  144. }
  145. MouseWheelZoomHandler?.Invoke(this, e);
  146. }
  147. public void SetSplitViewMode(SplitMode splitMode)
  148. {
  149. switch (splitMode)
  150. {
  151. case SplitMode.None:
  152. RemoveSplitViewerTool();
  153. break;
  154. case SplitMode.Vertical:
  155. RemoveSplitViewerTool();
  156. if(splitViewerTool != null)
  157. {
  158. VerticalSplitter.Visibility = Visibility.Visible;
  159. VerticalView.Visibility = Visibility.Visible;
  160. ViewToolGrid.ColumnDefinitions[1].Width = new GridLength(15, GridUnitType.Auto);
  161. ViewToolGrid.ColumnDefinitions[2].Width = new GridLength(ViewToolGrid.ActualWidth / 2);
  162. VerticalView.Child = splitViewerTool;
  163. }
  164. break;
  165. case SplitMode.Horizontal:
  166. RemoveSplitViewerTool();
  167. if(splitViewerTool != null)
  168. {
  169. HorizontalSplitter.Visibility = Visibility.Visible;
  170. HorizontalView.Visibility = Visibility.Visible;
  171. ViewToolGrid.RowDefinitions[1].Height = new GridLength(15,GridUnitType.Auto);
  172. ViewToolGrid.RowDefinitions[2].Height = new GridLength(ViewToolGrid.ActualHeight / 2);
  173. HorizontalView.Child = splitViewerTool;
  174. }
  175. break;
  176. }
  177. UpdateRenderFrame();
  178. UpdateAnnotFrame();
  179. }
  180. public void UpdateRenderFrame()
  181. {
  182. viewerTool?.GetCPDFViewer()?.UpDateRenderFrame();
  183. splitViewerTool?.GetCPDFViewer()?.UpDateRenderFrame();
  184. }
  185. public void UpdateAnnotFrame()
  186. {
  187. viewerTool?.GetCPDFViewer()?.UpDateAnnotFrame();
  188. splitViewerTool?.GetCPDFViewer()?.UpDateAnnotFrame();
  189. }
  190. public void SetToolType(ToolType type)
  191. {
  192. toolManager?.SetToolType(type);
  193. splitToolManager?.SetToolType(type);
  194. }
  195. public void SetCreateAnnotType(C_ANNOTATION_TYPE type)
  196. {
  197. toolManager?.SetCreateAnnotType(type);
  198. splitToolManager?.SetCreateAnnotType(type);
  199. }
  200. public void SetCreateWidgetType(C_WIDGET_TYPE type)
  201. {
  202. toolManager?.SetCreateWidgetType(type);
  203. splitToolManager?.SetCreateWidgetType(type);
  204. }
  205. public void SetAnnotParam(AnnotParam param)
  206. {
  207. DefaultSettingParam defaultSettingParam = viewerTool.GetDefaultSettingParam();
  208. DefaultSettingParam splitSettingParam = splitViewerTool.GetDefaultSettingParam();
  209. defaultSettingParam.SetAnnotParam(param);
  210. splitSettingParam.SetAnnotParam(param);
  211. }
  212. public void SetIsShowStampMouse(bool isShow)
  213. {
  214. viewerTool?.GetCPDFViewer()?.SetIsShowStampMouse(isShow);
  215. splitViewerTool?.GetCPDFViewer()?.SetIsShowStampMouse(isShow);
  216. }
  217. public void SetDrawModes(DrawModeData drawMode)
  218. {
  219. viewerTool?.GetCPDFViewer()?.SetDrawModes(drawMode.DrawMode);
  220. splitViewerTool?.GetCPDFViewer()?.SetDrawModes(drawMode.DrawMode);
  221. if (drawMode.DrawMode == DrawModes.Custom)
  222. {
  223. viewerTool?.GetCPDFViewer().SetPDFBackground(drawMode.CustomColor);
  224. splitViewerTool?.GetCPDFViewer().SetPDFBackground(drawMode.CustomColor);
  225. }
  226. UpdateRenderFrame();
  227. }
  228. public void SetStampMouseImage(byte[] imageArray,int imageWidth,int imageHeight)
  229. {
  230. viewerTool?.GetCPDFViewer()?.SetStampMouseImage(imageArray, imageWidth, imageHeight);
  231. splitViewerTool?.GetCPDFViewer()?.SetStampMouseImage(imageArray, imageWidth, imageHeight);
  232. }
  233. public bool SetStampMouseImage(string filePath)
  234. {
  235. if((bool)(viewerTool?.GetCPDFViewer()?.SetStampMouseImage(filePath)) && (bool)splitViewerTool?.GetCPDFViewer()?.SetStampMouseImage(filePath))
  236. {
  237. return true;
  238. }
  239. return false;
  240. }
  241. public void SetIsVisibleCustomMouse(bool isVisbleCustomMouse)
  242. {
  243. viewerTool?.GetCPDFViewer()?.SetIsVisibleCustomMouse(isVisbleCustomMouse);
  244. splitViewerTool?.GetCPDFViewer()?.SetIsVisibleCustomMouse(isVisbleCustomMouse);
  245. }
  246. public CPDFViewer GetCPDFViewer()
  247. {
  248. return FocusPDFViewTool?.GetCPDFViewer();
  249. }
  250. public BaseWidget GetCacheHitTestWidget()
  251. {
  252. return FocusPDFViewTool?.GetCacheHitTestWidget();
  253. }
  254. public BaseAnnot GetCacheHitTestAnnot()
  255. {
  256. return FocusPDFViewTool?.GetCacheHitTestAnnot();
  257. }
  258. private void RemoveSplitViewerTool()
  259. {
  260. UpdateRenderFrame();
  261. VerticalSplitter.Visibility = Visibility.Collapsed;
  262. HorizontalSplitter.Visibility = Visibility.Collapsed;
  263. VerticalView.Visibility = Visibility.Collapsed;
  264. HorizontalView.Visibility = Visibility.Collapsed;
  265. ViewToolGrid.ColumnDefinitions[1].Width = new GridLength(0);
  266. ViewToolGrid.ColumnDefinitions[2].Width = new GridLength(0);
  267. ViewToolGrid.RowDefinitions[1].Height = new GridLength(0);
  268. ViewToolGrid.RowDefinitions[2].Height = new GridLength(0);
  269. VerticalView.Child = null;
  270. HorizontalView.Child = null;
  271. }
  272. private void PDFView_GotFocus(object sender, RoutedEventArgs e)
  273. {
  274. switch((sender as Border).Tag)
  275. {
  276. case "ViewerTool":
  277. FocusPDFViewTool = viewerTool;
  278. break;
  279. case "SplitViewerTool":
  280. FocusPDFViewTool = splitViewerTool;
  281. break;
  282. }
  283. FocusPDFViewToolChanged?.Invoke(this,EventArgs.Empty);
  284. }
  285. #region Private Command Methods
  286. private double CheckZoomLevel(double zoom, bool IsGrowth)
  287. {
  288. zoom += (IsGrowth ? 0.01 : -0.01);
  289. double standardZoom = 100;
  290. if (zoom <= 0.01)
  291. {
  292. return 0.01;
  293. }
  294. if (zoom >= 10)
  295. {
  296. return 10;
  297. }
  298. zoom *= 100;
  299. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  300. {
  301. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  302. {
  303. standardZoom = zoomLevelList[i + 1];
  304. break;
  305. }
  306. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  307. {
  308. standardZoom = zoomLevelList[i];
  309. break;
  310. }
  311. }
  312. return standardZoom / 100;
  313. }
  314. #endregion
  315. }
  316. }