CPDFViewerTool.xaml.cs 44 KB

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