PDFViewControl.xaml.cs 13 KB

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