CPDFViewerTool.xaml.cs 40 KB

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