CPDFViewerTool.xaml.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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 (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. DrawMoveSelectImage();
  682. }
  683. }
  684. }
  685. else
  686. {
  687. if (AnnotMoveHitTest())
  688. {
  689. if (DrawSelectRectDownEvent() == false && Mouse.LeftButton != MouseButtonState.Pressed)
  690. {
  691. mouseEventObject.annotType = caheMoveAnnot.GetAnnotData().AnnotType;
  692. }
  693. if (isCacheRedaction)
  694. {
  695. (caheMoveAnnot as RedactionAnnot)?.SetIsMouseHover(true);
  696. (caheMoveAnnot as RedactionAnnot)?.Draw();
  697. }
  698. }
  699. else
  700. {
  701. if (isCacheRedaction)
  702. {
  703. (caheMoveAnnot as RedactionAnnot)?.SetIsMouseHover(false);
  704. (caheMoveAnnot as RedactionAnnot)?.Draw();
  705. }
  706. }
  707. if (mouseEventObject.annotType == C_ANNOTATION_TYPE.C_ANNOTATION_NONE)
  708. {
  709. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  710. if (cacheHitTestAnnot != null)
  711. {
  712. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  713. mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
  714. }
  715. else
  716. {
  717. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  718. }
  719. }
  720. }
  721. if(currentModel==ToolType.WidgetEdit)
  722. {
  723. AnnotSelectAreaDraw();
  724. if (!AnnotSelectMoveDraw())
  725. {
  726. AnnotSelectAreaHover();
  727. }
  728. }
  729. MouseMoveHandler?.Invoke(this, mouseEventObject);
  730. PDFViewer.SetCustomMousePoint(Mouse.GetPosition(this).Y, Mouse.GetPosition(this).X);
  731. if (oldCursor != newCursor)
  732. {
  733. this.Cursor = newCursor;
  734. }
  735. }
  736. protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
  737. {
  738. MouseEventObject mouseEventObject = new MouseEventObject
  739. {
  740. mouseButtonEventArgs = e,
  741. hitTestType = MouseHitTestType.Unknown,
  742. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  743. IsCreate = false
  744. };
  745. if (currentModel == ToolType.Pan || currentModel == ToolType.Viewer)
  746. {
  747. if (GetMousePointToTextSelectInfo())
  748. {
  749. mouseEventObject.hitTestType = MouseHitTestType.Text;
  750. }
  751. }
  752. if (isDrawSelectRect)
  753. {
  754. // Click inside the selected rectangle area
  755. if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
  756. {
  757. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  758. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  759. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  760. return;
  761. }
  762. }
  763. if (IsDrawEditAnnot)
  764. {
  765. // Click inside the selected rectangle area
  766. if (DrawEditAnnotDownEvent() && cacheHitTestAnnot != null)
  767. {
  768. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  769. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  770. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  771. return;
  772. }
  773. }
  774. Point = e.GetPosition(this);
  775. // Annotation selection effect
  776. if ((currentModel == ToolType.Pan || currentModel == ToolType.CreateAnnot))
  777. {
  778. if (AnnotHitTest())
  779. {
  780. if (!isCacheRedaction)
  781. {
  782. if (cacheHitTestAnnot?.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK && currentModel != ToolType.CreateAnnot)
  783. {
  784. LinkAnnotAction(cacheHitTestAnnot);
  785. }
  786. else
  787. {
  788. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  789. {
  790. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  791. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  792. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  793. };
  794. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  795. {
  796. SetEditAnnotObject();
  797. DrawEditAnnotLayer();
  798. }
  799. else
  800. {
  801. SelectedAnnot();
  802. DrawSelectedLayer();
  803. }
  804. isDrawSelectRect = true;
  805. }
  806. }
  807. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  808. if (cacheHitTestAnnot != null)
  809. {
  810. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  811. }
  812. else
  813. {
  814. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  815. }
  816. }
  817. else
  818. {
  819. CleanSelectedRect();
  820. }
  821. }
  822. // Form selection effect
  823. if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer) && AnnotWidgetHitTest())
  824. {
  825. mouseEventObject.hitTestType = MouseHitTestType.Widget;
  826. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  827. FormClickProcess();
  828. }
  829. // Form creation mode
  830. if (currentModel == ToolType.WidgetEdit)
  831. {
  832. if (!AnnotSelectAreaHitTest())
  833. {
  834. if (AnnotWidgetHitTest())
  835. {
  836. cacheHitTestAnnot = PDFViewer?.AnnotHitTest() as BaseWidget;
  837. SelectedAnnot();
  838. DrawSelectedLayer();
  839. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  840. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  841. AnnotSelectClean();
  842. AnnotSelectAddItem(cacheHitTestAnnot?.GetAnnotData());
  843. }
  844. else
  845. {
  846. CleanSelectedRect();
  847. }
  848. }
  849. else
  850. {
  851. mouseEventObject.hitTestType = MouseHitTestType.MultiSelector;
  852. }
  853. }
  854. // Content editing mode
  855. if (currentModel == ToolType.ContentEdit)
  856. {
  857. if (e.ClickCount == 1)
  858. {
  859. DrawTextEditDownEvent(false);
  860. if (GetLastSelectedRect() != null)
  861. {
  862. EditAreaObject editAreaObject = GetEditAreaObjectForRect(lastSelectedRect);
  863. switch (editAreaObject.cPDFEditArea.Type)
  864. {
  865. case CPDFEditType.EditText:
  866. mouseEventObject.hitTestType = MouseHitTestType.TextEdit;
  867. break;
  868. case CPDFEditType.EditImage:
  869. mouseEventObject.hitTestType = MouseHitTestType.ImageEdit;
  870. break;
  871. default:
  872. break;
  873. }
  874. }
  875. }
  876. }
  877. else
  878. {
  879. if ((currentModel == ToolType.Viewer || currentModel == ToolType.Pan) && mouseEventObject.hitTestType == MouseHitTestType.Unknown && DrawDownSelectImage(false))
  880. {
  881. mouseEventObject.hitTestType = MouseHitTestType.ImageSelect;
  882. }
  883. }
  884. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  885. }
  886. public bool GetIsCropMode()
  887. {
  888. if (lastSelectedRect != null)
  889. {
  890. return lastSelectedRect.GetCurrentDrawPointType() == DrawPointType.Crop;
  891. }
  892. return false;
  893. }
  894. public void SetCropMode(bool crop)
  895. {
  896. if (lastSelectedRect != null)
  897. {
  898. List<PointControlType> ignoreList = new List<PointControlType>();
  899. if (crop)
  900. {
  901. lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Crop);
  902. ignoreList.Add(PointControlType.Body);
  903. // Initialize ClipRect
  904. ClipThickness = new Thickness(0, 0, 0, 0);
  905. if (editArea.TryGetValue(lastSelectedRect, out EditAreaObject editAreaObject))
  906. {
  907. cropIndex = editAreaObject.EditAreaIndex;
  908. }
  909. lastSelectedRect.DataChanged -= LastSelectedRect_DataChanged;
  910. }
  911. else
  912. {
  913. lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Square);
  914. cropIndex = -1;
  915. lastSelectedRect.DataChanged += LastSelectedRect_DataChanged;
  916. }
  917. lastSelectedRect.SetIgnorePoints(ignoreList);
  918. lastSelectedRect.Draw();
  919. }
  920. }
  921. internal void SetToolType(ToolType model)
  922. {
  923. currentModel = model;
  924. CPDFViewer pdfViewer = GetCPDFViewer();
  925. if (pdfViewer != null)
  926. {
  927. if (currentModel == ToolType.WidgetEdit)
  928. {
  929. pdfViewer.IsHideFormShow = true;
  930. }
  931. else
  932. {
  933. pdfViewer.IsHideFormShow = false;
  934. }
  935. }
  936. }
  937. public ToolType GetToolType()
  938. {
  939. return currentModel;
  940. }
  941. public void SavePoint()
  942. {
  943. CachePoint = Point;
  944. }
  945. public void CleanPoint()
  946. {
  947. CachePoint = new Point();
  948. }
  949. private void CPDFViewerTool_Loaded(object sender, RoutedEventArgs e)
  950. {
  951. PDFViewer.DrawChanged += PDFViewer_DrawChanged;
  952. PDFViewer.UndoManager.HistoryChanged += UndoManager_HistoryChanged;
  953. PDFViewer.MouseEnter += PDFViewer_MouseEnter;
  954. PDFViewer.MouseLeave += PDFViewer_MouseLeave;
  955. }
  956. private void PDFViewer_MouseLeave(object sender, MouseEventArgs e)
  957. {
  958. PDFViewer.IsVisibilityMouse(false);
  959. }
  960. private void PDFViewer_MouseEnter(object sender, MouseEventArgs e)
  961. {
  962. PDFViewer.IsVisibilityMouse(true);
  963. }
  964. private void UndoManager_HistoryChanged(object sender, KeyValuePair<UndoAction, IHistory> data)
  965. {
  966. IsDocumentModified = true;
  967. }
  968. private void CPDFViewerTool_Unloaded(object sender, RoutedEventArgs e)
  969. {
  970. PDFViewer.DrawChanged -= PDFViewer_DrawChanged;
  971. }
  972. private void PDFViewer_DrawChanged(object sender, EventArgs e)
  973. {
  974. SizeChangeds();
  975. AnnotSelectUpdate();
  976. AnnotSelectDraw();
  977. DrawChanged?.Invoke(this, e);
  978. }
  979. public void SizeChangeds()
  980. {
  981. if (IsLoaded)
  982. {
  983. if (cacheHitTestAnnot != null)
  984. {
  985. BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
  986. bool Update = (baseLayer1 as AnnotLayer).GetUpdate(ref cacheHitTestAnnot);
  987. if (Update)
  988. {
  989. if (IsDrawEditAnnot)
  990. {
  991. SetEditAnnotObject();
  992. DrawEditAnnotLayer();
  993. }
  994. if (isDrawSelectRect)
  995. {
  996. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  997. {
  998. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  999. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  1000. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  1001. };
  1002. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  1003. {
  1004. SetEditAnnotObject();
  1005. DrawEditAnnotLayer();
  1006. }
  1007. else
  1008. {
  1009. SelectedAnnot();
  1010. }
  1011. DrawSelectedLayer();
  1012. }
  1013. }
  1014. else
  1015. {
  1016. SelectedAnnot(null);
  1017. }
  1018. }
  1019. else if (selectedPageIndex != -1 && selectedAnnotIndex != -1)
  1020. {
  1021. BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
  1022. cacheHitTestAnnot = (baseLayer1 as AnnotLayer).GetSelectedAnnot(ref selectedPageIndex, ref selectedAnnotIndex);
  1023. if (cacheHitTestAnnot != null)
  1024. {
  1025. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  1026. {
  1027. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  1028. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  1029. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  1030. };
  1031. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  1032. {
  1033. SetEditAnnotObject();
  1034. DrawEditAnnotLayer();
  1035. }
  1036. else
  1037. {
  1038. SelectedAnnot();
  1039. }
  1040. DrawSelectedLayer();
  1041. isDrawSelectRect = true;
  1042. }
  1043. }
  1044. if (PDFViewer.CurrentRenderFrame != null)
  1045. {
  1046. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  1047. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  1048. {
  1049. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  1050. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  1051. {
  1052. DrawSelectedEditAreaForIndex();
  1053. }
  1054. }
  1055. }
  1056. ReDrawSelectedMultiRect();
  1057. ReDrawWidget();
  1058. ReDrawSelectText();
  1059. ReDrawSelectImage();
  1060. UpdateFormHitPop();
  1061. UpdateTextPop();
  1062. }
  1063. }
  1064. private void ScrollViewer_MouseUp(object sender, MouseButtonEventArgs e)
  1065. {
  1066. }
  1067. private void ScrollViewer_MouseDown(object sender, MouseButtonEventArgs e)
  1068. {
  1069. }
  1070. private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
  1071. {
  1072. ScrollChangedHandler?.Invoke(this, e);
  1073. }
  1074. }
  1075. }