CPDFViewerTool.xaml.cs 40 KB

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