CPDFViewerTool.xaml.cs 43 KB

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