CPDFViewerTool.xaml.cs 38 KB

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