PDFViewControl.xaml.cs 33 KB

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