CPDFViewerTool.xaml.cs 35 KB

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