CPDFViewerTool.xaml.cs 40 KB

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