CPDFViewerTool.xaml.cs 36 KB

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