PDFViewControl.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 SelectedPDFViewToolChanged;
  35. public event EventHandler<MouseEventObject> MouseLeftButtonDownHandler;
  36. public event EventHandler<MouseEventObject> MouseLeftButtonUpHandler;
  37. public event EventHandler<MouseEventObject> MouseMoveHandler;
  38. public event EventHandler<MouseEventObject> MouseRightButtonDownHandler;
  39. //private ContextMenu RightMenu;
  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. PDFToolManager.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  60. PDFToolManager.MouseLeftButtonUpHandler -= PDFToolManager_MouseLeftButtonUpHandler;
  61. PDFToolManager.MouseMoveHandler -= PDFToolManager_MouseMoveHandler;
  62. PDFToolManager.MouseRightButtonDownHandler -= PDFToolManager_MouseRightButtonDownHandler;
  63. PDFViewTool.SizeChanged += PDFViewTool_SizeChanged;
  64. PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler += PDFViewControl_MouseWheelZoomHandler;
  65. PDFToolManager.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
  66. PDFToolManager.MouseLeftButtonUpHandler += PDFToolManager_MouseLeftButtonUpHandler;
  67. PDFToolManager.MouseMoveHandler += PDFToolManager_MouseMoveHandler;
  68. PDFToolManager.MouseRightButtonDownHandler += PDFToolManager_MouseRightButtonDownHandler;
  69. splitViewerTool.SizeChanged -= SplitViewerTool_SizeChanged;
  70. splitViewerTool.GetCPDFViewer().MouseWheelZoomHandler -= SplitPDFViewControl_MouseWheelZoomHandler;
  71. splitToolManager.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  72. splitToolManager.MouseLeftButtonUpHandler -= PDFToolManager_MouseLeftButtonUpHandler;
  73. splitToolManager.MouseMoveHandler -= PDFToolManager_MouseMoveHandler;
  74. splitToolManager.MouseRightButtonDownHandler += PDFToolManager_MouseRightButtonDownHandler;
  75. splitViewerTool.SizeChanged += SplitViewerTool_SizeChanged;
  76. splitViewerTool.GetCPDFViewer().MouseWheelZoomHandler += SplitPDFViewControl_MouseWheelZoomHandler;
  77. splitToolManager.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
  78. splitToolManager.MouseLeftButtonUpHandler += PDFToolManager_MouseLeftButtonUpHandler;
  79. splitToolManager.MouseMoveHandler += PDFToolManager_MouseMoveHandler;
  80. splitToolManager.MouseRightButtonDownHandler += PDFToolManager_MouseRightButtonDownHandler;
  81. }
  82. public ContextMenu GetRightMenu()
  83. {
  84. FocusPDFViewTool.ContextMenu?.Items.Clear();
  85. return FocusPDFViewTool.ContextMenu;
  86. }
  87. public void SetRightMenu(ContextMenu contextMenu)
  88. {
  89. FocusPDFViewTool.ContextMenu = contextMenu;
  90. }
  91. private void PDFToolManager_MouseRightButtonDownHandler(object sender, MouseEventObject e)
  92. {
  93. MouseRightButtonDownHandler?.Invoke(sender, e);
  94. }
  95. private void PDFToolManager_MouseMoveHandler(object sender, MouseEventObject e)
  96. {
  97. MouseMoveHandler?.Invoke(this, e);
  98. }
  99. private void PDFToolManager_MouseLeftButtonUpHandler(object sender, MouseEventObject e)
  100. {
  101. MouseLeftButtonUpHandler?.Invoke(sender, e);
  102. }
  103. private void PDFToolManager_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
  104. {
  105. MouseLeftButtonDownHandler?.Invoke(sender,e);
  106. }
  107. private void PDFViewTool_SizeChanged(object sender, SizeChangedEventArgs e)
  108. {
  109. PDFViewTool?.GetCPDFViewer()?.UpDateRenderFrame();
  110. }
  111. private void SplitViewerTool_SizeChanged(object sender, SizeChangedEventArgs e)
  112. {
  113. splitViewerTool?.GetCPDFViewer()?.UpDateRenderFrame();
  114. }
  115. public void InitDocument(string Path)
  116. {
  117. CPDFDocument pdfDoc = CPDFDocument.InitWithFilePath(Path);
  118. if (pdfDoc != null)
  119. {
  120. PDFViewTool.GetCPDFViewer().InitDoc(pdfDoc);
  121. PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitHeight);
  122. PDFViewTool.GetCPDFViewer().SetViewMode(ViewModes.SingleContinuous);
  123. splitViewerTool.GetCPDFViewer().InitDoc(pdfDoc);
  124. splitViewerTool.GetCPDFViewer().SetFitMode(FitModes.FitHeight);
  125. splitViewerTool.GetCPDFViewer().SetViewMode(ViewModes.SingleContinuous);
  126. }
  127. }
  128. private void PDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
  129. {
  130. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  131. {
  132. e.IsZoom = true;
  133. PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitZoom);
  134. double zoom = PDFViewTool.GetCPDFViewer().GetZoom();
  135. PDFViewTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior)));
  136. PDFViewTool?.GetCPDFViewer()?.UpDateRenderFrame();
  137. }
  138. }
  139. private void SplitPDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
  140. {
  141. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  142. {
  143. e.IsZoom = true;
  144. splitViewerTool.GetCPDFViewer().SetFitMode(FitModes.FitZoom);
  145. double zoom = splitViewerTool.GetCPDFViewer().GetZoom();
  146. splitViewerTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior)));
  147. splitViewerTool?.GetCPDFViewer()?.UpDateRenderFrame();
  148. }
  149. }
  150. public void SetSplitViewMode(SplitMode splitMode)
  151. {
  152. switch (splitMode)
  153. {
  154. case SplitMode.None:
  155. RemoveSplitViewerTool();
  156. break;
  157. case SplitMode.Vertical:
  158. RemoveSplitViewerTool();
  159. if(splitViewerTool != null)
  160. {
  161. VerticalSplitter.Visibility = Visibility.Visible;
  162. VerticalView.Visibility = Visibility.Visible;
  163. ViewToolGrid.ColumnDefinitions[1].Width = new GridLength(15, GridUnitType.Auto);
  164. ViewToolGrid.ColumnDefinitions[2].Width = new GridLength(ViewToolGrid.ActualWidth / 2);
  165. VerticalView.Child = splitViewerTool;
  166. }
  167. break;
  168. case SplitMode.Horizontal:
  169. RemoveSplitViewerTool();
  170. if(splitViewerTool != null)
  171. {
  172. HorizontalSplitter.Visibility = Visibility.Visible;
  173. HorizontalView.Visibility = Visibility.Visible;
  174. ViewToolGrid.RowDefinitions[1].Height = new GridLength(15,GridUnitType.Auto);
  175. ViewToolGrid.RowDefinitions[2].Height = new GridLength(ViewToolGrid.ActualHeight / 2);
  176. HorizontalView.Child = splitViewerTool;
  177. }
  178. break;
  179. }
  180. UpdateFrame();
  181. UpdateAnnotFrame();
  182. }
  183. public void UpdateFrame()
  184. {
  185. viewerTool?.GetCPDFViewer()?.UpDateRenderFrame();
  186. splitViewerTool?.GetCPDFViewer()?.UpDateRenderFrame();
  187. }
  188. public void UpdateAnnotFrame()
  189. {
  190. viewerTool?.GetCPDFViewer()?.UpDateAnnotFrame();
  191. splitViewerTool?.GetCPDFViewer()?.UpDateAnnotFrame();
  192. }
  193. public void SetToolType(ToolType type)
  194. {
  195. toolManager?.SetToolType(type);
  196. splitToolManager?.SetToolType(type);
  197. }
  198. public void SetCreateAnnotType(C_ANNOTATION_TYPE type)
  199. {
  200. toolManager?.SetCreateAnnotType(type);
  201. splitToolManager?.SetCreateAnnotType(type);
  202. }
  203. public void SetCreateWidgetType(C_WIDGET_TYPE type)
  204. {
  205. toolManager?.SetCreateWidgetType(type);
  206. splitToolManager?.SetCreateWidgetType(type);
  207. }
  208. public void SetAnnotParam(AnnotParam param)
  209. {
  210. DefaultSettingParam defaultSettingParam = viewerTool.GetDefaultSettingParam();
  211. DefaultSettingParam splitSettingParam = splitViewerTool.GetDefaultSettingParam();
  212. defaultSettingParam.SetAnnotParam(param);
  213. splitSettingParam.SetAnnotParam(param);
  214. }
  215. public void SetIsShowStampMouse(bool isShow)
  216. {
  217. viewerTool?.GetCPDFViewer()?.SetIsShowStampMouse(isShow);
  218. splitViewerTool?.GetCPDFViewer()?.SetIsShowStampMouse(isShow);
  219. }
  220. public void SetDrawModes(DrawModeData drawMode)
  221. {
  222. viewerTool?.GetCPDFViewer()?.SetDrawModes(drawMode.DrawMode);
  223. splitViewerTool?.GetCPDFViewer()?.SetDrawModes(drawMode.DrawMode);
  224. UpdateFrame();
  225. }
  226. public void SetStampMouseImage(byte[] imageArray,int imageWidth,int imageHeight)
  227. {
  228. viewerTool?.GetCPDFViewer()?.SetStampMouseImage(imageArray, imageWidth, imageHeight);
  229. splitViewerTool?.GetCPDFViewer()?.SetStampMouseImage(imageArray, imageWidth, imageHeight);
  230. }
  231. public bool SetStampMouseImage(string filePath)
  232. {
  233. if((bool)(viewerTool?.GetCPDFViewer()?.SetStampMouseImage(filePath)) && (bool)splitViewerTool?.GetCPDFViewer()?.SetStampMouseImage(filePath))
  234. {
  235. return true;
  236. }
  237. return false;
  238. }
  239. public void SetIsVisibleCustomMouse(bool isVisbleCustomMouse)
  240. {
  241. viewerTool?.GetCPDFViewer()?.SetIsVisibleCustomMouse(isVisbleCustomMouse);
  242. splitViewerTool?.GetCPDFViewer()?.SetIsVisibleCustomMouse(isVisbleCustomMouse);
  243. }
  244. public CPDFViewer GetCPDFViewer()
  245. {
  246. return FocusPDFViewTool?.GetCPDFViewer();
  247. }
  248. public BaseWidget GetCacheHitTestWidget()
  249. {
  250. return FocusPDFViewTool?.GetCacheHitTestWidget();
  251. }
  252. public BaseAnnot GetCacheHitTestAnnot()
  253. {
  254. return FocusPDFViewTool?.GetCacheHitTestAnnot();
  255. }
  256. private void RemoveSplitViewerTool()
  257. {
  258. UpdateFrame();
  259. VerticalSplitter.Visibility = Visibility.Collapsed;
  260. HorizontalSplitter.Visibility = Visibility.Collapsed;
  261. VerticalView.Visibility = Visibility.Collapsed;
  262. HorizontalView.Visibility = Visibility.Collapsed;
  263. ViewToolGrid.ColumnDefinitions[1].Width = new GridLength(0);
  264. ViewToolGrid.ColumnDefinitions[2].Width = new GridLength(0);
  265. ViewToolGrid.RowDefinitions[1].Height = new GridLength(0);
  266. ViewToolGrid.RowDefinitions[2].Height = new GridLength(0);
  267. VerticalView.Child = null;
  268. HorizontalView.Child = null;
  269. }
  270. #region Private Command Methods
  271. private double CheckZoomLevel(double zoom, bool IsGrowth)
  272. {
  273. zoom += (IsGrowth ? 0.01 : -0.01);
  274. double standardZoom = 100;
  275. if (zoom <= 0.01)
  276. {
  277. return 0.01;
  278. }
  279. if (zoom >= 10)
  280. {
  281. return 10;
  282. }
  283. zoom *= 100;
  284. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  285. {
  286. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  287. {
  288. standardZoom = zoomLevelList[i + 1];
  289. break;
  290. }
  291. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  292. {
  293. standardZoom = zoomLevelList[i];
  294. break;
  295. }
  296. }
  297. return standardZoom / 100;
  298. }
  299. #endregion
  300. private void PDFView_MouseDown(object sender, MouseButtonEventArgs e)
  301. {
  302. switch ((sender as Border).Tag)
  303. {
  304. case "ViewerTool":
  305. FocusPDFViewTool = viewerTool;
  306. break;
  307. case "SplitViewerTool":
  308. FocusPDFViewTool = splitViewerTool;
  309. break;
  310. }
  311. SelectedPDFViewToolChanged?.Invoke(this, EventArgs.Empty);
  312. }
  313. }
  314. }