PDFViewControl.xaml.cs 12 KB

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