PDFViewControl.xaml.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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.Controls.PDFControlUI;
  7. using ComPDFKitViewer;
  8. using ComPDFKitViewer.BaseObject;
  9. using ComPDFKitViewer.Widget;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Reflection;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Input;
  16. using ComPDFKit.Controls.Helper;
  17. using ComPDFKit.Tool.DrawTool;
  18. using SplitMode = ComPDFKit.Controls.PDFControlUI.CPDFViewModeUI.SplitMode;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using ComPDFKitViewer.Annot;
  22. using System.IO;
  23. namespace ComPDFKit.Controls.PDFControl
  24. {
  25. public partial class PDFViewControl : UserControl
  26. {
  27. private CPDFViewerTool splitViewerTool;
  28. private CPDFToolManager splitToolManager;
  29. private CPDFViewerTool viewerTool;
  30. private CPDFToolManager toolManager;
  31. public string Password { get; set; } = string.Empty;
  32. public CPDFViewerTool PDFViewTool { get; private set; }
  33. public CPDFToolManager PDFToolManager { get; private set; }
  34. public CPDFViewerTool FocusPDFViewTool { get; private set; }
  35. //public event EventHandler SplitPDFViewToolCreated;
  36. public event EventHandler FocusPDFViewToolChanged;
  37. public event EventHandler<MouseEventObject> MouseLeftButtonDownHandler;
  38. public event EventHandler<MouseEventObject> MouseLeftButtonUpHandler;
  39. public event EventHandler<MouseEventObject> MouseMoveHandler;
  40. public event EventHandler<MouseEventObject> MouseRightButtonDownHandler;
  41. public event EventHandler<ComPDFKitViewer.MouseWheelZoomArgs> MouseWheelZoomHandler;
  42. public event EventHandler DrawChanged;
  43. //private ContextMenu RightMenu;
  44. #region Properties
  45. private double splitWidthScale = 0.5;
  46. private double splitHeightScale = 0.5;
  47. public bool CustomSignHandle { get; set; }
  48. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  49. #endregion
  50. public PDFViewControl()
  51. {
  52. InitializeComponent();
  53. viewerTool = new CPDFViewerTool();
  54. toolManager = new CPDFToolManager(viewerTool);
  55. PDFViewTool = viewerTool;
  56. PDFToolManager = toolManager;
  57. PDFToolManager.SetToolType(ToolType.Viewer);
  58. splitViewerTool = new CPDFViewerTool();
  59. splitToolManager = new CPDFToolManager(splitViewerTool);
  60. splitToolManager.SetToolType(ToolType.Viewer);
  61. FocusPDFViewTool = viewerTool;
  62. PDFView.Child = PDFViewTool;
  63. VerticalView.Child = splitViewerTool;
  64. PDFViewTool.SizeChanged -= PDFViewTool_SizeChanged;
  65. PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler -= PDFViewControl_MouseWheelZoomHandler;
  66. PDFViewTool.GetCPDFViewer().MouseMove -= PDFViewControl_MouseMove;
  67. PDFViewTool.DrawChanged -= PDFViewTool_DrawChanged;
  68. PDFToolManager.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  69. PDFToolManager.MouseLeftButtonUpHandler -= PDFToolManager_MouseLeftButtonUpHandler;
  70. PDFToolManager.MouseMoveHandler -= PDFToolManager_MouseMoveHandler;
  71. PDFToolManager.MouseRightButtonDownHandler -= PDFToolManager_MouseRightButtonDownHandler;
  72. PDFViewTool.SizeChanged += PDFViewTool_SizeChanged;
  73. PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler += PDFViewControl_MouseWheelZoomHandler;
  74. PDFViewTool.GetCPDFViewer().MouseMove += PDFViewControl_MouseMove;
  75. PDFViewTool.DrawChanged += PDFViewTool_DrawChanged;
  76. PDFToolManager.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
  77. PDFToolManager.MouseLeftButtonUpHandler += PDFToolManager_MouseLeftButtonUpHandler;
  78. PDFToolManager.MouseMoveHandler += PDFToolManager_MouseMoveHandler;
  79. PDFToolManager.MouseRightButtonDownHandler += PDFToolManager_MouseRightButtonDownHandler;
  80. splitViewerTool.SizeChanged -= SplitViewerTool_SizeChanged;
  81. splitViewerTool.GetCPDFViewer().MouseWheelZoomHandler -= SplitPDFViewControl_MouseWheelZoomHandler;
  82. splitViewerTool.DrawChanged -= PDFViewTool_DrawChanged;
  83. splitToolManager.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  84. splitToolManager.MouseLeftButtonUpHandler -= PDFToolManager_MouseLeftButtonUpHandler;
  85. splitToolManager.MouseMoveHandler -= PDFToolManager_MouseMoveHandler;
  86. splitToolManager.MouseRightButtonDownHandler -= PDFToolManager_MouseRightButtonDownHandler;
  87. splitViewerTool.SizeChanged += SplitViewerTool_SizeChanged;
  88. splitViewerTool.GetCPDFViewer().MouseWheelZoomHandler += SplitPDFViewControl_MouseWheelZoomHandler;
  89. splitViewerTool.DrawChanged += PDFViewTool_DrawChanged;
  90. splitToolManager.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
  91. splitToolManager.MouseLeftButtonUpHandler += PDFToolManager_MouseLeftButtonUpHandler;
  92. splitToolManager.MouseMoveHandler += PDFToolManager_MouseMoveHandler;
  93. splitToolManager.MouseRightButtonDownHandler += PDFToolManager_MouseRightButtonDownHandler;
  94. SetCursor();
  95. }
  96. private void PDFViewTool_DrawChanged(object sender, EventArgs e)
  97. {
  98. FocusPDFViewToolChanged?.Invoke(this, EventArgs.Empty);
  99. DrawChanged?.Invoke(sender, e);
  100. }
  101. public ContextMenu GetRightMenu()
  102. {
  103. FocusPDFViewTool.ContextMenu?.Items.Clear();
  104. return FocusPDFViewTool.ContextMenu;
  105. }
  106. public void SetRightMenu(ContextMenu contextMenu)
  107. {
  108. FocusPDFViewTool.ContextMenu = contextMenu;
  109. }
  110. private void PDFToolManager_MouseRightButtonDownHandler(object sender, MouseEventObject e)
  111. {
  112. Point point = e.mouseButtonEventArgs.GetPosition(this);
  113. viewerTool.GetCPDFViewer().GetPointPageInfo(point, out int index, out Rect paintRect, out Rect pageBound);
  114. if (index == -1)
  115. {
  116. viewerTool.ContextMenu = null;
  117. return;
  118. }
  119. PDFViewTool.SetPastePoint(point);
  120. MouseRightButtonDownHandler?.Invoke(sender, e);
  121. }
  122. private void PDFToolManager_MouseMoveHandler(object sender, MouseEventObject e)
  123. {
  124. MouseMoveHandler?.Invoke(this, e);
  125. }
  126. private void PDFToolManager_MouseLeftButtonUpHandler(object sender, MouseEventObject e)
  127. {
  128. MouseLeftButtonUpHandler?.Invoke(sender, e);
  129. if (IsHitEmpty)
  130. {
  131. List<ToolType> toolTypes = new List<ToolType>()
  132. {
  133. ToolType.Viewer,
  134. ToolType.Pan
  135. };
  136. ToolType currentMode = PDFViewTool.GetToolType();
  137. if (GetCPDFViewer().Cursor == panToolCursor || GetCPDFViewer().Cursor == panTool2Cursor)
  138. {
  139. if (toolTypes.Contains(currentMode))
  140. {
  141. GetCPDFViewer().Cursor = panToolCursor;
  142. }
  143. else
  144. {
  145. GetCPDFViewer().Cursor = Cursors.Arrow;
  146. }
  147. }
  148. IsHitEmpty = false;
  149. }
  150. }
  151. private void PDFToolManager_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
  152. {
  153. MouseLeftButtonDownHandler?.Invoke(sender, e);
  154. ToolType toolType = PDFToolManager.GetToolType();
  155. if (e.hitTestType != MouseHitTestType.Text && e.hitTestType != MouseHitTestType.SelectedPageRect && e.hitTestType != MouseHitTestType.Annot && e.hitTestType != MouseHitTestType.SelectRect && e.hitTestType != MouseHitTestType.Widget)
  156. {
  157. if (toolType == ToolType.Pan)
  158. {
  159. IsHitEmpty = true;
  160. hitEmptyPos = e.mouseButtonEventArgs.GetPosition(this);
  161. GetCPDFViewer().Cursor = panToolCursor;
  162. }
  163. }
  164. }
  165. private void PDFViewTool_SizeChanged(object sender, SizeChangedEventArgs e)
  166. {
  167. }
  168. private void SplitViewerTool_SizeChanged(object sender, SizeChangedEventArgs e)
  169. {
  170. if (VerticalView.Visibility == Visibility.Visible)
  171. {
  172. splitWidthScale = VerticalView.ActualWidth / ViewToolGrid.ActualWidth;
  173. }
  174. if (HorizontalView.Visibility == Visibility.Visible)
  175. {
  176. splitHeightScale = HorizontalView.ActualHeight / ViewToolGrid.ActualHeight;
  177. }
  178. }
  179. public void InitDocument(string Path)
  180. {
  181. CPDFDocument pdfDoc = CPDFDocument.InitWithFilePath(Path);
  182. if (pdfDoc != null)
  183. {
  184. PDFViewTool.GetCPDFViewer().InitDoc(pdfDoc);
  185. PDFViewTool.GetCPDFViewer().SetFitMode(FitMode.FitHeight);
  186. PDFViewTool.GetCPDFViewer().SetViewMode(ViewMode.SingleContinuous);
  187. PDFViewTool.SetIsMultiSelected(true);
  188. splitViewerTool.GetCPDFViewer().InitDoc(pdfDoc);
  189. splitViewerTool.GetCPDFViewer().SetFitMode(FitMode.FitHeight);
  190. splitViewerTool.GetCPDFViewer().SetViewMode(ViewMode.SingleContinuous);
  191. }
  192. }
  193. private void PDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
  194. {
  195. FocusPDFViewTool = PDFViewTool;
  196. FocusPDFViewToolChanged?.Invoke(this, EventArgs.Empty);
  197. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  198. {
  199. e.IsZoom = true;
  200. PDFViewTool.GetCPDFViewer().SetFitMode(FitMode.FitZoom);
  201. double zoom = PDFViewTool.GetCPDFViewer().GetZoom();
  202. PDFViewTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior)));
  203. PDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  204. }
  205. MouseWheelZoomHandler?.Invoke(this, e);
  206. }
  207. private void SplitPDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
  208. {
  209. FocusPDFViewTool = splitViewerTool;
  210. FocusPDFViewToolChanged?.Invoke(this, EventArgs.Empty);
  211. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  212. {
  213. e.IsZoom = true;
  214. splitViewerTool.GetCPDFViewer().SetFitMode(FitMode.FitZoom);
  215. double zoom = splitViewerTool.GetCPDFViewer().GetZoom();
  216. splitViewerTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior)));
  217. splitViewerTool?.GetCPDFViewer()?.UpdateRenderFrame();
  218. }
  219. MouseWheelZoomHandler?.Invoke(this, e);
  220. }
  221. public void SetSplitViewMode(SplitMode splitMode)
  222. {
  223. switch (splitMode)
  224. {
  225. case SplitMode.None:
  226. RemoveSplitViewerTool();
  227. break;
  228. case SplitMode.Vertical:
  229. RemoveSplitViewerTool();
  230. if (splitViewerTool != null)
  231. {
  232. VerticalSplitter.Visibility = Visibility.Visible;
  233. VerticalView.Visibility = Visibility.Visible;
  234. ViewToolGrid.ColumnDefinitions[1].Width = new GridLength(15, GridUnitType.Auto);
  235. ViewToolGrid.ColumnDefinitions[2].Width = new GridLength(ViewToolGrid.ActualWidth / 2);
  236. VerticalView.Child = splitViewerTool;
  237. }
  238. break;
  239. case SplitMode.Horizontal:
  240. RemoveSplitViewerTool();
  241. if (splitViewerTool != null)
  242. {
  243. HorizontalSplitter.Visibility = Visibility.Visible;
  244. HorizontalView.Visibility = Visibility.Visible;
  245. ViewToolGrid.RowDefinitions[1].Height = new GridLength(15, GridUnitType.Auto);
  246. ViewToolGrid.RowDefinitions[2].Height = new GridLength(ViewToolGrid.ActualHeight / 2);
  247. HorizontalView.Child = splitViewerTool;
  248. }
  249. break;
  250. }
  251. UpdateRenderFrame();
  252. UpdateAnnotFrame();
  253. }
  254. public void UpdateRenderFrame()
  255. {
  256. viewerTool?.GetCPDFViewer()?.UpdateRenderFrame();
  257. splitViewerTool?.GetCPDFViewer()?.UpdateRenderFrame();
  258. }
  259. public void UpdateAnnotFrame()
  260. {
  261. viewerTool?.GetCPDFViewer()?.UpdateAnnotFrame();
  262. splitViewerTool?.GetCPDFViewer()?.UpdateAnnotFrame();
  263. }
  264. public void SetCropMode(bool isCrop)
  265. {
  266. viewerTool?.GetCPDFViewer()?.SetIsCrop(isCrop);
  267. splitViewerTool?.GetCPDFViewer()?.SetIsCrop(isCrop);
  268. viewerTool?.GetCPDFViewer()?.UpdateVirtualNodes();
  269. splitViewerTool?.GetCPDFViewer()?.UpdateVirtualNodes();
  270. viewerTool?.GetCPDFViewer()?.UpdateRenderFrame();
  271. splitViewerTool?.GetCPDFViewer()?.UpdateRenderFrame();
  272. }
  273. public void SetToolType(ToolType type)
  274. {
  275. toolManager?.SetToolType(type);
  276. splitToolManager?.SetToolType(type);
  277. }
  278. public void SetCreateAnnotType(C_ANNOTATION_TYPE type)
  279. {
  280. toolManager?.SetCreateAnnotType(type);
  281. splitToolManager?.SetCreateAnnotType(type);
  282. }
  283. public void SetCreateWidgetType(C_WIDGET_TYPE type)
  284. {
  285. toolManager?.SetCreateWidgetType(type);
  286. splitToolManager?.SetCreateWidgetType(type);
  287. }
  288. public void SetAnnotParam(AnnotParam param)
  289. {
  290. DefaultSettingParam defaultSettingParam = viewerTool.GetDefaultSettingParam();
  291. DefaultSettingParam splitSettingParam = splitViewerTool.GetDefaultSettingParam();
  292. defaultSettingParam.SetAnnotParam(param);
  293. splitSettingParam.SetAnnotParam(param);
  294. }
  295. public void SetIsShowStampMouse(bool isShow)
  296. {
  297. viewerTool?.GetCPDFViewer()?.SetIsShowStampMouse(isShow);
  298. splitViewerTool?.GetCPDFViewer()?.SetIsShowStampMouse(isShow);
  299. }
  300. public void SetDrawModes(DrawModeData drawMode)
  301. {
  302. viewerTool?.GetCPDFViewer()?.SetDrawModes(drawMode.DrawMode);
  303. splitViewerTool?.GetCPDFViewer()?.SetDrawModes(drawMode.DrawMode);
  304. if (drawMode.DrawMode == DrawMode.Custom)
  305. {
  306. viewerTool?.GetCPDFViewer().SetPDFBackground(drawMode.CustomColor);
  307. splitViewerTool?.GetCPDFViewer().SetPDFBackground(drawMode.CustomColor);
  308. }
  309. UpdateRenderFrame();
  310. }
  311. public void SetStampMouseImage(byte[] imageArray, int imageWidth, int imageHeight)
  312. {
  313. viewerTool?.GetCPDFViewer()?.SetStampMouseImage(imageArray, imageWidth, imageHeight);
  314. splitViewerTool?.GetCPDFViewer()?.SetStampMouseImage(imageArray, imageWidth, imageHeight);
  315. }
  316. public bool SetStampMouseImage(string filePath)
  317. {
  318. if ((bool)(viewerTool?.GetCPDFViewer()?.SetStampMouseImage(filePath)) && (bool)splitViewerTool?.GetCPDFViewer()?.SetStampMouseImage(filePath))
  319. {
  320. return true;
  321. }
  322. return false;
  323. }
  324. public void SetIsVisibleCustomMouse(bool isVisbleCustomMouse)
  325. {
  326. viewerTool?.GetCPDFViewer()?.SetIsVisibleCustomMouse(isVisbleCustomMouse);
  327. splitViewerTool?.GetCPDFViewer()?.SetIsVisibleCustomMouse(isVisbleCustomMouse);
  328. }
  329. public CPDFViewer GetCPDFViewer()
  330. {
  331. return FocusPDFViewTool?.GetCPDFViewer();
  332. }
  333. public BaseWidget GetCacheHitTestWidget()
  334. {
  335. return FocusPDFViewTool?.GetCacheHitTestWidget();
  336. }
  337. public BaseAnnot GetCacheHitTestAnnot()
  338. {
  339. return FocusPDFViewTool?.GetCacheHitTestAnnot();
  340. }
  341. public void CreateViewerMenu(object sender, ref ContextMenu contextMenu)
  342. {
  343. MenuItem fitWidthMenu = new MenuItem();
  344. fitWidthMenu.Header = LanguageHelper.CommonManager.GetString("Menu_AutoSize");
  345. fitWidthMenu.Click += (o, p) =>
  346. {
  347. FocusPDFViewTool?.GetCPDFViewer()?.SetFitMode(FitMode.FitWidth);
  348. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  349. };
  350. contextMenu.Items.Add(fitWidthMenu);
  351. MenuItem fitSizeMenu = new MenuItem();
  352. fitSizeMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RealSize");
  353. fitSizeMenu.Click += (o, p) =>
  354. {
  355. FocusPDFViewTool?.GetCPDFViewer()?.SetFitMode(FitMode.FitOriginal);
  356. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  357. };
  358. contextMenu.Items.Add(fitSizeMenu);
  359. MenuItem zoomInMenu = new MenuItem();
  360. zoomInMenu.Header = LanguageHelper.CommonManager.GetString("Menu_ZoomIn");
  361. zoomInMenu.Click += (o, p) =>
  362. {
  363. FocusPDFViewTool?.GetCPDFViewer()?.SetFitMode(FitMode.FitZoom);
  364. double zoom = FocusPDFViewTool?.GetCPDFViewer()?.GetZoom() ?? 1;
  365. FocusPDFViewTool?.GetCPDFViewer()?.SetZoom(CheckZoomLevel(zoom, true));
  366. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  367. };
  368. contextMenu.Items.Add(zoomInMenu);
  369. MenuItem zoomOutMenu = new MenuItem();
  370. zoomOutMenu.Header = LanguageHelper.CommonManager.GetString("Menu_ZoomOut");
  371. zoomOutMenu.Click += (o, p) =>
  372. {
  373. FocusPDFViewTool?.GetCPDFViewer()?.SetFitMode(FitMode.FitZoom);
  374. double zoom = FocusPDFViewTool?.GetCPDFViewer()?.GetZoom() ?? 1;
  375. FocusPDFViewTool?.GetCPDFViewer()?.SetZoom(CheckZoomLevel(zoom, false));
  376. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  377. };
  378. contextMenu.Items.Add(zoomOutMenu);
  379. contextMenu.Items.Add(new Separator());
  380. MenuItem singleView = new MenuItem();
  381. singleView.Header = LanguageHelper.CommonManager.GetString("Menu_SinglePage");
  382. singleView.Click += (o, p) =>
  383. {
  384. FocusPDFViewTool?.GetCPDFViewer()?.SetViewMode(ViewMode.Single);
  385. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  386. };
  387. contextMenu.Items.Add(singleView);
  388. MenuItem singleContinuousView = new MenuItem();
  389. singleContinuousView.Header = LanguageHelper.CommonManager.GetString("Menu_SingleContinuous");
  390. singleContinuousView.Click += (o, p) =>
  391. {
  392. FocusPDFViewTool?.GetCPDFViewer()?.SetViewMode(ViewMode.SingleContinuous);
  393. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  394. };
  395. contextMenu.Items.Add(singleContinuousView);
  396. MenuItem doubleView = new MenuItem();
  397. doubleView.Header = LanguageHelper.CommonManager.GetString("Menu_DoublePage");
  398. doubleView.Click += (o, p) =>
  399. {
  400. FocusPDFViewTool?.GetCPDFViewer()?.SetViewMode(ViewMode.Double);
  401. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  402. };
  403. contextMenu.Items.Add(doubleView);
  404. MenuItem doubleContinuousView = new MenuItem();
  405. doubleContinuousView.Header = LanguageHelper.CommonManager.GetString("Menu_DoubleContinuous");
  406. doubleContinuousView.Click += (o, p) =>
  407. {
  408. FocusPDFViewTool?.GetCPDFViewer()?.SetViewMode(ViewMode.DoubleContinuous);
  409. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  410. };
  411. contextMenu.Items.Add(doubleContinuousView);
  412. contextMenu.Items.Add(new Separator());
  413. MenuItem resetFormMenu = new MenuItem();
  414. resetFormMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Reset");
  415. resetFormMenu.Click += (o, p) =>
  416. {
  417. //FocusPDFViewTool?.GetCPDFViewer()?.ResetForm();
  418. };
  419. contextMenu.Items.Add(resetFormMenu);
  420. }
  421. private void RemoveSplitViewerTool()
  422. {
  423. UpdateRenderFrame();
  424. VerticalSplitter.Visibility = Visibility.Collapsed;
  425. HorizontalSplitter.Visibility = Visibility.Collapsed;
  426. VerticalView.Visibility = Visibility.Collapsed;
  427. HorizontalView.Visibility = Visibility.Collapsed;
  428. ViewToolGrid.ColumnDefinitions[1].Width = new GridLength(0);
  429. ViewToolGrid.ColumnDefinitions[2].Width = new GridLength(0);
  430. ViewToolGrid.RowDefinitions[1].Height = new GridLength(0);
  431. ViewToolGrid.RowDefinitions[2].Height = new GridLength(0);
  432. VerticalView.Child = null;
  433. HorizontalView.Child = null;
  434. }
  435. public void WindowSizeChange()
  436. {
  437. if (VerticalView.Visibility == Visibility.Visible)
  438. {
  439. ViewToolGrid.ColumnDefinitions[2].Width = new GridLength(ViewToolGrid.ActualWidth * splitWidthScale);
  440. }
  441. if (HorizontalView.Visibility == Visibility.Visible)
  442. {
  443. ViewToolGrid.RowDefinitions[2].Height = new GridLength(ViewToolGrid.ActualHeight * splitHeightScale);
  444. }
  445. }
  446. private void PDFView_GotFocus(object sender, RoutedEventArgs e)
  447. {
  448. switch ((sender as Border).Tag)
  449. {
  450. case "ViewerTool":
  451. FocusPDFViewTool = viewerTool;
  452. break;
  453. case "SplitViewerTool":
  454. FocusPDFViewTool = splitViewerTool;
  455. break;
  456. }
  457. if (FocusPDFViewTool.IsLoaded)
  458. {
  459. FocusPDFViewToolChanged?.Invoke(this, EventArgs.Empty);
  460. }
  461. }
  462. #region Private Command Methods
  463. private double CheckZoomLevel(double zoom, bool IsGrowth)
  464. {
  465. zoom += (IsGrowth ? 0.01 : -0.01);
  466. double standardZoom = 100;
  467. if (zoom <= 0.01)
  468. {
  469. return 0.01;
  470. }
  471. if (zoom >= 10)
  472. {
  473. return 10;
  474. }
  475. zoom *= 100;
  476. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  477. {
  478. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  479. {
  480. standardZoom = zoomLevelList[i + 1];
  481. break;
  482. }
  483. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  484. {
  485. standardZoom = zoomLevelList[i];
  486. break;
  487. }
  488. }
  489. return standardZoom / 100;
  490. }
  491. #endregion
  492. #region automatic scroll
  493. internal bool EnableAutoScroll { get; set; } = true;
  494. public bool IsHitEmpty
  495. {
  496. get;
  497. private set;
  498. }
  499. private bool cancelTask = false;
  500. private bool scrollEnable = true;
  501. private bool isAutomaticScroll = false;
  502. private Point mouseCurrentPoint;
  503. private Point middlePressPoint;
  504. private Point middleMovePoint;
  505. private Point hitEmptyPos;
  506. private Task MiddleScrollTask;
  507. private void PDFView_MouseDown(object sender, MouseButtonEventArgs e)
  508. {
  509. switch ((sender as Border).Tag)
  510. {
  511. case "ViewerTool":
  512. FocusPDFViewTool = viewerTool;
  513. splitToolManager.ClearSelect();
  514. break;
  515. case "SplitViewerTool":
  516. FocusPDFViewTool = splitViewerTool;
  517. toolManager.ClearSelect();
  518. break;
  519. }
  520. if (FocusPDFViewTool.IsLoaded && FocusPDFViewTool.GetCPDFViewer()?.CurrentRenderFrame != null)
  521. {
  522. FocusPDFViewToolChanged?.Invoke(this, EventArgs.Empty);
  523. }
  524. }
  525. private void PDFViewControl_MouseMove(object sender, MouseEventArgs e)
  526. {
  527. if (isAutomaticScroll)
  528. {
  529. middleMovePoint = e.GetPosition(this);
  530. e.Handled = true;
  531. return;
  532. }
  533. if (e.LeftButton == MouseButtonState.Pressed && IsHitEmpty)
  534. {
  535. Point currentPoint = e.GetPosition(this);
  536. Vector offset = currentPoint - hitEmptyPos;
  537. if (Math.Abs(offset.X) > 10)
  538. {
  539. GetCPDFViewer()?.SetHorizontalOffset(GetCPDFViewer().HorizontalOffset - offset.X);
  540. hitEmptyPos.X = currentPoint.X;
  541. }
  542. if (Math.Abs(offset.Y) > 10)
  543. {
  544. GetCPDFViewer()?.SetVerticalOffset(GetCPDFViewer().VerticalOffset - offset.Y);
  545. hitEmptyPos.Y = currentPoint.Y;
  546. }
  547. GetCPDFViewer().Cursor = panTool2Cursor;
  548. e.Handled = true;
  549. return;
  550. }
  551. if (e.LeftButton == MouseButtonState.Released && viewerTool != null)
  552. {
  553. List<ToolType> toolTypes = new List<ToolType>
  554. {
  555. ToolType.Viewer,
  556. ToolType.Pan
  557. };
  558. ToolType currentMode = PDFViewTool.GetToolType();
  559. bool cursorSet = false;
  560. Cursor newCursor = GetCPDFViewer().Cursor;
  561. if (toolTypes.Contains(currentMode))
  562. {
  563. BaseAnnot hitAnnot = GetCPDFViewer().AnnotHitTest();
  564. if (hitAnnot != null)
  565. {
  566. if (hitAnnot is LinkAnnot || hitAnnot is BaseWidget)
  567. {
  568. newCursor = Cursors.Hand;
  569. cursorSet = true;
  570. }
  571. else
  572. {
  573. newCursor = annotEditCursor;
  574. cursorSet = true;
  575. }
  576. }
  577. else
  578. {
  579. if (!PDFViewTool.IsSelectRectMousePoint())
  580. {
  581. newCursor = (PDFToolManager.GetToolType() == ToolType.Pan) ? panToolCursor : Cursors.Arrow;
  582. if (PDFViewTool.IsText())
  583. {
  584. newCursor = Cursors.IBeam;
  585. }
  586. cursorSet = true;
  587. }
  588. }
  589. if (cursorSet)
  590. {
  591. e.Handled = true;
  592. }
  593. }
  594. if (currentMode == ToolType.CreateAnnot)
  595. {
  596. List<C_ANNOTATION_TYPE> annotTypes = new List<C_ANNOTATION_TYPE>
  597. {
  598. C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT,
  599. C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE,
  600. C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY,
  601. C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT
  602. };
  603. if (GetCPDFViewer().AnnotHitTest() == null && PDFViewTool.IsText() && annotTypes.Contains(toolManager.GetAnnotType()))
  604. {
  605. newCursor = Cursors.IBeam;
  606. }
  607. else
  608. {
  609. if (newCursor == Cursors.IBeam || newCursor == panToolCursor || newCursor == Cursors.Arrow)
  610. {
  611. newCursor = annotEditCursor;
  612. }
  613. }
  614. }
  615. if (newCursor != GetCPDFViewer().Cursor)
  616. {
  617. GetCPDFViewer().Cursor = newCursor;
  618. }
  619. }
  620. }
  621. protected override void OnMouseDown(MouseButtonEventArgs e)
  622. {
  623. if (e.MiddleButton == MouseButtonState.Pressed)
  624. {
  625. isAutomaticScroll ^= true;
  626. }
  627. else
  628. {
  629. isAutomaticScroll = false;
  630. }
  631. if (isAutomaticScroll)
  632. {
  633. GetCPDFViewer().Cursor = Cursors.ScrollAll;
  634. middlePressPoint = e.GetPosition(this);
  635. middleMovePoint = middlePressPoint;
  636. if (MiddleScrollTask != null && MiddleScrollTask.Status == TaskStatus.Running)
  637. {
  638. cancelTask = true;
  639. MiddleScrollTask.Wait();
  640. }
  641. cancelTask = false;
  642. MiddleScrollTask = Task.Factory.StartNew(() => DoScrollWork());
  643. }
  644. }
  645. private void DoScrollWork()
  646. {
  647. try
  648. {
  649. while (!cancelTask && isAutomaticScroll)
  650. {
  651. Vector subSize = middleMovePoint - middlePressPoint;
  652. Dispatcher.Invoke(() =>
  653. {
  654. GetCPDFViewer().SetVerticalOffset(GetCPDFViewer().VerticalOffset + subSize.Y);
  655. GetCPDFViewer().SetHorizontalOffset(GetCPDFViewer().HorizontalOffset + subSize.X);
  656. });
  657. Thread.Sleep(100);
  658. }
  659. Dispatcher.Invoke(() =>
  660. {
  661. if (GetCPDFViewer().Cursor == Cursors.ScrollAll)
  662. {
  663. GetCPDFViewer().Cursor = Cursors.Arrow;
  664. }
  665. });
  666. }
  667. catch (Exception ex)
  668. {
  669. MiddleScrollTask = null;
  670. }
  671. }
  672. #endregion
  673. #region Hand
  674. private Cursor panToolCursor;
  675. private Cursor panTool2Cursor;
  676. private Cursor annotEditCursor;
  677. private Cursor LoadCursor(string cursorResource)
  678. {
  679. var assembly = Assembly.GetExecutingAssembly();
  680. Stream stream = assembly.GetManifestResourceStream(cursorResource);
  681. return new Cursor(stream);
  682. }
  683. private void SetCursor()
  684. {
  685. SetPanToolCursor(LoadCursor("ComPDFKit.Controls.Asset.Cursor.PanTool.cur"));
  686. SetPanTool2Cursor(LoadCursor("ComPDFKit.Controls.Asset.Cursor.PanTool2.cur"));
  687. SetAnnotEditCursor(LoadCursor("ComPDFKit.Controls.Asset.Cursor.AnnotEdit.cur"));
  688. }
  689. public void SetPanToolCursor(Cursor cursor)
  690. {
  691. panToolCursor = cursor;
  692. }
  693. public void SetPanTool2Cursor(Cursor cursor)
  694. {
  695. panTool2Cursor = cursor;
  696. }
  697. public void SetAnnotEditCursor(Cursor cursor)
  698. {
  699. annotEditCursor = cursor;
  700. }
  701. #endregion
  702. }
  703. }