CPDFViewerTool.xaml.cs 39 KB

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