CPDFViewerTool.xaml.cs 41 KB

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