CPDFViewerTool.xaml.cs 40 KB

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