PDFViewControl.xaml.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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. GetCPDFViewer().OnRenderFinish -= PDFViewControl_OnRenderFinish;
  101. GetCPDFViewer().OnRenderFinish += PDFViewControl_OnRenderFinish;
  102. SetCursor();
  103. }
  104. private void PDFViewControl_OnRenderFinish(object sender, EventArgs e)
  105. {
  106. SetCursorStatus();
  107. }
  108. private void PDFViewTool_DrawChanged(object sender, EventArgs e)
  109. {
  110. FocusPDFViewToolChanged?.Invoke(this, EventArgs.Empty);
  111. DrawChanged?.Invoke(sender, e);
  112. }
  113. private void PDFViewTool_PageSelectedChanged(object sender, PageSelectedData e)
  114. {
  115. SnapshotData = 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<ToolType> toolTypes = new List<ToolType>()
  148. {
  149. ToolType.Viewer,
  150. ToolType.Pan
  151. };
  152. ToolType currentMode = PDFViewTool.GetToolType();
  153. if (GetCPDFViewer().Cursor == panToolCursor || GetCPDFViewer().Cursor == panTool2Cursor)
  154. {
  155. if (toolTypes.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 && e.hitTestType != MouseHitTestType.Widget)
  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. }
  184. private void SplitViewerTool_SizeChanged(object sender, SizeChangedEventArgs e)
  185. {
  186. if (VerticalView.Visibility == Visibility.Visible)
  187. {
  188. splitWidthScale = VerticalView.ActualWidth / ViewToolGrid.ActualWidth;
  189. }
  190. if (HorizontalView.Visibility == Visibility.Visible)
  191. {
  192. splitHeightScale = HorizontalView.ActualHeight / ViewToolGrid.ActualHeight;
  193. }
  194. }
  195. public void InitDocument(string Path)
  196. {
  197. CPDFDocument pdfDoc = CPDFDocument.InitWithFilePath(Path);
  198. if (pdfDoc != null)
  199. {
  200. PDFViewTool.GetCPDFViewer().InitDoc(pdfDoc);
  201. PDFViewTool.GetCPDFViewer().SetFitMode(FitMode.FitHeight);
  202. PDFViewTool.GetCPDFViewer().SetViewMode(ViewMode.SingleContinuous);
  203. PDFViewTool.SetIsMultiSelected(true);
  204. splitViewerTool.GetCPDFViewer().InitDoc(pdfDoc);
  205. splitViewerTool.GetCPDFViewer().SetFitMode(FitMode.FitHeight);
  206. splitViewerTool.GetCPDFViewer().SetViewMode(ViewMode.SingleContinuous);
  207. }
  208. }
  209. private void PDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
  210. {
  211. FocusPDFViewTool = PDFViewTool;
  212. FocusPDFViewToolChanged?.Invoke(this, EventArgs.Empty);
  213. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  214. {
  215. e.IsZoom = true;
  216. PDFViewTool.GetCPDFViewer().SetFitMode(FitMode.FitZoom);
  217. double zoom = PDFViewTool.GetCPDFViewer().GetZoom();
  218. PDFViewTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior)));
  219. PDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  220. }
  221. MouseWheelZoomHandler?.Invoke(this, e);
  222. }
  223. private void SplitPDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
  224. {
  225. FocusPDFViewTool = splitViewerTool;
  226. FocusPDFViewToolChanged?.Invoke(this, EventArgs.Empty);
  227. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  228. {
  229. e.IsZoom = true;
  230. splitViewerTool.GetCPDFViewer().SetFitMode(FitMode.FitZoom);
  231. double zoom = splitViewerTool.GetCPDFViewer().GetZoom();
  232. splitViewerTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior)));
  233. splitViewerTool?.GetCPDFViewer()?.UpdateRenderFrame();
  234. }
  235. MouseWheelZoomHandler?.Invoke(this, e);
  236. }
  237. public void SetSplitViewMode(SplitMode splitMode)
  238. {
  239. switch (splitMode)
  240. {
  241. case SplitMode.None:
  242. RemoveSplitViewerTool();
  243. break;
  244. case SplitMode.Vertical:
  245. RemoveSplitViewerTool();
  246. if (splitViewerTool != null)
  247. {
  248. VerticalSplitter.Visibility = Visibility.Visible;
  249. VerticalView.Visibility = Visibility.Visible;
  250. ViewToolGrid.ColumnDefinitions[1].Width = new GridLength(15, GridUnitType.Auto);
  251. ViewToolGrid.ColumnDefinitions[2].Width = new GridLength(ViewToolGrid.ActualWidth / 2);
  252. VerticalView.Child = splitViewerTool;
  253. }
  254. break;
  255. case SplitMode.Horizontal:
  256. RemoveSplitViewerTool();
  257. if (splitViewerTool != null)
  258. {
  259. HorizontalSplitter.Visibility = Visibility.Visible;
  260. HorizontalView.Visibility = Visibility.Visible;
  261. ViewToolGrid.RowDefinitions[1].Height = new GridLength(15, GridUnitType.Auto);
  262. ViewToolGrid.RowDefinitions[2].Height = new GridLength(ViewToolGrid.ActualHeight / 2);
  263. HorizontalView.Child = splitViewerTool;
  264. }
  265. break;
  266. }
  267. UpdateRenderFrame();
  268. UpdateAnnotFrame();
  269. }
  270. public void UpdateRenderFrame()
  271. {
  272. viewerTool?.GetCPDFViewer()?.UpdateRenderFrame();
  273. splitViewerTool?.GetCPDFViewer()?.UpdateRenderFrame();
  274. }
  275. public void UpdateAnnotFrame()
  276. {
  277. viewerTool?.GetCPDFViewer()?.UpdateAnnotFrame();
  278. splitViewerTool?.GetCPDFViewer()?.UpdateAnnotFrame();
  279. }
  280. public void SetCropMode(bool isCrop)
  281. {
  282. viewerTool?.GetCPDFViewer()?.SetIsCrop(isCrop);
  283. splitViewerTool?.GetCPDFViewer()?.SetIsCrop(isCrop);
  284. viewerTool?.GetCPDFViewer()?.UpdateVirtualNodes();
  285. splitViewerTool?.GetCPDFViewer()?.UpdateVirtualNodes();
  286. viewerTool?.GetCPDFViewer()?.UpdateRenderFrame();
  287. splitViewerTool?.GetCPDFViewer()?.UpdateRenderFrame();
  288. }
  289. public void SetToolType(ToolType type)
  290. {
  291. toolManager?.SetToolType(type);
  292. splitToolManager?.SetToolType(type);
  293. }
  294. public void SetCreateAnnotType(C_ANNOTATION_TYPE type)
  295. {
  296. toolManager?.SetCreateAnnotType(type);
  297. splitToolManager?.SetCreateAnnotType(type);
  298. }
  299. public void SetCreateWidgetType(C_WIDGET_TYPE type)
  300. {
  301. toolManager?.SetCreateWidgetType(type);
  302. splitToolManager?.SetCreateWidgetType(type);
  303. }
  304. public void SetAnnotParam(AnnotParam param)
  305. {
  306. DefaultSettingParam defaultSettingParam = viewerTool.GetDefaultSettingParam();
  307. DefaultSettingParam splitSettingParam = splitViewerTool.GetDefaultSettingParam();
  308. defaultSettingParam.SetAnnotParam(param);
  309. splitSettingParam.SetAnnotParam(param);
  310. }
  311. public void SetIsShowStampMouse(bool isShow)
  312. {
  313. viewerTool?.GetCPDFViewer()?.SetIsShowStampMouse(isShow);
  314. splitViewerTool?.GetCPDFViewer()?.SetIsShowStampMouse(isShow);
  315. }
  316. public void SetDrawModes(DrawModeData drawMode)
  317. {
  318. viewerTool?.GetCPDFViewer()?.SetDrawModes(drawMode.DrawMode);
  319. splitViewerTool?.GetCPDFViewer()?.SetDrawModes(drawMode.DrawMode);
  320. if (drawMode.DrawMode == DrawMode.Custom)
  321. {
  322. viewerTool?.GetCPDFViewer().SetPDFBackground(drawMode.CustomColor);
  323. splitViewerTool?.GetCPDFViewer().SetPDFBackground(drawMode.CustomColor);
  324. }
  325. UpdateRenderFrame();
  326. }
  327. public void SetStampMouseImage(byte[] imageArray, int imageWidth, int imageHeight)
  328. {
  329. viewerTool?.GetCPDFViewer()?.SetStampMouseImage(imageArray, imageWidth, imageHeight);
  330. splitViewerTool?.GetCPDFViewer()?.SetStampMouseImage(imageArray, imageWidth, imageHeight);
  331. }
  332. public bool SetStampMouseImage(string filePath)
  333. {
  334. if ((bool)(viewerTool?.GetCPDFViewer()?.SetStampMouseImage(filePath)) && (bool)splitViewerTool?.GetCPDFViewer()?.SetStampMouseImage(filePath))
  335. {
  336. return true;
  337. }
  338. return false;
  339. }
  340. public void SetIsVisibleCustomMouse(bool isVisbleCustomMouse)
  341. {
  342. viewerTool?.GetCPDFViewer()?.SetIsVisibleCustomMouse(isVisbleCustomMouse);
  343. splitViewerTool?.GetCPDFViewer()?.SetIsVisibleCustomMouse(isVisbleCustomMouse);
  344. }
  345. public CPDFViewer GetCPDFViewer()
  346. {
  347. return FocusPDFViewTool?.GetCPDFViewer();
  348. }
  349. public BaseWidget GetCacheHitTestWidget()
  350. {
  351. return FocusPDFViewTool?.GetCacheHitTestWidget();
  352. }
  353. public BaseAnnot GetCacheHitTestAnnot()
  354. {
  355. return FocusPDFViewTool?.GetCacheHitTestAnnot();
  356. }
  357. public void CreateViewerMenu(object sender, ref ContextMenu contextMenu)
  358. {
  359. MenuItem fitWidthMenu = new MenuItem();
  360. fitWidthMenu.Header = LanguageHelper.CommonManager.GetString("Menu_AutoSize");
  361. fitWidthMenu.Click += (o, p) =>
  362. {
  363. FocusPDFViewTool?.GetCPDFViewer()?.SetFitMode(FitMode.FitWidth);
  364. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  365. };
  366. contextMenu.Items.Add(fitWidthMenu);
  367. MenuItem fitSizeMenu = new MenuItem();
  368. fitSizeMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RealSize");
  369. fitSizeMenu.Click += (o, p) =>
  370. {
  371. FocusPDFViewTool?.GetCPDFViewer()?.SetFitMode(FitMode.FitOriginal);
  372. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  373. };
  374. contextMenu.Items.Add(fitSizeMenu);
  375. MenuItem zoomInMenu = new MenuItem();
  376. zoomInMenu.Header = LanguageHelper.CommonManager.GetString("Menu_ZoomIn");
  377. zoomInMenu.Click += (o, p) =>
  378. {
  379. FocusPDFViewTool?.GetCPDFViewer()?.SetFitMode(FitMode.FitZoom);
  380. double zoom = FocusPDFViewTool?.GetCPDFViewer()?.GetZoom() ?? 1;
  381. FocusPDFViewTool?.GetCPDFViewer()?.SetZoom(CheckZoomLevel(zoom, true));
  382. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  383. };
  384. contextMenu.Items.Add(zoomInMenu);
  385. MenuItem zoomOutMenu = new MenuItem();
  386. zoomOutMenu.Header = LanguageHelper.CommonManager.GetString("Menu_ZoomOut");
  387. zoomOutMenu.Click += (o, p) =>
  388. {
  389. FocusPDFViewTool?.GetCPDFViewer()?.SetFitMode(FitMode.FitZoom);
  390. double zoom = FocusPDFViewTool?.GetCPDFViewer()?.GetZoom() ?? 1;
  391. FocusPDFViewTool?.GetCPDFViewer()?.SetZoom(CheckZoomLevel(zoom, false));
  392. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  393. };
  394. contextMenu.Items.Add(zoomOutMenu);
  395. contextMenu.Items.Add(new Separator());
  396. MenuItem singleView = new MenuItem();
  397. singleView.Header = LanguageHelper.CommonManager.GetString("Menu_SinglePage");
  398. singleView.Click += (o, p) =>
  399. {
  400. FocusPDFViewTool?.GetCPDFViewer()?.SetViewMode(ViewMode.Single);
  401. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  402. };
  403. contextMenu.Items.Add(singleView);
  404. MenuItem singleContinuousView = new MenuItem();
  405. singleContinuousView.Header = LanguageHelper.CommonManager.GetString("Menu_SingleContinuous");
  406. singleContinuousView.Click += (o, p) =>
  407. {
  408. FocusPDFViewTool?.GetCPDFViewer()?.SetViewMode(ViewMode.SingleContinuous);
  409. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  410. };
  411. contextMenu.Items.Add(singleContinuousView);
  412. MenuItem doubleView = new MenuItem();
  413. doubleView.Header = LanguageHelper.CommonManager.GetString("Menu_DoublePage");
  414. doubleView.Click += (o, p) =>
  415. {
  416. FocusPDFViewTool?.GetCPDFViewer()?.SetViewMode(ViewMode.Double);
  417. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  418. };
  419. contextMenu.Items.Add(doubleView);
  420. MenuItem doubleContinuousView = new MenuItem();
  421. doubleContinuousView.Header = LanguageHelper.CommonManager.GetString("Menu_DoubleContinuous");
  422. doubleContinuousView.Click += (o, p) =>
  423. {
  424. FocusPDFViewTool?.GetCPDFViewer()?.SetViewMode(ViewMode.DoubleContinuous);
  425. FocusPDFViewTool?.GetCPDFViewer()?.UpdateRenderFrame();
  426. };
  427. contextMenu.Items.Add(doubleContinuousView);
  428. contextMenu.Items.Add(new Separator());
  429. MenuItem resetFormMenu = new MenuItem();
  430. resetFormMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Reset");
  431. resetFormMenu.Click += (o, p) =>
  432. {
  433. //FocusPDFViewTool?.GetCPDFViewer()?.ResetForm();
  434. };
  435. contextMenu.Items.Add(resetFormMenu);
  436. }
  437. private void RemoveSplitViewerTool()
  438. {
  439. UpdateRenderFrame();
  440. VerticalSplitter.Visibility = Visibility.Collapsed;
  441. HorizontalSplitter.Visibility = Visibility.Collapsed;
  442. VerticalView.Visibility = Visibility.Collapsed;
  443. HorizontalView.Visibility = Visibility.Collapsed;
  444. ViewToolGrid.ColumnDefinitions[1].Width = new GridLength(0);
  445. ViewToolGrid.ColumnDefinitions[2].Width = new GridLength(0);
  446. ViewToolGrid.RowDefinitions[1].Height = new GridLength(0);
  447. ViewToolGrid.RowDefinitions[2].Height = new GridLength(0);
  448. VerticalView.Child = null;
  449. HorizontalView.Child = null;
  450. }
  451. public void WindowSizeChange()
  452. {
  453. if (VerticalView.Visibility == Visibility.Visible)
  454. {
  455. ViewToolGrid.ColumnDefinitions[2].Width = new GridLength(ViewToolGrid.ActualWidth * splitWidthScale);
  456. }
  457. if (HorizontalView.Visibility == Visibility.Visible)
  458. {
  459. ViewToolGrid.RowDefinitions[2].Height = new GridLength(ViewToolGrid.ActualHeight * splitHeightScale);
  460. }
  461. }
  462. private void PDFView_GotFocus(object sender, RoutedEventArgs e)
  463. {
  464. switch ((sender as Border).Tag)
  465. {
  466. case "ViewerTool":
  467. FocusPDFViewTool = viewerTool;
  468. break;
  469. case "SplitViewerTool":
  470. FocusPDFViewTool = splitViewerTool;
  471. break;
  472. }
  473. if (FocusPDFViewTool.IsLoaded)
  474. {
  475. FocusPDFViewToolChanged?.Invoke(this, EventArgs.Empty);
  476. }
  477. }
  478. private void ReloadDocument()
  479. {
  480. viewerTool?.GetCPDFViewer()?.UpdateVirtualNodes();
  481. splitViewerTool?.GetCPDFViewer()?.UpdateVirtualNodes();
  482. viewerTool?.GetCPDFViewer()?.UpdateRenderFrame();
  483. splitViewerTool?.GetCPDFViewer()?.UpdateRenderFrame();
  484. }
  485. public void CropPage(CPDFDisplayBox cropBox, Rect cropRect, List<int> pagesList)
  486. {
  487. bool needLoad = false;
  488. CPDFDocument doc = FocusPDFViewTool.GetCPDFViewer().GetDocument();
  489. foreach (int pageIndex in pagesList)
  490. {
  491. if (pageIndex < 0 || pageIndex > doc.PageCount)
  492. {
  493. continue;
  494. }
  495. CPDFPage CropPage = doc.PageAtIndex(pageIndex);
  496. CRect rect = new CRect((float)cropRect.X, (float)(cropRect.Y + cropRect.Height), (float)(cropRect.X + cropRect.Width), (float)cropRect.Y);
  497. CropPage.CropPage(cropBox, rect);
  498. CropPage.ReleaseAllAnnotations();
  499. needLoad = true;
  500. }
  501. if (needLoad)
  502. {
  503. ReloadDocument();
  504. }
  505. }
  506. #region Private Command Methods
  507. private double CheckZoomLevel(double zoom, bool IsGrowth)
  508. {
  509. zoom += (IsGrowth ? 0.01 : -0.01);
  510. double standardZoom = 100;
  511. if (zoom <= 0.01)
  512. {
  513. return 0.01;
  514. }
  515. if (zoom >= 10)
  516. {
  517. return 10;
  518. }
  519. zoom *= 100;
  520. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  521. {
  522. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  523. {
  524. standardZoom = zoomLevelList[i + 1];
  525. break;
  526. }
  527. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  528. {
  529. standardZoom = zoomLevelList[i];
  530. break;
  531. }
  532. }
  533. return standardZoom / 100;
  534. }
  535. #endregion
  536. #region automatic scroll
  537. internal bool EnableAutoScroll { get; set; } = true;
  538. public bool IsHitEmpty
  539. {
  540. get;
  541. private set;
  542. }
  543. private bool cancelTask = false;
  544. private bool scrollEnable = true;
  545. private bool isAutomaticScroll = false;
  546. private Point mouseCurrentPoint;
  547. private Point middlePressPoint;
  548. private Point middleMovePoint;
  549. private Point hitEmptyPos;
  550. private Task MiddleScrollTask;
  551. private void PDFView_MouseDown(object sender, MouseButtonEventArgs e)
  552. {
  553. switch ((sender as Border).Tag)
  554. {
  555. case "ViewerTool":
  556. FocusPDFViewTool = viewerTool;
  557. splitToolManager.ClearSelect();
  558. break;
  559. case "SplitViewerTool":
  560. FocusPDFViewTool = splitViewerTool;
  561. toolManager.ClearSelect();
  562. break;
  563. }
  564. if (FocusPDFViewTool.IsLoaded && FocusPDFViewTool.GetCPDFViewer()?.CurrentRenderFrame != null)
  565. {
  566. FocusPDFViewToolChanged?.Invoke(this, EventArgs.Empty);
  567. }
  568. }
  569. private void SetCursorStatus()
  570. {
  571. List<ToolType> toolTypes = new List<ToolType>
  572. {
  573. ToolType.Viewer,
  574. ToolType.Pan
  575. };
  576. ToolType currentMode = PDFViewTool.GetToolType();
  577. Cursor newCursor = Cursors.Arrow;
  578. if (toolTypes.Contains(currentMode))
  579. {
  580. BaseAnnot hitAnnot = GetCPDFViewer().AnnotHitTest();
  581. if (hitAnnot != null)
  582. {
  583. if (hitAnnot is LinkAnnot || hitAnnot is BaseWidget)
  584. {
  585. newCursor = Cursors.Hand;
  586. }
  587. else
  588. {
  589. newCursor = annotEditCursor;
  590. }
  591. }
  592. else
  593. {
  594. if (!PDFViewTool.IsSelectRectMousePoint())
  595. {
  596. newCursor = (PDFToolManager.GetToolType() == ToolType.Pan) ? panToolCursor : Cursors.Arrow;
  597. if (PDFViewTool.IsText())
  598. {
  599. newCursor = Cursors.IBeam;
  600. }
  601. }
  602. }
  603. if (currentMode == ToolType.CreateAnnot)
  604. {
  605. List<C_ANNOTATION_TYPE> annotTypes = new List<C_ANNOTATION_TYPE>
  606. {
  607. C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT,
  608. C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE,
  609. C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY,
  610. C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT
  611. };
  612. if (GetCPDFViewer().AnnotHitTest() == null && PDFViewTool.IsText() && annotTypes.Contains(toolManager.GetAnnotType()))
  613. {
  614. newCursor = Cursors.IBeam;
  615. }
  616. else
  617. {
  618. if (newCursor == Cursors.IBeam || newCursor == panToolCursor || newCursor == Cursors.Arrow)
  619. {
  620. newCursor = annotEditCursor;
  621. }
  622. }
  623. }
  624. }
  625. GetCPDFViewer().Cursor = newCursor;
  626. }
  627. private void PDFViewControl_MouseMove(object sender, MouseEventArgs e)
  628. {
  629. if (GetCPDFViewer().IsRendering)
  630. {
  631. GetCPDFViewer().Cursor = Cursors.Wait;
  632. FocusPDFViewTool.ContextMenu?.Items.Clear();
  633. return;
  634. }
  635. if (isAutomaticScroll)
  636. {
  637. middleMovePoint = e.GetPosition(this);
  638. e.Handled = true;
  639. return;
  640. }
  641. if (e.LeftButton == MouseButtonState.Pressed && IsHitEmpty)
  642. {
  643. Point currentPoint = e.GetPosition(this);
  644. Vector offset = currentPoint - hitEmptyPos;
  645. if (Math.Abs(offset.X) > 10)
  646. {
  647. GetCPDFViewer()?.SetHorizontalOffset(GetCPDFViewer().HorizontalOffset - offset.X);
  648. hitEmptyPos.X = currentPoint.X;
  649. }
  650. if (Math.Abs(offset.Y) > 10)
  651. {
  652. GetCPDFViewer()?.SetVerticalOffset(GetCPDFViewer().VerticalOffset - offset.Y);
  653. hitEmptyPos.Y = currentPoint.Y;
  654. }
  655. GetCPDFViewer().Cursor = panTool2Cursor;
  656. e.Handled = true;
  657. return;
  658. }
  659. if (e.LeftButton == MouseButtonState.Released && viewerTool != null)
  660. {
  661. List<ToolType> toolTypes = new List<ToolType>
  662. {
  663. ToolType.Viewer,
  664. ToolType.Pan
  665. };
  666. ToolType currentMode = PDFViewTool.GetToolType();
  667. bool cursorSet = false;
  668. Cursor newCursor = GetCPDFViewer().Cursor;
  669. if (toolTypes.Contains(currentMode))
  670. {
  671. BaseAnnot hitAnnot = GetCPDFViewer().AnnotHitTest();
  672. if (hitAnnot != null)
  673. {
  674. if (hitAnnot is LinkAnnot || hitAnnot is BaseWidget)
  675. {
  676. newCursor = Cursors.Hand;
  677. cursorSet = true;
  678. }
  679. else
  680. {
  681. newCursor = annotEditCursor;
  682. cursorSet = true;
  683. }
  684. }
  685. else
  686. {
  687. if (!PDFViewTool.IsSelectRectMousePoint())
  688. {
  689. newCursor = (PDFToolManager.GetToolType() == ToolType.Pan) ? panToolCursor : Cursors.Arrow;
  690. if (PDFViewTool.IsText())
  691. {
  692. newCursor = Cursors.IBeam;
  693. }
  694. cursorSet = true;
  695. }
  696. }
  697. if (cursorSet)
  698. {
  699. e.Handled = true;
  700. }
  701. }
  702. if (currentMode == ToolType.CreateAnnot)
  703. {
  704. List<C_ANNOTATION_TYPE> annotTypes = new List<C_ANNOTATION_TYPE>
  705. {
  706. C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT,
  707. C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE,
  708. C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY,
  709. C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT
  710. };
  711. if (GetCPDFViewer().AnnotHitTest() == null && PDFViewTool.IsText() && annotTypes.Contains(toolManager.GetAnnotType()))
  712. {
  713. newCursor = Cursors.IBeam;
  714. }
  715. else
  716. {
  717. if (newCursor == Cursors.IBeam || newCursor == panToolCursor || newCursor == Cursors.Arrow)
  718. {
  719. newCursor = annotEditCursor;
  720. }
  721. }
  722. }
  723. if (newCursor != GetCPDFViewer().Cursor)
  724. {
  725. GetCPDFViewer().Cursor = newCursor;
  726. }
  727. }
  728. }
  729. protected override void OnMouseDown(MouseButtonEventArgs e)
  730. {
  731. if (e.MiddleButton == MouseButtonState.Pressed)
  732. {
  733. isAutomaticScroll ^= true;
  734. }
  735. else
  736. {
  737. isAutomaticScroll = false;
  738. }
  739. if (isAutomaticScroll)
  740. {
  741. GetCPDFViewer().Cursor = Cursors.ScrollAll;
  742. middlePressPoint = e.GetPosition(this);
  743. middleMovePoint = middlePressPoint;
  744. if (MiddleScrollTask != null && MiddleScrollTask.Status == TaskStatus.Running)
  745. {
  746. cancelTask = true;
  747. MiddleScrollTask.Wait();
  748. }
  749. cancelTask = false;
  750. MiddleScrollTask = Task.Factory.StartNew(() => DoScrollWork());
  751. }
  752. }
  753. private void DoScrollWork()
  754. {
  755. try
  756. {
  757. while (!cancelTask && isAutomaticScroll)
  758. {
  759. Vector subSize = middleMovePoint - middlePressPoint;
  760. Dispatcher.Invoke(() =>
  761. {
  762. GetCPDFViewer().SetVerticalOffset(GetCPDFViewer().VerticalOffset + subSize.Y);
  763. GetCPDFViewer().SetHorizontalOffset(GetCPDFViewer().HorizontalOffset + subSize.X);
  764. });
  765. Thread.Sleep(100);
  766. }
  767. Dispatcher.Invoke(() =>
  768. {
  769. if (GetCPDFViewer().Cursor == Cursors.ScrollAll)
  770. {
  771. GetCPDFViewer().Cursor = Cursors.Arrow;
  772. }
  773. });
  774. }
  775. catch (Exception ex)
  776. {
  777. MiddleScrollTask = null;
  778. }
  779. }
  780. #endregion
  781. #region Hand
  782. private Cursor panToolCursor;
  783. private Cursor panTool2Cursor;
  784. private Cursor annotEditCursor;
  785. private Cursor LoadCursor(string cursorResource)
  786. {
  787. var assembly = Assembly.GetExecutingAssembly();
  788. Stream stream = assembly.GetManifestResourceStream(cursorResource);
  789. return new Cursor(stream);
  790. }
  791. private void SetCursor()
  792. {
  793. SetPanToolCursor(LoadCursor("ComPDFKit.Controls.Asset.Cursor.PanTool.cur"));
  794. SetPanTool2Cursor(LoadCursor("ComPDFKit.Controls.Asset.Cursor.PanTool2.cur"));
  795. SetAnnotEditCursor(LoadCursor("ComPDFKit.Controls.Asset.Cursor.AnnotEdit.cur"));
  796. }
  797. public void SetPanToolCursor(Cursor cursor)
  798. {
  799. panToolCursor = cursor;
  800. }
  801. public void SetPanTool2Cursor(Cursor cursor)
  802. {
  803. panTool2Cursor = cursor;
  804. }
  805. public void SetAnnotEditCursor(Cursor cursor)
  806. {
  807. annotEditCursor = cursor;
  808. }
  809. #endregion
  810. }
  811. }