PDFViewControl.xaml.cs 32 KB

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