CPDFViewerTool.xaml.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit.PDFDocument.Action;
  4. using ComPDFKit.PDFPage;
  5. using ComPDFKit.Tool.DrawTool;
  6. using ComPDFKit.Tool.SettingParam;
  7. using ComPDFKit.Viewer.Helper;
  8. using ComPDFKitViewer;
  9. using ComPDFKitViewer.Annot;
  10. using ComPDFKitViewer.BaseObject;
  11. using ComPDFKitViewer.Helper;
  12. using ComPDFKitViewer.Layer;
  13. using ComPDFKitViewer.Widget;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Diagnostics;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using UndoAction = ComPDFKitViewer.Helper.UndoAction;
  22. namespace ComPDFKit.Tool
  23. {
  24. public struct MouseEventObject
  25. {
  26. public MouseEventArgs mouseButtonEventArgs;
  27. public MouseHitTestType hitTestType;
  28. public C_ANNOTATION_TYPE annotType;
  29. /// <summary>
  30. /// Identifies whether the object is created
  31. /// </summary>
  32. public bool IsCreate;
  33. public bool IsDrawing;
  34. public bool IsMersured;
  35. public object Data;
  36. }
  37. public enum MouseHitTestType
  38. {
  39. Unknown,
  40. Text,
  41. Annot,
  42. SelectRect,
  43. Widget,
  44. TextEdit,
  45. ImageEdit,
  46. PathEdit,
  47. ImageSelect,
  48. MultiTextEdit,
  49. SelectedPageRect,
  50. }
  51. public enum ToolType
  52. {
  53. None = -1,
  54. Viewer,
  55. Pan,
  56. CreateAnnot,
  57. WidgetEdit,
  58. ContentEdit,
  59. Customize,
  60. SelectedPage,
  61. }
  62. public partial class CPDFViewerTool : UserControl
  63. {
  64. public Cursor RotationCursor;
  65. public bool IsDocumentModified
  66. {
  67. get => isDocumentModified;
  68. set
  69. {
  70. isDocumentModified = value;
  71. DocumentModifiedChanged?.Invoke(this, EventArgs.Empty);
  72. }
  73. }
  74. public CPDFViewerTool()
  75. {
  76. InitializeComponent();
  77. BindCommand();
  78. Application.Current.Exit += Current_Exit;
  79. InsertSelectImageView();
  80. InsertAnnotView();
  81. InsertAnnotEditView();
  82. InsertWidgetView();
  83. InsertSelectedRectView();
  84. InsertMultiSelectedRectView();
  85. InsertCustomizeToolView();
  86. InsertSelectTextView();
  87. //Frame Select
  88. InsertFrameSelectToolView();
  89. InsertTextEditView();
  90. InsertPageSelectedRectView();
  91. }
  92. private void Current_Exit(object sender, ExitEventArgs e)
  93. {
  94. GetCPDFViewer().Dispose();
  95. }
  96. protected override Visual GetVisualChild(int index)
  97. {
  98. return base.GetVisualChild(index);
  99. }
  100. protected override int VisualChildrenCount => base.VisualChildrenCount;
  101. public event EventHandler<MouseEventObject> MouseLeftButtonDownHandler;
  102. public event EventHandler<MouseEventObject> MouseLeftButtonUpHandler;
  103. public event EventHandler<MouseEventObject> MouseMoveHandler;
  104. public event EventHandler<MouseEventObject> MouseRightButtonDownHandler;
  105. public event EventHandler<ScrollChangedEventArgs> ScrollChangedHandler;
  106. public event EventHandler DrawChanged;
  107. public event EventHandler DocumentModifiedChanged;
  108. private ToolType currentModel = ToolType.Viewer;
  109. DefaultSettingParam defaultSettingParam = new DefaultSettingParam();
  110. DefaultDrawParam defaultDrawParam = new DefaultDrawParam();
  111. MeasureSetting measureSetting = new MeasureSetting();
  112. Point Point = new Point();
  113. Point CachePoint = new Point();
  114. private bool isMultiSelected;
  115. private bool isDocumentModified = false;
  116. public bool CanAddTextEdit = true;
  117. protected bool isContinueCreateTextEdit = false;
  118. public bool GetIsMultiSelected()
  119. {
  120. return isMultiSelected;
  121. }
  122. /// <summary>
  123. /// Set whether continuous text editing is required
  124. /// </summary>
  125. /// <param name="isContinueCreateTextEdit"></param>
  126. public void SetContinueCreateTextEdit(bool isContinueCreateTextEdit)
  127. {
  128. this.isContinueCreateTextEdit = isContinueCreateTextEdit;
  129. CanAddTextEdit = true;
  130. }
  131. /// <summary>
  132. /// Does it support multiple selection
  133. /// </summary>
  134. /// <param name="isMulti">true Can MultiSelected</param>
  135. public void SetIsMultiSelected(bool isMulti)
  136. {
  137. isMultiSelected = isMulti;
  138. }
  139. public DefaultSettingParam GetDefaultSettingParam()
  140. {
  141. return defaultSettingParam;
  142. }
  143. public DefaultDrawParam GetDefaultDrawParam()
  144. {
  145. return defaultDrawParam;
  146. }
  147. /// <summary>
  148. /// Set default painting parameters
  149. /// </summary>
  150. /// <param name="defaultDrawParam"></param>
  151. public void SetDefaultDrawParam(DefaultDrawParam defaultDrawParam = null)
  152. {
  153. if (defaultDrawParam == null)
  154. {
  155. this.defaultDrawParam = new DefaultDrawParam();
  156. }
  157. else
  158. {
  159. this.defaultDrawParam = defaultDrawParam;
  160. }
  161. }
  162. public MeasureSetting GetMeasureSetting()
  163. {
  164. return measureSetting;
  165. }
  166. public bool IsSelectRectMousePoint()
  167. {
  168. if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
  169. {
  170. return true;
  171. }
  172. return false;
  173. }
  174. private void LinkAnnotAction(BaseAnnot annot)
  175. {
  176. AnnotData data = annot.GetAnnotData();
  177. CPDFLinkAnnotation linkAnnot = data.Annot as CPDFLinkAnnotation;
  178. CPDFAction action = linkAnnot.GetLinkAction();
  179. if (action != null)
  180. {
  181. ActionProcess(action);
  182. }
  183. else
  184. {
  185. CPDFDestination dest = linkAnnot.GetDestination(PDFViewer.GetDocument());
  186. if (dest != null)
  187. {
  188. CPDFGoToAction gotoAction = new CPDFGoToAction();
  189. gotoAction.SetDestination(PDFViewer.GetDocument(), dest);
  190. ActionProcess(gotoAction);
  191. }
  192. }
  193. }
  194. public void ActionProcess(CPDFAction action)
  195. {
  196. if (action == null)
  197. {
  198. return;
  199. }
  200. switch (action.ActionType)
  201. {
  202. case C_ACTION_TYPE.ACTION_TYPE_NAMED:
  203. {
  204. CPDFNamedAction namedAction = action as CPDFNamedAction;
  205. string namedStr = namedAction.GetName();
  206. switch (namedStr)
  207. {
  208. case "FirstPage":
  209. {
  210. PDFViewer?.GoToPage(0, new Point(0, 0));
  211. break;
  212. }
  213. case "LastPage":
  214. {
  215. PDFViewer?.GoToPage(PDFViewer.GetDocument().PageCount - 1, new Point(0, 0));
  216. break;
  217. }
  218. case "NextPage":
  219. if (PDFViewer != null)
  220. {
  221. int nextIndex = PDFViewer.CurrentRenderFrame.PageIndex + 1;
  222. if (nextIndex < PDFViewer.GetDocument().PageCount)
  223. {
  224. PDFViewer.GoToPage(nextIndex, new Point(0, 0));
  225. }
  226. }
  227. break;
  228. case "PrevPage":
  229. if (PDFViewer != null)
  230. {
  231. int prevIndex = PDFViewer.CurrentRenderFrame.PageIndex - 1;
  232. if (prevIndex >= 0)
  233. {
  234. PDFViewer.GoToPage(prevIndex, new Point(0, 0));
  235. }
  236. }
  237. break;
  238. default:
  239. break;
  240. }
  241. break;
  242. }
  243. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  244. if (PDFViewer != null)
  245. {
  246. CPDFGoToAction gotoAction = action as CPDFGoToAction;
  247. CPDFDestination dest = gotoAction.GetDestination(PDFViewer.GetDocument());
  248. if (dest != null)
  249. {
  250. Size pageSize = DataConversionForWPF.CSizeConversionForSize(PDFViewer.GetDocument().GetPageSize(dest.PageIndex));
  251. PDFViewer.GoToPage(dest.PageIndex, new Point(dest.Position_X, pageSize.Height - dest.Position_Y));
  252. }
  253. }
  254. break;
  255. case C_ACTION_TYPE.ACTION_TYPE_GOTOR:
  256. if (PDFViewer != null)
  257. {
  258. CPDFGoToRAction gotorAction = action as CPDFGoToRAction;
  259. CPDFDestination dest = gotorAction.GetDestination(PDFViewer.GetDocument());
  260. if (dest != null)
  261. {
  262. Size pageSize = DataConversionForWPF.CSizeConversionForSize(PDFViewer.GetDocument().GetPageSize(dest.PageIndex));
  263. PDFViewer.GoToPage(dest.PageIndex, new Point(dest.Position_X, pageSize.Height - dest.Position_Y));
  264. }
  265. }
  266. break;
  267. case C_ACTION_TYPE.ACTION_TYPE_URI:
  268. {
  269. CPDFUriAction uriAction = action as CPDFUriAction;
  270. string uri = uriAction.GetUri();
  271. try
  272. {
  273. if (!string.IsNullOrEmpty(uri))
  274. {
  275. Process.Start(uri);
  276. }
  277. }
  278. catch (Exception ex)
  279. {
  280. }
  281. }
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
  288. {
  289. if (isContinueCreateTextEdit)
  290. {
  291. if (lastSelectedRect != null)
  292. {
  293. CanAddTextEdit = false;
  294. }
  295. else
  296. {
  297. CanAddTextEdit = true;
  298. }
  299. }
  300. if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null)
  301. {
  302. return;
  303. }
  304. if (!HitTestBorder())
  305. {
  306. RemovePopTextUI();
  307. }
  308. Focus();
  309. Mouse.Capture(this, CaptureMode.SubTree);
  310. MouseEventObject mouseEventObject = new MouseEventObject
  311. {
  312. mouseButtonEventArgs = e,
  313. hitTestType = MouseHitTestType.Unknown,
  314. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  315. IsCreate = false
  316. };
  317. if (isDrawSelectRect)
  318. {
  319. if (e.ClickCount == 2)
  320. {
  321. // Refresh the currently selected annotation object to ensure it is the latest
  322. AnnotHitTest();
  323. if (cacheHitTestAnnot is FreeTextAnnot)
  324. {
  325. BuildPopTextUI(cacheHitTestAnnot);
  326. isDrawSelectRect = false;
  327. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  328. mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
  329. MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
  330. return;
  331. }
  332. if (cacheHitTestAnnot is StickyNoteAnnot)
  333. {
  334. (cacheHitTestAnnot as StickyNoteAnnot).PopStickyNote();
  335. }
  336. }
  337. // Click inside the selected rectangle area
  338. if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
  339. {
  340. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  341. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  342. mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
  343. MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
  344. return;
  345. }
  346. else
  347. {
  348. CleanSelectedRect();
  349. }
  350. }
  351. if (IsDrawEditAnnot)
  352. {
  353. // Click inside the selected rectangle area
  354. if (DrawEditAnnotDownEvent() && cacheHitTestAnnot != null)
  355. {
  356. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  357. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  358. mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
  359. MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
  360. return;
  361. }
  362. }
  363. Point = e.GetPosition(this);
  364. // Annotation selection effect
  365. if ((currentModel == ToolType.Pan
  366. || currentModel == ToolType.CreateAnnot)
  367. && AnnotHitTest()
  368. && IsCanSave()
  369. && !PDFViewer.GetIsShowStampMouse()
  370. )
  371. {
  372. //if (!IsCacheRedaction)
  373. {
  374. if (cacheHitTestAnnot?.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK && currentModel != ToolType.CreateAnnot)
  375. {
  376. LinkAnnotAction(cacheHitTestAnnot);
  377. }
  378. else
  379. {
  380. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  381. {
  382. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  383. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  384. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  385. };
  386. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  387. {
  388. mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
  389. SetEditAnnotObject();
  390. }
  391. else
  392. {
  393. SelectedAnnot();
  394. CleanDrawSelectImage();
  395. }
  396. isDrawSelectRect = true;
  397. }
  398. }
  399. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  400. if (cacheHitTestAnnot != null)
  401. {
  402. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  403. }
  404. else
  405. {
  406. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  407. }
  408. }
  409. // Form selected effect
  410. else if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer) && AnnotWidgetHitTest())
  411. {
  412. mouseEventObject.hitTestType = MouseHitTestType.Widget;
  413. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  414. FormClickProcess();
  415. }
  416. else if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer))
  417. {
  418. if (AnnotHitTest() && cacheHitTestAnnot.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK)
  419. {
  420. LinkAnnotAction(cacheHitTestAnnot);
  421. }
  422. if (DrawDownSelectImage(true))
  423. {
  424. mouseEventObject.hitTestType = MouseHitTestType.ImageSelect;
  425. }
  426. else
  427. {
  428. ReDrawSelectImage();
  429. }
  430. }
  431. // Form creation mode
  432. else if (currentModel == ToolType.WidgetEdit)
  433. {
  434. if (AnnotWidgetHitTest())
  435. {
  436. cacheHitTestAnnot = PDFViewer?.AnnotHitTest() as BaseWidget;
  437. SelectedAnnot();
  438. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  439. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  440. }
  441. }
  442. // Content editing mode
  443. else if (currentModel == ToolType.ContentEdit)
  444. {
  445. OpenSelectedMulti(isMultiSelected);
  446. if (!PDFViewer.GetIsShowStampMouse())
  447. {
  448. DrawTextEditDownEvent(true);
  449. }
  450. if (lastSelectedRect != null)
  451. {
  452. //Multi selection processing optimization, other click effects
  453. DrawEndFrameSelect();
  454. if (!Keyboard.IsKeyDown(multiKey) || !isMultiSelected)
  455. {
  456. CleanSelectedMultiRect();
  457. OpenSelectedMulti(false);
  458. if (PDFViewer.CurrentRenderFrame != null)
  459. {
  460. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  461. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  462. {
  463. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  464. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  465. {
  466. DrawSelectedEditAreaForIndex();
  467. }
  468. }
  469. }
  470. ReDrawSelectedMultiRect();
  471. }
  472. if (lastSelectedRect == null)
  473. {
  474. return;
  475. }
  476. SelectedMultiRect(lastSelectedRect.GetRect(), lastSelectedRect.GetMaxRect(), SelectedType.PDFEdit);
  477. HideDrawSelectedMultiRect();
  478. lastSelectedRect.DataChanged -= SelectedRect_DataChanged;
  479. lastSelectedRect.DataChanged += SelectedRect_DataChanged;
  480. }
  481. else
  482. {
  483. if (Keyboard.IsKeyDown(multiKey) && isMultiSelected)
  484. {
  485. DelMultiSelectRect();
  486. }
  487. if (HitTestMultiSelectedRect())
  488. {
  489. mouseEventObject.hitTestType = MouseHitTestType.MultiTextEdit;
  490. }
  491. else
  492. {
  493. //Clear the currently selected object
  494. startSelectedRect = null;
  495. startSelectedIndex = -1;
  496. startSelectedPageIndex = -1;
  497. startSelectedEditAreaObject = null;
  498. CleanSelectedMultiRect();
  499. OpenSelectedMulti(false);
  500. if (PDFViewer.CurrentRenderFrame != null)
  501. {
  502. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  503. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  504. {
  505. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  506. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  507. {
  508. DrawSelectedEditAreaForIndex();
  509. }
  510. }
  511. }
  512. ReDrawSelectedMultiRect();
  513. }
  514. }
  515. }
  516. else if (currentModel == ToolType.SelectedPage)
  517. {
  518. if (HitTestPageSelectedRect())
  519. {
  520. }
  521. else
  522. {
  523. CleanPageSelectedRect();
  524. CreatePageSelectdRect();
  525. }
  526. mouseEventObject.hitTestType = MouseHitTestType.SelectedPageRect;
  527. }
  528. MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
  529. }
  530. protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
  531. {
  532. if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null)
  533. {
  534. return;
  535. }
  536. MouseEventObject mouseEventObject = new MouseEventObject
  537. {
  538. mouseButtonEventArgs = e,
  539. hitTestType = MouseHitTestType.Unknown,
  540. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  541. IsCreate = false
  542. };
  543. if (isDrawSelectRect || IsDrawEditAnnot)
  544. {
  545. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  546. if (cacheHitTestAnnot != null)
  547. {
  548. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  549. }
  550. else
  551. {
  552. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  553. }
  554. MouseLeftButtonUpHandler?.Invoke(this, mouseEventObject);
  555. ReleaseMouseCapture();
  556. return;
  557. }
  558. MouseLeftButtonUpHandler?.Invoke(this, mouseEventObject);
  559. ReleaseMouseCapture();
  560. }
  561. protected override void OnMouseMove(MouseEventArgs e)
  562. {
  563. if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null)
  564. {
  565. return;
  566. }
  567. Cursor oldCursor = this.Cursor;
  568. Cursor newCursor = this.Cursor;
  569. MouseEventObject mouseEventObject = new MouseEventObject
  570. {
  571. mouseButtonEventArgs = e,
  572. hitTestType = MouseHitTestType.Unknown,
  573. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  574. IsCreate = false
  575. };
  576. if (Mouse.LeftButton != MouseButtonState.Pressed)
  577. {
  578. List<ToolType> allowModeList = new List<ToolType>()
  579. {
  580. ToolType.Pan,
  581. ToolType.Viewer
  582. };
  583. if (allowModeList.Contains(currentModel))
  584. {
  585. newCursor = Cursors.Arrow;
  586. if (caheMoveAnnot is BaseWidget)
  587. {
  588. BaseWidget widget = (BaseWidget)caheMoveAnnot;
  589. if (widget.GetFormType() == PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON && PDFViewer != null)
  590. {
  591. newCursor = Cursors.Hand;
  592. }
  593. }
  594. if (caheMoveAnnot is LinkAnnot)
  595. {
  596. newCursor = Cursors.Hand;
  597. }
  598. }
  599. }
  600. if (!isDrawSelectRect && !IsDrawEditAnnot)
  601. {
  602. if (AnnotMoveHitTest())
  603. {
  604. if (isCacheRedaction)
  605. {
  606. (caheMoveAnnot as RedactionAnnot).SetIsMouseHover(true);
  607. (caheMoveAnnot as RedactionAnnot).Draw();
  608. }
  609. mouseEventObject.annotType = caheMoveAnnot.GetAnnotData().AnnotType;
  610. mouseEventObject.IsMersured = caheMoveAnnot.GetAnnotData().Annot.IsMeasured();
  611. }
  612. else
  613. {
  614. if (isCacheRedaction)
  615. {
  616. isCacheRedaction = false;
  617. (caheMoveAnnot as RedactionAnnot).SetIsMouseHover(false);
  618. (caheMoveAnnot as RedactionAnnot).Draw();
  619. }
  620. caheMoveAnnot = null;
  621. if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer))
  622. {
  623. DrawMoveSelectImage();
  624. }
  625. }
  626. }
  627. else
  628. {
  629. if (AnnotMoveHitTest())
  630. {
  631. if (DrawSelectRectDownEvent() == false && Mouse.LeftButton != MouseButtonState.Pressed)
  632. {
  633. mouseEventObject.annotType = caheMoveAnnot.GetAnnotData().AnnotType;
  634. }
  635. if (isCacheRedaction)
  636. {
  637. (caheMoveAnnot as RedactionAnnot)?.SetIsMouseHover(true);
  638. (caheMoveAnnot as RedactionAnnot)?.Draw();
  639. }
  640. }
  641. else
  642. {
  643. if (isCacheRedaction)
  644. {
  645. (caheMoveAnnot as RedactionAnnot)?.SetIsMouseHover(false);
  646. (caheMoveAnnot as RedactionAnnot)?.Draw();
  647. }
  648. }
  649. if (mouseEventObject.annotType == C_ANNOTATION_TYPE.C_ANNOTATION_NONE)
  650. {
  651. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  652. if (cacheHitTestAnnot != null)
  653. {
  654. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  655. mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
  656. }
  657. else
  658. {
  659. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  660. }
  661. }
  662. }
  663. MouseMoveHandler?.Invoke(this, mouseEventObject);
  664. PDFViewer.SetCustomMousePoint(Mouse.GetPosition(this).Y, Mouse.GetPosition(this).X);
  665. if (oldCursor != newCursor)
  666. {
  667. this.Cursor = newCursor;
  668. }
  669. }
  670. protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
  671. {
  672. MouseEventObject mouseEventObject = new MouseEventObject
  673. {
  674. mouseButtonEventArgs = e,
  675. hitTestType = MouseHitTestType.Unknown,
  676. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  677. IsCreate = false
  678. };
  679. if (currentModel == ToolType.Pan || currentModel == ToolType.Viewer)
  680. {
  681. if (GetMousePointToTextSelectInfo())
  682. {
  683. mouseEventObject.hitTestType = MouseHitTestType.Text;
  684. }
  685. }
  686. if (isDrawSelectRect)
  687. {
  688. // Click inside the selected rectangle area
  689. if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
  690. {
  691. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  692. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  693. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  694. return;
  695. }
  696. }
  697. if (IsDrawEditAnnot)
  698. {
  699. // Click inside the selected rectangle area
  700. if (DrawEditAnnotDownEvent() && cacheHitTestAnnot != null)
  701. {
  702. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  703. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  704. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  705. return;
  706. }
  707. }
  708. Point = e.GetPosition(this);
  709. // Annotation selection effect
  710. if ((currentModel == ToolType.Pan || currentModel == ToolType.CreateAnnot))
  711. {
  712. if (AnnotHitTest())
  713. {
  714. if (!isCacheRedaction)
  715. {
  716. if (cacheHitTestAnnot?.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK && currentModel != ToolType.CreateAnnot)
  717. {
  718. LinkAnnotAction(cacheHitTestAnnot);
  719. }
  720. else
  721. {
  722. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  723. {
  724. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  725. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  726. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  727. };
  728. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  729. {
  730. SetEditAnnotObject();
  731. DrawEditAnnotLayer();
  732. }
  733. else
  734. {
  735. SelectedAnnot();
  736. DrawSelectedLayer();
  737. }
  738. isDrawSelectRect = true;
  739. }
  740. }
  741. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  742. if (cacheHitTestAnnot != null)
  743. {
  744. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  745. }
  746. else
  747. {
  748. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  749. }
  750. }
  751. else
  752. {
  753. CleanSelectedRect();
  754. }
  755. }
  756. // Form selection effect
  757. if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer) && AnnotWidgetHitTest())
  758. {
  759. mouseEventObject.hitTestType = MouseHitTestType.Widget;
  760. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  761. FormClickProcess();
  762. }
  763. // Form creation mode
  764. if (currentModel == ToolType.WidgetEdit)
  765. {
  766. if (AnnotWidgetHitTest())
  767. {
  768. cacheHitTestAnnot = PDFViewer?.AnnotHitTest() as BaseWidget;
  769. SelectedAnnot();
  770. DrawSelectedLayer();
  771. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  772. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  773. }
  774. else
  775. {
  776. CleanSelectedRect();
  777. }
  778. }
  779. // Content editing mode
  780. if (currentModel == ToolType.ContentEdit)
  781. {
  782. if (e.ClickCount == 1)
  783. {
  784. DrawTextEditDownEvent(false);
  785. if (GetLastSelectedRect() != null)
  786. {
  787. EditAreaObject editAreaObject = GetEditAreaObjectForRect(lastSelectedRect);
  788. switch (editAreaObject.cPDFEditArea.Type)
  789. {
  790. case CPDFEditType.EditText:
  791. mouseEventObject.hitTestType = MouseHitTestType.TextEdit;
  792. break;
  793. case CPDFEditType.EditImage:
  794. mouseEventObject.hitTestType = MouseHitTestType.ImageEdit;
  795. break;
  796. case CPDFEditType.EditPath:
  797. mouseEventObject.hitTestType = MouseHitTestType.PathEdit;
  798. break;
  799. default:
  800. break;
  801. }
  802. }
  803. }
  804. }
  805. else
  806. {
  807. if ((currentModel == ToolType.Viewer || currentModel == ToolType.Pan) && mouseEventObject.hitTestType == MouseHitTestType.Unknown && DrawDownSelectImage(false))
  808. {
  809. mouseEventObject.hitTestType = MouseHitTestType.ImageSelect;
  810. }
  811. }
  812. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  813. }
  814. public bool GetIsCropMode()
  815. {
  816. if (lastSelectedRect != null)
  817. {
  818. return lastSelectedRect.GetCurrentDrawPointType() == DrawPointType.Crop;
  819. }
  820. return false;
  821. }
  822. public void SetCropMode(bool crop)
  823. {
  824. if (lastSelectedRect != null)
  825. {
  826. List<PointControlType> ignoreList = new List<PointControlType>();
  827. if (crop)
  828. {
  829. lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Crop);
  830. ignoreList.Add(PointControlType.Body);
  831. // Initialize ClipRect
  832. ClipThickness = new Thickness(0, 0, 0, 0);
  833. if (editArea.TryGetValue(lastSelectedRect, out EditAreaObject editAreaObject))
  834. {
  835. cropIndex = editAreaObject.EditAreaIndex;
  836. }
  837. lastSelectedRect.DataChanged -= LastSelectedRect_DataChanged;
  838. }
  839. else
  840. {
  841. lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Square);
  842. cropIndex = -1;
  843. lastSelectedRect.DataChanged += LastSelectedRect_DataChanged;
  844. }
  845. lastSelectedRect.SetIgnorePoints(ignoreList);
  846. lastSelectedRect.Draw();
  847. }
  848. }
  849. internal void SetToolType(ToolType model)
  850. {
  851. currentModel = model;
  852. CPDFViewer pdfViewer = GetCPDFViewer();
  853. if (pdfViewer != null)
  854. {
  855. if (currentModel == ToolType.WidgetEdit)
  856. {
  857. pdfViewer.IsHideFormShow = true;
  858. }
  859. else
  860. {
  861. pdfViewer.IsHideFormShow = false;
  862. }
  863. }
  864. }
  865. public ToolType GetToolType()
  866. {
  867. return currentModel;
  868. }
  869. public void SavePoint()
  870. {
  871. CachePoint = Point;
  872. }
  873. public void CleanPoint()
  874. {
  875. CachePoint = new Point();
  876. }
  877. private void CPDFViewerTool_Loaded(object sender, RoutedEventArgs e)
  878. {
  879. PDFViewer.DrawChanged += PDFViewer_DrawChanged;
  880. PDFViewer.UndoManager.HistoryChanged += UndoManager_HistoryChanged;
  881. PDFViewer.MouseEnter += PDFViewer_MouseEnter;
  882. PDFViewer.MouseLeave += PDFViewer_MouseLeave;
  883. }
  884. private void PDFViewer_MouseLeave(object sender, MouseEventArgs e)
  885. {
  886. PDFViewer.IsVisibilityMouse(false);
  887. }
  888. private void PDFViewer_MouseEnter(object sender, MouseEventArgs e)
  889. {
  890. PDFViewer.IsVisibilityMouse(true);
  891. }
  892. private void UndoManager_HistoryChanged(object sender, KeyValuePair<UndoAction, IHistory> data)
  893. {
  894. IsDocumentModified = true;
  895. }
  896. private void CPDFViewerTool_Unloaded(object sender, RoutedEventArgs e)
  897. {
  898. PDFViewer.DrawChanged -= PDFViewer_DrawChanged;
  899. }
  900. private void PDFViewer_DrawChanged(object sender, EventArgs e)
  901. {
  902. SizeChangeds();
  903. DrawChanged?.Invoke(this, e);
  904. }
  905. public void SizeChangeds()
  906. {
  907. if (IsLoaded)
  908. {
  909. if (cacheHitTestAnnot != null)
  910. {
  911. BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
  912. bool Update = (baseLayer1 as AnnotLayer).GetUpdate(ref cacheHitTestAnnot);
  913. if (Update)
  914. {
  915. if (IsDrawEditAnnot)
  916. {
  917. SetEditAnnotObject();
  918. DrawEditAnnotLayer();
  919. }
  920. if (isDrawSelectRect)
  921. {
  922. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  923. {
  924. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  925. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  926. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  927. };
  928. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  929. {
  930. SetEditAnnotObject();
  931. DrawEditAnnotLayer();
  932. }
  933. else
  934. {
  935. SelectedAnnot();
  936. }
  937. DrawSelectedLayer();
  938. }
  939. }
  940. else
  941. {
  942. SelectedAnnot(null);
  943. }
  944. }
  945. else if (selectedPageIndex != -1 && selectedAnnotIndex != -1)
  946. {
  947. BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
  948. cacheHitTestAnnot = (baseLayer1 as AnnotLayer).GetSelectedAnnot(ref selectedPageIndex, ref selectedAnnotIndex);
  949. if (cacheHitTestAnnot != null)
  950. {
  951. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  952. {
  953. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  954. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  955. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  956. };
  957. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  958. {
  959. SetEditAnnotObject();
  960. DrawEditAnnotLayer();
  961. }
  962. else
  963. {
  964. SelectedAnnot();
  965. }
  966. DrawSelectedLayer();
  967. isDrawSelectRect = true;
  968. }
  969. }
  970. if (PDFViewer.CurrentRenderFrame != null)
  971. {
  972. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  973. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  974. {
  975. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  976. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  977. {
  978. DrawSelectedEditAreaForIndex();
  979. }
  980. }
  981. }
  982. ReDrawSelectedMultiRect();
  983. ReDrawWidget();
  984. ReDrawSelectText();
  985. ReDrawSelectImage();
  986. UpdateFormHitPop();
  987. UpdateTextPop();
  988. }
  989. }
  990. private void ScrollViewer_MouseUp(object sender, MouseButtonEventArgs e)
  991. {
  992. }
  993. private void ScrollViewer_MouseDown(object sender, MouseButtonEventArgs e)
  994. {
  995. }
  996. private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
  997. {
  998. ScrollChangedHandler?.Invoke(this, e);
  999. }
  1000. }
  1001. }