CPDFViewerTool.xaml.cs 40 KB

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