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