PDFViewControl.xaml.cs 32 KB

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