CPDFViewerTool.xaml.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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 DrawChanged;
  105. public event EventHandler DocumentModifiedChanged;
  106. private ToolType currentModel = ToolType.Viewer;
  107. DefaultSettingParam defaultSettingParam = new DefaultSettingParam();
  108. DefaultDrawParam defaultDrawParam = new DefaultDrawParam();
  109. MeasureSetting measureSetting = new MeasureSetting();
  110. Point Point = new Point();
  111. Point CachePoint = new Point();
  112. private bool isMultiSelected;
  113. private bool isDocumentModified = false;
  114. public bool CanAddTextEdit = true;
  115. protected bool isContinueCreateTextEdit = false;
  116. public bool GetIsMultiSelected()
  117. {
  118. return isMultiSelected;
  119. }
  120. /// <summary>
  121. /// Set whether continuous text editing is required
  122. /// </summary>
  123. /// <param name="isContinueCreateTextEdit"></param>
  124. public void SetContinueCreateTextEdit(bool isContinueCreateTextEdit)
  125. {
  126. this.isContinueCreateTextEdit = isContinueCreateTextEdit;
  127. CanAddTextEdit = true;
  128. }
  129. /// <summary>
  130. /// Does it support multiple selection
  131. /// </summary>
  132. /// <param name="isMulti">true Can MultiSelected</param>
  133. public void SetIsMultiSelected(bool isMulti)
  134. {
  135. isMultiSelected = isMulti;
  136. }
  137. public DefaultSettingParam GetDefaultSettingParam()
  138. {
  139. return defaultSettingParam;
  140. }
  141. public DefaultDrawParam GetDefaultDrawParam()
  142. {
  143. return defaultDrawParam;
  144. }
  145. /// <summary>
  146. /// Set default painting parameters
  147. /// </summary>
  148. /// <param name="defaultDrawParam"></param>
  149. public void SetDefaultDrawParam(DefaultDrawParam defaultDrawParam = null)
  150. {
  151. if (defaultDrawParam == null)
  152. {
  153. this.defaultDrawParam = new DefaultDrawParam();
  154. }
  155. else
  156. {
  157. this.defaultDrawParam = defaultDrawParam;
  158. }
  159. }
  160. public MeasureSetting GetMeasureSetting()
  161. {
  162. return measureSetting;
  163. }
  164. public bool IsSelectRectMousePoint()
  165. {
  166. if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
  167. {
  168. return true;
  169. }
  170. return false;
  171. }
  172. private void LinkAnnotAction(BaseAnnot annot)
  173. {
  174. AnnotData data = annot.GetAnnotData();
  175. CPDFLinkAnnotation linkAnnot = data.Annot as CPDFLinkAnnotation;
  176. CPDFAction action = linkAnnot.GetLinkAction();
  177. if (action != null)
  178. {
  179. ActionProcess(action);
  180. }
  181. else
  182. {
  183. CPDFDestination dest = linkAnnot.GetDestination(PDFViewer.GetDocument());
  184. if (dest != null)
  185. {
  186. CPDFGoToAction gotoAction = new CPDFGoToAction();
  187. gotoAction.SetDestination(PDFViewer.GetDocument(), dest);
  188. ActionProcess(gotoAction);
  189. }
  190. }
  191. }
  192. public void ActionProcess(CPDFAction action)
  193. {
  194. if (action == null)
  195. {
  196. return;
  197. }
  198. switch (action.ActionType)
  199. {
  200. case C_ACTION_TYPE.ACTION_TYPE_NAMED:
  201. {
  202. CPDFNamedAction namedAction = action as CPDFNamedAction;
  203. string namedStr = namedAction.GetName();
  204. switch (namedStr)
  205. {
  206. case "FirstPage":
  207. {
  208. PDFViewer?.GoToPage(0, new Point(0, 0));
  209. break;
  210. }
  211. case "LastPage":
  212. {
  213. PDFViewer?.GoToPage(PDFViewer.GetDocument().PageCount - 1, new Point(0, 0));
  214. break;
  215. }
  216. case "NextPage":
  217. if (PDFViewer != null)
  218. {
  219. int nextIndex = PDFViewer.CurrentRenderFrame.PageIndex + 1;
  220. if (nextIndex < PDFViewer.GetDocument().PageCount)
  221. {
  222. PDFViewer.GoToPage(nextIndex, new Point(0, 0));
  223. }
  224. }
  225. break;
  226. case "PrevPage":
  227. if (PDFViewer != null)
  228. {
  229. int prevIndex = PDFViewer.CurrentRenderFrame.PageIndex - 1;
  230. if (prevIndex >= 0)
  231. {
  232. PDFViewer.GoToPage(prevIndex, new Point(0, 0));
  233. }
  234. }
  235. break;
  236. default:
  237. break;
  238. }
  239. break;
  240. }
  241. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  242. if (PDFViewer != null)
  243. {
  244. CPDFGoToAction gotoAction = action as CPDFGoToAction;
  245. CPDFDestination dest = gotoAction.GetDestination(PDFViewer.GetDocument());
  246. if (dest != null)
  247. {
  248. Size pageSize = DataConversionForWPF.CSizeConversionForSize(PDFViewer.GetDocument().GetPageSize(dest.PageIndex));
  249. PDFViewer.GoToPage(dest.PageIndex, new Point(dest.Position_X, pageSize.Height - dest.Position_Y));
  250. }
  251. }
  252. break;
  253. case C_ACTION_TYPE.ACTION_TYPE_GOTOR:
  254. if (PDFViewer != null)
  255. {
  256. CPDFGoToRAction gotorAction = action as CPDFGoToRAction;
  257. CPDFDestination dest = gotorAction.GetDestination(PDFViewer.GetDocument());
  258. if (dest != null)
  259. {
  260. Size pageSize = DataConversionForWPF.CSizeConversionForSize(PDFViewer.GetDocument().GetPageSize(dest.PageIndex));
  261. PDFViewer.GoToPage(dest.PageIndex, new Point(dest.Position_X, pageSize.Height - dest.Position_Y));
  262. }
  263. }
  264. break;
  265. case C_ACTION_TYPE.ACTION_TYPE_URI:
  266. {
  267. CPDFUriAction uriAction = action as CPDFUriAction;
  268. string uri = uriAction.GetUri();
  269. try
  270. {
  271. if (!string.IsNullOrEmpty(uri))
  272. {
  273. Process.Start(uri);
  274. }
  275. }
  276. catch (Exception ex)
  277. {
  278. }
  279. }
  280. break;
  281. default:
  282. break;
  283. }
  284. }
  285. protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
  286. {
  287. if (isContinueCreateTextEdit)
  288. {
  289. if (lastSelectedRect != null)
  290. {
  291. CanAddTextEdit = false;
  292. }
  293. else
  294. {
  295. CanAddTextEdit = true;
  296. }
  297. }
  298. if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null)
  299. {
  300. return;
  301. }
  302. if (PDFViewer.IsRendering)
  303. return;
  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 (DrawDownSelectImage(true))
  419. {
  420. mouseEventObject.hitTestType = MouseHitTestType.ImageSelect;
  421. }
  422. else
  423. {
  424. ReDrawSelectImage();
  425. }
  426. }
  427. // Form creation mode
  428. else if (currentModel == ToolType.WidgetEdit)
  429. {
  430. if (AnnotWidgetHitTest())
  431. {
  432. cacheHitTestAnnot = PDFViewer?.AnnotHitTest() as BaseWidget;
  433. SelectedAnnot();
  434. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  435. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  436. }
  437. }
  438. // Content editing mode
  439. else if (currentModel == ToolType.ContentEdit)
  440. {
  441. OpenSelectedMulti(isMultiSelected);
  442. if (!PDFViewer.GetIsShowStampMouse())
  443. {
  444. DrawTextEditDownEvent(true);
  445. }
  446. if (lastSelectedRect != null)
  447. {
  448. //Multi selection processing optimization, other click effects
  449. DrawEndFrameSelect();
  450. if (!Keyboard.IsKeyDown(multiKey) || !isMultiSelected)
  451. {
  452. CleanSelectedMultiRect();
  453. OpenSelectedMulti(false);
  454. if (PDFViewer.CurrentRenderFrame != null)
  455. {
  456. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  457. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  458. {
  459. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  460. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  461. {
  462. DrawSelectedEditAreaForIndex();
  463. }
  464. }
  465. }
  466. ReDrawSelectedMultiRect();
  467. }
  468. if (lastSelectedRect == null)
  469. {
  470. return;
  471. }
  472. SelectedMultiRect(lastSelectedRect.GetRect(), lastSelectedRect.GetMaxRect(), SelectedType.PDFEdit);
  473. HideDrawSelectedMultiRect();
  474. lastSelectedRect.DataChanged -= SelectedRect_DataChanged;
  475. lastSelectedRect.DataChanged += SelectedRect_DataChanged;
  476. }
  477. else
  478. {
  479. if (Keyboard.IsKeyDown(multiKey) && isMultiSelected)
  480. {
  481. DelMultiSelectRect();
  482. }
  483. if (HitTestMultiSelectedRect())
  484. {
  485. mouseEventObject.hitTestType = MouseHitTestType.MultiTextEdit;
  486. }
  487. else
  488. {
  489. //Clear the currently selected object
  490. startSelectedRect = null;
  491. startSelectedIndex = -1;
  492. startSelectedPageIndex = -1;
  493. startSelectedEditAreaObject = null;
  494. CleanSelectedMultiRect();
  495. OpenSelectedMulti(false);
  496. if (PDFViewer.CurrentRenderFrame != null)
  497. {
  498. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  499. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  500. {
  501. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  502. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  503. {
  504. DrawSelectedEditAreaForIndex();
  505. }
  506. }
  507. }
  508. ReDrawSelectedMultiRect();
  509. }
  510. }
  511. }
  512. else if (currentModel == ToolType.SelectedPage)
  513. {
  514. if (HitTestPageSelectedRect())
  515. {
  516. }
  517. else
  518. {
  519. CleanPageSelectedRect();
  520. CreatePageSelectdRect();
  521. }
  522. mouseEventObject.hitTestType = MouseHitTestType.SelectedPageRect;
  523. }
  524. MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
  525. }
  526. protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
  527. {
  528. if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null)
  529. {
  530. return;
  531. }
  532. if (PDFViewer.IsRendering)
  533. return;
  534. MouseEventObject mouseEventObject = new MouseEventObject
  535. {
  536. mouseButtonEventArgs = e,
  537. hitTestType = MouseHitTestType.Unknown,
  538. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  539. IsCreate = false
  540. };
  541. if (isDrawSelectRect || IsDrawEditAnnot)
  542. {
  543. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  544. if (cacheHitTestAnnot != null)
  545. {
  546. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  547. }
  548. else
  549. {
  550. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  551. }
  552. MouseLeftButtonUpHandler?.Invoke(this, mouseEventObject);
  553. ReleaseMouseCapture();
  554. return;
  555. }
  556. MouseLeftButtonUpHandler?.Invoke(this, mouseEventObject);
  557. ReleaseMouseCapture();
  558. }
  559. protected override void OnMouseMove(MouseEventArgs e)
  560. {
  561. if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null)
  562. {
  563. return;
  564. }
  565. if (PDFViewer.IsRendering)
  566. return;
  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. if (PDFViewer.IsRendering)
  673. return;
  674. MouseEventObject mouseEventObject = new MouseEventObject
  675. {
  676. mouseButtonEventArgs = e,
  677. hitTestType = MouseHitTestType.Unknown,
  678. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  679. IsCreate = false
  680. };
  681. if (currentModel == ToolType.Pan || currentModel == ToolType.Viewer)
  682. {
  683. if (GetMousePointToTextSelectInfo())
  684. {
  685. mouseEventObject.hitTestType = MouseHitTestType.Text;
  686. }
  687. }
  688. if (isDrawSelectRect)
  689. {
  690. // Click inside the selected rectangle area
  691. if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
  692. {
  693. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  694. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  695. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  696. return;
  697. }
  698. }
  699. if (IsDrawEditAnnot)
  700. {
  701. // Click inside the selected rectangle area
  702. if (DrawEditAnnotDownEvent() && cacheHitTestAnnot != null)
  703. {
  704. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  705. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  706. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  707. return;
  708. }
  709. }
  710. Point = e.GetPosition(this);
  711. // Annotation selection effect
  712. if ((currentModel == ToolType.Pan || currentModel == ToolType.CreateAnnot))
  713. {
  714. if (AnnotHitTest())
  715. {
  716. if (!isCacheRedaction)
  717. {
  718. if (cacheHitTestAnnot?.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK && currentModel != ToolType.CreateAnnot)
  719. {
  720. LinkAnnotAction(cacheHitTestAnnot);
  721. }
  722. else
  723. {
  724. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  725. {
  726. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  727. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  728. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  729. };
  730. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  731. {
  732. SetEditAnnotObject();
  733. DrawEditAnnotLayer();
  734. }
  735. else
  736. {
  737. SelectedAnnot();
  738. DrawSelectedLayer();
  739. }
  740. isDrawSelectRect = true;
  741. }
  742. }
  743. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  744. if (cacheHitTestAnnot != null)
  745. {
  746. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  747. }
  748. else
  749. {
  750. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  751. }
  752. }
  753. else
  754. {
  755. CleanSelectedRect();
  756. }
  757. }
  758. // Form selection effect
  759. if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer) && AnnotWidgetHitTest())
  760. {
  761. mouseEventObject.hitTestType = MouseHitTestType.Widget;
  762. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  763. FormClickProcess();
  764. }
  765. // Form creation mode
  766. if (currentModel == ToolType.WidgetEdit)
  767. {
  768. if (AnnotWidgetHitTest())
  769. {
  770. cacheHitTestAnnot = PDFViewer?.AnnotHitTest() as BaseWidget;
  771. SelectedAnnot();
  772. DrawSelectedLayer();
  773. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  774. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  775. }
  776. else
  777. {
  778. CleanSelectedRect();
  779. }
  780. }
  781. // Content editing mode
  782. if (currentModel == ToolType.ContentEdit)
  783. {
  784. if (e.ClickCount == 1)
  785. {
  786. DrawTextEditDownEvent(false);
  787. if (GetLastSelectedRect() != null)
  788. {
  789. EditAreaObject editAreaObject = GetEditAreaObjectForRect(lastSelectedRect);
  790. switch (editAreaObject.cPDFEditArea.Type)
  791. {
  792. case CPDFEditType.EditText:
  793. mouseEventObject.hitTestType = MouseHitTestType.TextEdit;
  794. break;
  795. case CPDFEditType.EditImage:
  796. mouseEventObject.hitTestType = MouseHitTestType.ImageEdit;
  797. break;
  798. default:
  799. break;
  800. }
  801. }
  802. }
  803. }
  804. else
  805. {
  806. if ((currentModel == ToolType.Viewer || currentModel == ToolType.Pan) && mouseEventObject.hitTestType == MouseHitTestType.Unknown && DrawDownSelectImage(false))
  807. {
  808. mouseEventObject.hitTestType = MouseHitTestType.ImageSelect;
  809. }
  810. }
  811. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  812. }
  813. public bool GetIsCropMode()
  814. {
  815. if (lastSelectedRect != null)
  816. {
  817. return lastSelectedRect.GetCurrentDrawPointType() == DrawPointType.Crop;
  818. }
  819. return false;
  820. }
  821. public void SetCropMode(bool crop)
  822. {
  823. if (lastSelectedRect != null)
  824. {
  825. List<PointControlType> ignoreList = new List<PointControlType>();
  826. if (crop)
  827. {
  828. lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Crop);
  829. ignoreList.Add(PointControlType.Body);
  830. // Initialize ClipRect
  831. ClipThickness = new Thickness(0, 0, 0, 0);
  832. if (editArea.TryGetValue(lastSelectedRect, out EditAreaObject editAreaObject))
  833. {
  834. cropIndex = editAreaObject.EditAreaIndex;
  835. }
  836. lastSelectedRect.DataChanged -= LastSelectedRect_DataChanged;
  837. }
  838. else
  839. {
  840. lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Square);
  841. cropIndex = -1;
  842. lastSelectedRect.DataChanged += LastSelectedRect_DataChanged;
  843. }
  844. lastSelectedRect.SetIgnorePoints(ignoreList);
  845. lastSelectedRect.Draw();
  846. }
  847. }
  848. internal void SetToolType(ToolType model)
  849. {
  850. currentModel = model;
  851. CPDFViewer pdfViewer = GetCPDFViewer();
  852. if (pdfViewer != null)
  853. {
  854. if (currentModel == ToolType.WidgetEdit)
  855. {
  856. pdfViewer.IsHideFormShow = true;
  857. }
  858. else
  859. {
  860. pdfViewer.IsHideFormShow = false;
  861. }
  862. }
  863. }
  864. public ToolType GetToolType()
  865. {
  866. return currentModel;
  867. }
  868. public void SavePoint()
  869. {
  870. CachePoint = Point;
  871. }
  872. public void CleanPoint()
  873. {
  874. CachePoint = new Point();
  875. }
  876. private void CPDFViewerTool_Loaded(object sender, RoutedEventArgs e)
  877. {
  878. PDFViewer.DrawChanged += PDFViewer_DrawChanged;
  879. PDFViewer.UndoManager.HistoryChanged += UndoManager_HistoryChanged;
  880. PDFViewer.MouseEnter += PDFViewer_MouseEnter;
  881. PDFViewer.MouseLeave += PDFViewer_MouseLeave;
  882. }
  883. private void PDFViewer_MouseLeave(object sender, MouseEventArgs e)
  884. {
  885. PDFViewer.IsVisibilityMouse(false);
  886. }
  887. private void PDFViewer_MouseEnter(object sender, MouseEventArgs e)
  888. {
  889. PDFViewer.IsVisibilityMouse(true);
  890. }
  891. private void UndoManager_HistoryChanged(object sender, KeyValuePair<UndoAction, IHistory> data)
  892. {
  893. IsDocumentModified = true;
  894. }
  895. private void CPDFViewerTool_Unloaded(object sender, RoutedEventArgs e)
  896. {
  897. PDFViewer.DrawChanged -= PDFViewer_DrawChanged;
  898. }
  899. private void PDFViewer_DrawChanged(object sender, EventArgs e)
  900. {
  901. SizeChangeds();
  902. DrawChanged?.Invoke(this, e);
  903. }
  904. public void SizeChangeds()
  905. {
  906. if (IsLoaded)
  907. {
  908. if (cacheHitTestAnnot != null)
  909. {
  910. BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
  911. bool Update = (baseLayer1 as AnnotLayer).GetUpdate(ref cacheHitTestAnnot);
  912. if (Update)
  913. {
  914. if (IsDrawEditAnnot)
  915. {
  916. SetEditAnnotObject();
  917. DrawEditAnnotLayer();
  918. }
  919. if (isDrawSelectRect)
  920. {
  921. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  922. {
  923. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  924. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  925. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  926. };
  927. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  928. {
  929. SetEditAnnotObject();
  930. DrawEditAnnotLayer();
  931. }
  932. else
  933. {
  934. SelectedAnnot();
  935. }
  936. DrawSelectedLayer();
  937. }
  938. }
  939. else
  940. {
  941. SelectedAnnot(null);
  942. }
  943. }
  944. else if (selectedPageIndex != -1 && selectedAnnotIndex != -1)
  945. {
  946. BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
  947. cacheHitTestAnnot = (baseLayer1 as AnnotLayer).GetSelectedAnnot(ref selectedPageIndex, ref selectedAnnotIndex);
  948. if (cacheHitTestAnnot != null)
  949. {
  950. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  951. {
  952. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  953. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  954. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  955. };
  956. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  957. {
  958. SetEditAnnotObject();
  959. DrawEditAnnotLayer();
  960. }
  961. else
  962. {
  963. SelectedAnnot();
  964. }
  965. DrawSelectedLayer();
  966. isDrawSelectRect = true;
  967. }
  968. }
  969. if (PDFViewer.CurrentRenderFrame != null)
  970. {
  971. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  972. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  973. {
  974. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  975. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  976. {
  977. DrawSelectedEditAreaForIndex();
  978. }
  979. }
  980. }
  981. ReDrawSelectedMultiRect();
  982. ReDrawWidget();
  983. ReDrawSelectText();
  984. ReDrawSelectImage();
  985. UpdateFormHitPop();
  986. UpdateTextPop();
  987. }
  988. }
  989. private void ScrollViewer_MouseUp(object sender, MouseButtonEventArgs e)
  990. {
  991. }
  992. private void ScrollViewer_MouseDown(object sender, MouseButtonEventArgs e)
  993. {
  994. }
  995. }
  996. }