CPDFViewerTool.xaml.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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 (!HitTestBorder())
  303. {
  304. RemovePopTextUI();
  305. }
  306. Focus();
  307. Mouse.Capture(this, CaptureMode.SubTree);
  308. MouseEventObject mouseEventObject = new MouseEventObject
  309. {
  310. mouseButtonEventArgs = e,
  311. hitTestType = MouseHitTestType.Unknown,
  312. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  313. IsCreate = false
  314. };
  315. if (isDrawSelectRect)
  316. {
  317. if (e.ClickCount == 2)
  318. {
  319. // Refresh the currently selected annotation object to ensure it is the latest
  320. AnnotHitTest();
  321. if (cacheHitTestAnnot is FreeTextAnnot)
  322. {
  323. BuildPopTextUI(cacheHitTestAnnot);
  324. isDrawSelectRect = false;
  325. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  326. mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
  327. MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
  328. return;
  329. }
  330. if (cacheHitTestAnnot is StickyNoteAnnot)
  331. {
  332. (cacheHitTestAnnot as StickyNoteAnnot).PopStickyNote();
  333. }
  334. }
  335. // Click inside the selected rectangle area
  336. if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
  337. {
  338. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  339. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  340. mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
  341. MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
  342. return;
  343. }
  344. else
  345. {
  346. CleanSelectedRect();
  347. }
  348. }
  349. if (IsDrawEditAnnot)
  350. {
  351. // Click inside the selected rectangle area
  352. if (DrawEditAnnotDownEvent() && cacheHitTestAnnot != null)
  353. {
  354. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  355. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  356. mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
  357. MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
  358. return;
  359. }
  360. }
  361. Point = e.GetPosition(this);
  362. // Annotation selection effect
  363. if ((currentModel == ToolType.Pan
  364. || currentModel == ToolType.CreateAnnot)
  365. && AnnotHitTest()
  366. && IsCanSave()
  367. && !PDFViewer.GetIsShowStampMouse()
  368. )
  369. {
  370. //if (!IsCacheRedaction)
  371. {
  372. if (cacheHitTestAnnot?.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK && currentModel != ToolType.CreateAnnot)
  373. {
  374. LinkAnnotAction(cacheHitTestAnnot);
  375. }
  376. else
  377. {
  378. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  379. {
  380. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  381. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  382. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  383. };
  384. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  385. {
  386. mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
  387. SetEditAnnotObject();
  388. }
  389. else
  390. {
  391. SelectedAnnot();
  392. CleanDrawSelectImage();
  393. }
  394. isDrawSelectRect = true;
  395. }
  396. }
  397. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  398. if (cacheHitTestAnnot != null)
  399. {
  400. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  401. }
  402. else
  403. {
  404. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  405. }
  406. }
  407. // Form selected effect
  408. else if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer) && AnnotWidgetHitTest())
  409. {
  410. mouseEventObject.hitTestType = MouseHitTestType.Widget;
  411. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  412. FormClickProcess();
  413. }
  414. else if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer))
  415. {
  416. if (DrawDownSelectImage(true))
  417. {
  418. mouseEventObject.hitTestType = MouseHitTestType.ImageSelect;
  419. }
  420. else
  421. {
  422. ReDrawSelectImage();
  423. }
  424. }
  425. // Form creation mode
  426. else if (currentModel == ToolType.WidgetEdit)
  427. {
  428. if (AnnotWidgetHitTest())
  429. {
  430. cacheHitTestAnnot = PDFViewer?.AnnotHitTest() as BaseWidget;
  431. SelectedAnnot();
  432. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  433. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  434. }
  435. }
  436. // Content editing mode
  437. else if (currentModel == ToolType.ContentEdit)
  438. {
  439. OpenSelectedMulti(isMultiSelected);
  440. if (!PDFViewer.GetIsShowStampMouse())
  441. {
  442. DrawTextEditDownEvent(true);
  443. }
  444. if (lastSelectedRect != null)
  445. {
  446. //Multi selection processing optimization, other click effects
  447. DrawEndFrameSelect();
  448. if (!Keyboard.IsKeyDown(multiKey) || !isMultiSelected)
  449. {
  450. CleanSelectedMultiRect();
  451. OpenSelectedMulti(false);
  452. if (PDFViewer.CurrentRenderFrame != null)
  453. {
  454. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  455. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  456. {
  457. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  458. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  459. {
  460. DrawSelectedEditAreaForIndex();
  461. }
  462. }
  463. }
  464. ReDrawSelectedMultiRect();
  465. }
  466. if (lastSelectedRect == null)
  467. {
  468. return;
  469. }
  470. SelectedMultiRect(lastSelectedRect.GetRect(), lastSelectedRect.GetMaxRect(), SelectedType.PDFEdit);
  471. HideDrawSelectedMultiRect();
  472. lastSelectedRect.DataChanged -= SelectedRect_DataChanged;
  473. lastSelectedRect.DataChanged += SelectedRect_DataChanged;
  474. }
  475. else
  476. {
  477. if (Keyboard.IsKeyDown(multiKey) && isMultiSelected)
  478. {
  479. DelMultiSelectRect();
  480. }
  481. if (HitTestMultiSelectedRect())
  482. {
  483. mouseEventObject.hitTestType = MouseHitTestType.MultiTextEdit;
  484. }
  485. else
  486. {
  487. //Clear the currently selected object
  488. startSelectedRect = null;
  489. startSelectedIndex = -1;
  490. startSelectedPageIndex = -1;
  491. startSelectedEditAreaObject = null;
  492. CleanSelectedMultiRect();
  493. OpenSelectedMulti(false);
  494. if (PDFViewer.CurrentRenderFrame != null)
  495. {
  496. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  497. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  498. {
  499. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  500. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  501. {
  502. DrawSelectedEditAreaForIndex();
  503. }
  504. }
  505. }
  506. ReDrawSelectedMultiRect();
  507. }
  508. }
  509. }
  510. else if (currentModel == ToolType.SelectedPage)
  511. {
  512. if (HitTestPageSelectedRect())
  513. {
  514. }
  515. else
  516. {
  517. CleanPageSelectedRect();
  518. CreatePageSelectdRect();
  519. }
  520. mouseEventObject.hitTestType = MouseHitTestType.SelectedPageRect;
  521. }
  522. MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
  523. }
  524. protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
  525. {
  526. if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null)
  527. {
  528. return;
  529. }
  530. MouseEventObject mouseEventObject = new MouseEventObject
  531. {
  532. mouseButtonEventArgs = e,
  533. hitTestType = MouseHitTestType.Unknown,
  534. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  535. IsCreate = false
  536. };
  537. if (isDrawSelectRect || IsDrawEditAnnot)
  538. {
  539. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  540. if (cacheHitTestAnnot != null)
  541. {
  542. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  543. }
  544. else
  545. {
  546. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  547. }
  548. MouseLeftButtonUpHandler?.Invoke(this, mouseEventObject);
  549. ReleaseMouseCapture();
  550. return;
  551. }
  552. MouseLeftButtonUpHandler?.Invoke(this, mouseEventObject);
  553. ReleaseMouseCapture();
  554. }
  555. protected override void OnMouseMove(MouseEventArgs e)
  556. {
  557. if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null)
  558. {
  559. return;
  560. }
  561. Cursor oldCursor = this.Cursor;
  562. Cursor newCursor = this.Cursor;
  563. MouseEventObject mouseEventObject = new MouseEventObject
  564. {
  565. mouseButtonEventArgs = e,
  566. hitTestType = MouseHitTestType.Unknown,
  567. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  568. IsCreate = false
  569. };
  570. if (Mouse.LeftButton != MouseButtonState.Pressed)
  571. {
  572. List<ToolType> allowModeList = new List<ToolType>()
  573. {
  574. ToolType.Pan,
  575. ToolType.Viewer
  576. };
  577. if (allowModeList.Contains(currentModel))
  578. {
  579. newCursor = Cursors.Arrow;
  580. if (caheMoveAnnot is BaseWidget)
  581. {
  582. BaseWidget widget = (BaseWidget)caheMoveAnnot;
  583. if (widget.GetFormType() == PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON && PDFViewer != null)
  584. {
  585. newCursor = Cursors.Hand;
  586. }
  587. }
  588. if (caheMoveAnnot is LinkAnnot)
  589. {
  590. newCursor = Cursors.Hand;
  591. }
  592. }
  593. }
  594. if (!isDrawSelectRect && !IsDrawEditAnnot)
  595. {
  596. if (AnnotMoveHitTest())
  597. {
  598. if (isCacheRedaction)
  599. {
  600. (caheMoveAnnot as RedactionAnnot).SetIsMouseHover(true);
  601. (caheMoveAnnot as RedactionAnnot).Draw();
  602. }
  603. mouseEventObject.annotType = caheMoveAnnot.GetAnnotData().AnnotType;
  604. mouseEventObject.IsMersured = caheMoveAnnot.GetAnnotData().Annot.IsMeasured();
  605. }
  606. else
  607. {
  608. if (isCacheRedaction)
  609. {
  610. isCacheRedaction = false;
  611. (caheMoveAnnot as RedactionAnnot).SetIsMouseHover(false);
  612. (caheMoveAnnot as RedactionAnnot).Draw();
  613. }
  614. caheMoveAnnot = null;
  615. if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer))
  616. {
  617. DrawMoveSelectImage();
  618. }
  619. }
  620. }
  621. else
  622. {
  623. if (AnnotMoveHitTest())
  624. {
  625. if (DrawSelectRectDownEvent() == false && Mouse.LeftButton != MouseButtonState.Pressed)
  626. {
  627. mouseEventObject.annotType = caheMoveAnnot.GetAnnotData().AnnotType;
  628. }
  629. if (isCacheRedaction)
  630. {
  631. (caheMoveAnnot as RedactionAnnot)?.SetIsMouseHover(true);
  632. (caheMoveAnnot as RedactionAnnot)?.Draw();
  633. }
  634. }
  635. else
  636. {
  637. if (isCacheRedaction)
  638. {
  639. (caheMoveAnnot as RedactionAnnot)?.SetIsMouseHover(false);
  640. (caheMoveAnnot as RedactionAnnot)?.Draw();
  641. }
  642. }
  643. if (mouseEventObject.annotType == C_ANNOTATION_TYPE.C_ANNOTATION_NONE)
  644. {
  645. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  646. if (cacheHitTestAnnot != null)
  647. {
  648. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  649. mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
  650. }
  651. else
  652. {
  653. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  654. }
  655. }
  656. }
  657. MouseMoveHandler?.Invoke(this, mouseEventObject);
  658. PDFViewer.SetCustomMousePoint(Mouse.GetPosition(this).Y, Mouse.GetPosition(this).X);
  659. if (oldCursor != newCursor)
  660. {
  661. this.Cursor = newCursor;
  662. }
  663. }
  664. protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
  665. {
  666. MouseEventObject mouseEventObject = new MouseEventObject
  667. {
  668. mouseButtonEventArgs = e,
  669. hitTestType = MouseHitTestType.Unknown,
  670. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  671. IsCreate = false
  672. };
  673. if (currentModel == ToolType.Pan || currentModel == ToolType.Viewer)
  674. {
  675. if (GetMousePointToTextSelectInfo())
  676. {
  677. mouseEventObject.hitTestType = MouseHitTestType.Text;
  678. }
  679. }
  680. if (isDrawSelectRect)
  681. {
  682. // Click inside the selected rectangle area
  683. if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
  684. {
  685. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  686. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  687. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  688. return;
  689. }
  690. }
  691. if (IsDrawEditAnnot)
  692. {
  693. // Click inside the selected rectangle area
  694. if (DrawEditAnnotDownEvent() && cacheHitTestAnnot != null)
  695. {
  696. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  697. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  698. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  699. return;
  700. }
  701. }
  702. Point = e.GetPosition(this);
  703. // Annotation selection effect
  704. if ((currentModel == ToolType.Pan || currentModel == ToolType.CreateAnnot))
  705. {
  706. if (AnnotHitTest())
  707. {
  708. if (!isCacheRedaction)
  709. {
  710. if (cacheHitTestAnnot?.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK && currentModel != ToolType.CreateAnnot)
  711. {
  712. LinkAnnotAction(cacheHitTestAnnot);
  713. }
  714. else
  715. {
  716. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  717. {
  718. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  719. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  720. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  721. };
  722. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  723. {
  724. SetEditAnnotObject();
  725. DrawEditAnnotLayer();
  726. }
  727. else
  728. {
  729. SelectedAnnot();
  730. DrawSelectedLayer();
  731. }
  732. isDrawSelectRect = true;
  733. }
  734. }
  735. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  736. if (cacheHitTestAnnot != null)
  737. {
  738. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  739. }
  740. else
  741. {
  742. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  743. }
  744. }
  745. else
  746. {
  747. CleanSelectedRect();
  748. }
  749. }
  750. // Form selection effect
  751. if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer) && AnnotWidgetHitTest())
  752. {
  753. mouseEventObject.hitTestType = MouseHitTestType.Widget;
  754. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  755. FormClickProcess();
  756. }
  757. // Form creation mode
  758. if (currentModel == ToolType.WidgetEdit)
  759. {
  760. if (AnnotWidgetHitTest())
  761. {
  762. cacheHitTestAnnot = PDFViewer?.AnnotHitTest() as BaseWidget;
  763. SelectedAnnot();
  764. DrawSelectedLayer();
  765. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  766. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  767. }
  768. else
  769. {
  770. CleanSelectedRect();
  771. }
  772. }
  773. // Content editing mode
  774. if (currentModel == ToolType.ContentEdit)
  775. {
  776. if (e.ClickCount == 1)
  777. {
  778. DrawTextEditDownEvent(false);
  779. if (GetLastSelectedRect() != null)
  780. {
  781. EditAreaObject editAreaObject = GetEditAreaObjectForRect(lastSelectedRect);
  782. switch (editAreaObject.cPDFEditArea.Type)
  783. {
  784. case CPDFEditType.EditText:
  785. mouseEventObject.hitTestType = MouseHitTestType.TextEdit;
  786. break;
  787. case CPDFEditType.EditImage:
  788. mouseEventObject.hitTestType = MouseHitTestType.ImageEdit;
  789. break;
  790. default:
  791. break;
  792. }
  793. }
  794. }
  795. }
  796. else
  797. {
  798. if ((currentModel == ToolType.Viewer || currentModel == ToolType.Pan) && mouseEventObject.hitTestType == MouseHitTestType.Unknown && DrawDownSelectImage(false))
  799. {
  800. mouseEventObject.hitTestType = MouseHitTestType.ImageSelect;
  801. }
  802. }
  803. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  804. }
  805. public bool GetIsCropMode()
  806. {
  807. if (lastSelectedRect != null)
  808. {
  809. return lastSelectedRect.GetCurrentDrawPointType() == DrawPointType.Crop;
  810. }
  811. return false;
  812. }
  813. public void SetCropMode(bool crop)
  814. {
  815. if (lastSelectedRect != null)
  816. {
  817. List<PointControlType> ignoreList = new List<PointControlType>();
  818. if (crop)
  819. {
  820. lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Crop);
  821. ignoreList.Add(PointControlType.Body);
  822. // Initialize ClipRect
  823. ClipThickness = new Thickness(0, 0, 0, 0);
  824. if (editArea.TryGetValue(lastSelectedRect, out EditAreaObject editAreaObject))
  825. {
  826. cropIndex = editAreaObject.EditAreaIndex;
  827. }
  828. lastSelectedRect.DataChanged -= LastSelectedRect_DataChanged;
  829. }
  830. else
  831. {
  832. lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Square);
  833. cropIndex = -1;
  834. lastSelectedRect.DataChanged += LastSelectedRect_DataChanged;
  835. }
  836. lastSelectedRect.SetIgnorePoints(ignoreList);
  837. lastSelectedRect.Draw();
  838. }
  839. }
  840. internal void SetToolType(ToolType model)
  841. {
  842. currentModel = model;
  843. CPDFViewer pdfViewer = GetCPDFViewer();
  844. if (pdfViewer != null)
  845. {
  846. if (currentModel == ToolType.WidgetEdit)
  847. {
  848. pdfViewer.IsHideFormShow = true;
  849. }
  850. else
  851. {
  852. pdfViewer.IsHideFormShow = false;
  853. }
  854. }
  855. }
  856. public ToolType GetToolType()
  857. {
  858. return currentModel;
  859. }
  860. public void SavePoint()
  861. {
  862. CachePoint = Point;
  863. }
  864. public void CleanPoint()
  865. {
  866. CachePoint = new Point();
  867. }
  868. private void CPDFViewerTool_Loaded(object sender, RoutedEventArgs e)
  869. {
  870. PDFViewer.DrawChanged += PDFViewer_DrawChanged;
  871. PDFViewer.UndoManager.HistoryChanged += UndoManager_HistoryChanged;
  872. PDFViewer.MouseEnter += PDFViewer_MouseEnter;
  873. PDFViewer.MouseLeave += PDFViewer_MouseLeave;
  874. }
  875. private void PDFViewer_MouseLeave(object sender, MouseEventArgs e)
  876. {
  877. PDFViewer.IsVisibilityMouse(false);
  878. }
  879. private void PDFViewer_MouseEnter(object sender, MouseEventArgs e)
  880. {
  881. PDFViewer.IsVisibilityMouse(true);
  882. }
  883. private void UndoManager_HistoryChanged(object sender, KeyValuePair<UndoAction, IHistory> data)
  884. {
  885. IsDocumentModified = true;
  886. }
  887. private void CPDFViewerTool_Unloaded(object sender, RoutedEventArgs e)
  888. {
  889. PDFViewer.DrawChanged -= PDFViewer_DrawChanged;
  890. }
  891. private void PDFViewer_DrawChanged(object sender, EventArgs e)
  892. {
  893. SizeChangeds();
  894. DrawChanged?.Invoke(this, e);
  895. }
  896. public void SizeChangeds()
  897. {
  898. if (IsLoaded)
  899. {
  900. if (cacheHitTestAnnot != null)
  901. {
  902. BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
  903. bool Update = (baseLayer1 as AnnotLayer).GetUpdate(ref cacheHitTestAnnot);
  904. if (Update)
  905. {
  906. if (IsDrawEditAnnot)
  907. {
  908. SetEditAnnotObject();
  909. DrawEditAnnotLayer();
  910. }
  911. if (isDrawSelectRect)
  912. {
  913. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  914. {
  915. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  916. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  917. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  918. };
  919. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  920. {
  921. SetEditAnnotObject();
  922. DrawEditAnnotLayer();
  923. }
  924. else
  925. {
  926. SelectedAnnot();
  927. }
  928. DrawSelectedLayer();
  929. }
  930. }
  931. else
  932. {
  933. SelectedAnnot(null);
  934. }
  935. }
  936. else if (selectedPageIndex != -1 && selectedAnnotIndex != -1)
  937. {
  938. BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
  939. cacheHitTestAnnot = (baseLayer1 as AnnotLayer).GetSelectedAnnot(ref selectedPageIndex, ref selectedAnnotIndex);
  940. if (cacheHitTestAnnot != null)
  941. {
  942. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  943. {
  944. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  945. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  946. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  947. };
  948. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  949. {
  950. SetEditAnnotObject();
  951. DrawEditAnnotLayer();
  952. }
  953. else
  954. {
  955. SelectedAnnot();
  956. }
  957. DrawSelectedLayer();
  958. isDrawSelectRect = true;
  959. }
  960. }
  961. if (PDFViewer.CurrentRenderFrame != null)
  962. {
  963. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  964. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  965. {
  966. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  967. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  968. {
  969. DrawSelectedEditAreaForIndex();
  970. }
  971. }
  972. }
  973. ReDrawSelectedMultiRect();
  974. ReDrawWidget();
  975. ReDrawSelectText();
  976. ReDrawSelectImage();
  977. UpdateFormHitPop();
  978. UpdateTextPop();
  979. }
  980. }
  981. private void ScrollViewer_MouseUp(object sender, MouseButtonEventArgs e)
  982. {
  983. }
  984. private void ScrollViewer_MouseDown(object sender, MouseButtonEventArgs e)
  985. {
  986. }
  987. }
  988. }