CPDFViewerTool.xaml.cs 38 KB

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