CPDFViewerTool.xaml.cs 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit.PDFDocument.Action;
  4. using ComPDFKit.PDFPage;
  5. using ComPDFKit.Tool.DrawTool;
  6. using ComPDFKit.Tool.FillTool;
  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. PathEdit,
  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 || PDFViewer.IsRendering)
  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 (AnnotHitTest() && cacheHitTestAnnot.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK)
  443. {
  444. LinkAnnotAction(cacheHitTestAnnot);
  445. }
  446. if (!IsText() && DrawDownSelectImage(true))
  447. {
  448. mouseEventObject.hitTestType = MouseHitTestType.ImageSelect;
  449. }
  450. else
  451. {
  452. ReDrawSelectImage();
  453. }
  454. }
  455. // Form creation mode
  456. else if (currentModel == ToolType.WidgetEdit)
  457. {
  458. if (!AnnotSelectAreaHitTest())
  459. {
  460. if (AnnotWidgetHitTest())
  461. {
  462. BaseWidget hitWidget = PDFViewer?.AnnotHitTest() as BaseWidget;
  463. cacheHitTestAnnot = null;
  464. if (AnnotSelectGetCount() == 0 && hitWidget != null)
  465. {
  466. AnnotSelectAddItem(hitWidget.GetAnnotData());
  467. cacheHitTestAnnot = hitWidget;
  468. SelectedAnnot();
  469. }
  470. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  471. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  472. }
  473. else
  474. {
  475. AnnotSelectAreaHit();
  476. }
  477. }
  478. AnnotSelectMoveHit();
  479. }
  480. // Content editing mode
  481. else if (currentModel == ToolType.ContentEdit)
  482. {
  483. OpenSelectedMulti(isMultiSelected);
  484. if (!PDFViewer.GetIsShowStampMouse())
  485. {
  486. DrawTextEditDownEvent(true);
  487. }
  488. if (lastSelectedRect != null)
  489. {
  490. //Multi selection processing optimization, other click effects
  491. DrawEndFrameSelect();
  492. if (!Keyboard.IsKeyDown(multiKey) || !isMultiSelected)
  493. {
  494. CleanSelectedMultiRect();
  495. OpenSelectedMulti(false);
  496. if (PDFViewer.CurrentRenderFrame != null)
  497. {
  498. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  499. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  500. {
  501. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  502. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  503. {
  504. DrawSelectedEditAreaForIndex();
  505. }
  506. }
  507. }
  508. ReDrawSelectedMultiRect();
  509. }
  510. if (lastSelectedRect == null)
  511. {
  512. return;
  513. }
  514. SelectedMultiRect(lastSelectedRect.GetRect(), lastSelectedRect.GetMaxRect(), SelectedType.PDFEdit);
  515. HideDrawSelectedMultiRect();
  516. lastSelectedRect.DataChanged -= SelectedRect_DataChanged;
  517. lastSelectedRect.DataChanged += SelectedRect_DataChanged;
  518. }
  519. else
  520. {
  521. if (Keyboard.IsKeyDown(multiKey) && isMultiSelected)
  522. {
  523. DelMultiSelectRect();
  524. }
  525. if (HitTestMultiSelectedRect())
  526. {
  527. mouseEventObject.hitTestType = MouseHitTestType.MultiTextEdit;
  528. }
  529. else
  530. {
  531. //Clear the currently selected object
  532. startSelectedRect = null;
  533. startSelectedIndex = -1;
  534. startSelectedPageIndex = -1;
  535. startSelectedEditAreaObject = null;
  536. CleanSelectedMultiRect();
  537. OpenSelectedMulti(false);
  538. if (PDFViewer.CurrentRenderFrame != null)
  539. {
  540. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  541. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  542. {
  543. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  544. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  545. {
  546. DrawSelectedEditAreaForIndex();
  547. }
  548. }
  549. }
  550. ReDrawSelectedMultiRect();
  551. }
  552. }
  553. }
  554. else if (currentModel == ToolType.SelectedPage)
  555. {
  556. if (HitTestPageSelectedRect())
  557. {
  558. }
  559. else
  560. {
  561. CleanPageSelectedRect();
  562. CreatePageSelectdRect();
  563. }
  564. mouseEventObject.hitTestType = MouseHitTestType.SelectedPageRect;
  565. }
  566. MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
  567. }
  568. protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
  569. {
  570. if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null || PDFViewer.IsRendering)
  571. {
  572. return;
  573. }
  574. if (FillManager?.MouseUpHandle(e) == true)
  575. {
  576. return;
  577. }
  578. if (currentModel == ToolType.WidgetEdit)
  579. {
  580. bool areaSelectAdd = AnnotSelectAreaSelect(true);
  581. if (AnnotWidgetHitTest())
  582. {
  583. BaseWidget checkItem = PDFViewer?.AnnotHitTest() as BaseWidget;
  584. AnnotSelectAddItem(checkItem?.GetAnnotData(), !AnnotSelectAreaHitTest());
  585. }
  586. if (AnnotSelectGetCount() > 1)
  587. {
  588. CleanSelectedRect();
  589. }
  590. if (IsMoved == false && AnnotSelectAreaHitTest() == false && AnnotWidgetHitTest() == false)
  591. {
  592. AnnotSelectClean();
  593. }
  594. if (AnnotSelectGetCount() == 1)
  595. {
  596. cacheHitTestAnnot = AnnotSelectGetAnnot();
  597. if (cacheHitTestAnnot != null)
  598. {
  599. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  600. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  601. if (selectedRect != null)
  602. {
  603. if (IsMoved && areaSelectAdd == false)
  604. {
  605. selectedRect.SetAnnotData(cacheHitTestAnnot.GetAnnotData(),PDFViewer);
  606. }
  607. else
  608. {
  609. selectedRect.SetAnnotData(cacheHitTestAnnot.GetAnnotData(), PDFViewer);
  610. }
  611. selectedRect.Draw();
  612. }
  613. isDrawSelectRect = true;
  614. }
  615. }
  616. }
  617. MouseEventObject mouseEventObject = new MouseEventObject
  618. {
  619. mouseButtonEventArgs = e,
  620. hitTestType = MouseHitTestType.Unknown,
  621. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  622. IsCreate = false
  623. };
  624. if (isDrawSelectRect || IsDrawEditAnnot)
  625. {
  626. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  627. if (cacheHitTestAnnot != null)
  628. {
  629. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  630. }
  631. else
  632. {
  633. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  634. }
  635. }
  636. MouseLeftButtonUpHandler?.Invoke(this, mouseEventObject);
  637. ReleaseMouseCapture();
  638. if (currentModel == ToolType.WidgetEdit)
  639. {
  640. AnnotSelectUpdate();
  641. AnnotSelectDraw();
  642. AnnotSelectSave();
  643. AreaMoveData = new AnnotSelectAreaData();
  644. }
  645. IsMoved = false;
  646. }
  647. protected override async void OnMouseMove(MouseEventArgs e)
  648. {
  649. IsMoved = e.LeftButton == MouseButtonState.Pressed;
  650. if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null || PDFViewer.IsRendering)
  651. {
  652. return;
  653. }
  654. if(FillManager?.MouseMoveHandle(e)==true)
  655. {
  656. return;
  657. }
  658. Cursor oldCursor = this.Cursor;
  659. Cursor newCursor = this.Cursor;
  660. MouseEventObject mouseEventObject = new MouseEventObject
  661. {
  662. mouseButtonEventArgs = e,
  663. hitTestType = MouseHitTestType.Unknown,
  664. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  665. IsCreate = false
  666. };
  667. if (currentModel == ToolType.ViewerScroller)
  668. {
  669. // Scroller Tool Only Scrolle
  670. MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
  671. return;
  672. }
  673. if (Mouse.LeftButton != MouseButtonState.Pressed)
  674. {
  675. List<ToolType> allowModeList = new List<ToolType>()
  676. {
  677. ToolType.Pan,
  678. ToolType.Viewer
  679. };
  680. if (allowModeList.Contains(currentModel))
  681. {
  682. newCursor = Cursors.Arrow;
  683. if (caheMoveAnnot is BaseWidget)
  684. {
  685. BaseWidget widget = (BaseWidget)caheMoveAnnot;
  686. if (widget.GetFormType() == PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON && PDFViewer != null)
  687. {
  688. newCursor = Cursors.Hand;
  689. }
  690. }
  691. if (caheMoveAnnot is LinkAnnot)
  692. {
  693. newCursor = Cursors.Hand;
  694. }
  695. }
  696. }
  697. if (!isDrawSelectRect && !IsDrawEditAnnot)
  698. {
  699. if (AnnotMoveHitTest())
  700. {
  701. if (isCacheRedaction)
  702. {
  703. (caheMoveAnnot as RedactionAnnot).SetIsMouseHover(true);
  704. (caheMoveAnnot as RedactionAnnot).Draw();
  705. }
  706. mouseEventObject.annotType = caheMoveAnnot.GetAnnotData().AnnotType;
  707. mouseEventObject.IsMersured = caheMoveAnnot.GetAnnotData().Annot.IsMeasured();
  708. }
  709. else
  710. {
  711. if (isCacheRedaction)
  712. {
  713. isCacheRedaction = false;
  714. (caheMoveAnnot as RedactionAnnot).SetIsMouseHover(false);
  715. (caheMoveAnnot as RedactionAnnot).Draw();
  716. }
  717. caheMoveAnnot = null;
  718. if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer))
  719. {
  720. if (GetTextSelectInfo() == null)
  721. {
  722. DrawMoveSelectImage();
  723. }
  724. }
  725. }
  726. }
  727. else
  728. {
  729. if (AnnotMoveHitTest())
  730. {
  731. if (DrawSelectRectDownEvent() == false && Mouse.LeftButton != MouseButtonState.Pressed)
  732. {
  733. mouseEventObject.annotType = caheMoveAnnot.GetAnnotData().AnnotType;
  734. }
  735. if (isCacheRedaction)
  736. {
  737. (caheMoveAnnot as RedactionAnnot)?.SetIsMouseHover(true);
  738. (caheMoveAnnot as RedactionAnnot)?.Draw();
  739. }
  740. }
  741. else
  742. {
  743. if (isCacheRedaction)
  744. {
  745. (caheMoveAnnot as RedactionAnnot)?.SetIsMouseHover(false);
  746. (caheMoveAnnot as RedactionAnnot)?.Draw();
  747. }
  748. }
  749. if (mouseEventObject.annotType == C_ANNOTATION_TYPE.C_ANNOTATION_NONE)
  750. {
  751. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  752. if (cacheHitTestAnnot != null)
  753. {
  754. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  755. mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
  756. }
  757. else
  758. {
  759. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  760. }
  761. }
  762. }
  763. if (currentModel == ToolType.WidgetEdit)
  764. {
  765. AnnotSelectAreaDraw();
  766. if (!AnnotSelectMoveDraw())
  767. {
  768. AnnotSelectAreaHover();
  769. }
  770. }
  771. MouseMoveHandler?.Invoke(this, mouseEventObject);
  772. PDFViewer.SetCustomMousePoint(Mouse.GetPosition(this).Y, Mouse.GetPosition(this).X);
  773. if (oldCursor != newCursor)
  774. {
  775. this.Cursor = newCursor;
  776. }
  777. }
  778. protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
  779. {
  780. if(PDFViewer.IsRendering)
  781. {
  782. return;
  783. }
  784. MouseEventObject mouseEventObject = new MouseEventObject
  785. {
  786. mouseButtonEventArgs = e,
  787. hitTestType = MouseHitTestType.Unknown,
  788. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  789. IsCreate = false
  790. };
  791. if (currentModel == ToolType.Pan || currentModel == ToolType.Viewer)
  792. {
  793. if (GetMousePointToTextSelectInfo())
  794. {
  795. mouseEventObject.hitTestType = MouseHitTestType.Text;
  796. }
  797. }
  798. if (isDrawSelectRect)
  799. {
  800. // Click inside the selected rectangle area
  801. if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
  802. {
  803. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  804. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  805. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  806. return;
  807. }
  808. }
  809. if (IsDrawEditAnnot)
  810. {
  811. // Click inside the selected rectangle area
  812. if (DrawEditAnnotDownEvent() && cacheHitTestAnnot != null)
  813. {
  814. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  815. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  816. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  817. return;
  818. }
  819. }
  820. Point = e.GetPosition(this);
  821. // Annotation selection effect
  822. if ((currentModel == ToolType.Pan || currentModel == ToolType.CreateAnnot))
  823. {
  824. if (AnnotHitTest())
  825. {
  826. if (!isCacheRedaction)
  827. {
  828. if (cacheHitTestAnnot?.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK && currentModel != ToolType.CreateAnnot)
  829. {
  830. LinkAnnotAction(cacheHitTestAnnot);
  831. }
  832. else
  833. {
  834. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  835. {
  836. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  837. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  838. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  839. };
  840. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  841. {
  842. SetEditAnnotObject();
  843. DrawEditAnnotLayer();
  844. }
  845. else
  846. {
  847. SelectedAnnot();
  848. DrawSelectedLayer();
  849. }
  850. isDrawSelectRect = true;
  851. }
  852. }
  853. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  854. if (cacheHitTestAnnot != null)
  855. {
  856. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  857. }
  858. else
  859. {
  860. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  861. }
  862. }
  863. else
  864. {
  865. CleanSelectedRect();
  866. }
  867. }
  868. // Form selection effect
  869. if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer) && AnnotWidgetHitTest())
  870. {
  871. mouseEventObject.hitTestType = MouseHitTestType.Widget;
  872. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  873. FormClickProcess();
  874. }
  875. // Form creation mode
  876. if (currentModel == ToolType.WidgetEdit)
  877. {
  878. if (!AnnotSelectAreaHitTest())
  879. {
  880. if (AnnotWidgetHitTest())
  881. {
  882. cacheHitTestAnnot = PDFViewer?.AnnotHitTest() as BaseWidget;
  883. SelectedAnnot();
  884. DrawSelectedLayer();
  885. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  886. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  887. AnnotSelectClean();
  888. AnnotSelectAddItem(cacheHitTestAnnot?.GetAnnotData());
  889. }
  890. else
  891. {
  892. CleanSelectedRect();
  893. }
  894. }
  895. else
  896. {
  897. mouseEventObject.hitTestType = MouseHitTestType.MultiSelector;
  898. }
  899. }
  900. // Content editing mode
  901. if (currentModel == ToolType.ContentEdit)
  902. {
  903. if (e.ClickCount == 1)
  904. {
  905. DrawTextEditDownEvent(false);
  906. if (GetLastSelectedRect() != null)
  907. {
  908. EditAreaObject editAreaObject = GetEditAreaObjectForRect(lastSelectedRect);
  909. switch (editAreaObject.cPDFEditArea.Type)
  910. {
  911. case CPDFEditType.EditText:
  912. mouseEventObject.hitTestType = MouseHitTestType.TextEdit;
  913. break;
  914. case CPDFEditType.EditImage:
  915. mouseEventObject.hitTestType = MouseHitTestType.ImageEdit;
  916. break;
  917. case CPDFEditType.EditPath:
  918. mouseEventObject.hitTestType = MouseHitTestType.PathEdit;
  919. break;
  920. default:
  921. break;
  922. }
  923. }
  924. }
  925. }
  926. else
  927. {
  928. if ((currentModel == ToolType.Viewer || currentModel == ToolType.Pan) && mouseEventObject.hitTestType == MouseHitTestType.Unknown && DrawDownSelectImage(false))
  929. {
  930. mouseEventObject.hitTestType = MouseHitTestType.ImageSelect;
  931. }
  932. }
  933. if (currentModel == ToolType.SelectedPage)
  934. {
  935. if (HitTestPageSelectedRect())
  936. {
  937. mouseEventObject.hitTestType = MouseHitTestType.SelectedPageRect;
  938. }
  939. }
  940. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  941. }
  942. public bool GetIsCropMode()
  943. {
  944. if (lastSelectedRect != null)
  945. {
  946. return lastSelectedRect.GetCurrentDrawPointType() == DrawPointType.Crop;
  947. }
  948. return false;
  949. }
  950. public void SetCropMode(bool crop)
  951. {
  952. if (lastSelectedRect != null)
  953. {
  954. List<PointControlType> ignoreList = new List<PointControlType>();
  955. if (crop)
  956. {
  957. lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Crop);
  958. ignoreList.Add(PointControlType.Body);
  959. // Initialize ClipRect
  960. ClipThickness = new Thickness(0, 0, 0, 0);
  961. if (editArea.TryGetValue(lastSelectedRect, out EditAreaObject editAreaObject))
  962. {
  963. cropIndex = editAreaObject.EditAreaIndex;
  964. }
  965. lastSelectedRect.DataChanged -= LastSelectedRect_DataChanged;
  966. }
  967. else
  968. {
  969. lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Square);
  970. cropIndex = -1;
  971. lastSelectedRect.DataChanged += LastSelectedRect_DataChanged;
  972. }
  973. lastSelectedRect.SetIgnorePoints(ignoreList);
  974. lastSelectedRect.Draw();
  975. }
  976. }
  977. internal void SetToolType(ToolType model)
  978. {
  979. currentModel = model;
  980. CPDFViewer pdfViewer = GetCPDFViewer();
  981. if (pdfViewer != null)
  982. {
  983. if (currentModel == ToolType.WidgetEdit)
  984. {
  985. pdfViewer.IsHideFormShow = true;
  986. }
  987. else
  988. {
  989. pdfViewer.IsHideFormShow = false;
  990. }
  991. }
  992. }
  993. public ToolType GetToolType()
  994. {
  995. return currentModel;
  996. }
  997. public void SavePoint()
  998. {
  999. CachePoint = Point;
  1000. }
  1001. public void CleanPoint()
  1002. {
  1003. CachePoint = new Point();
  1004. }
  1005. private void CPDFViewerTool_Loaded(object sender, RoutedEventArgs e)
  1006. {
  1007. PDFViewer.DrawChanged += PDFViewer_DrawChanged;
  1008. PDFViewer.UndoManager.HistoryChanged += UndoManager_HistoryChanged;
  1009. PDFViewer.MouseEnter += PDFViewer_MouseEnter;
  1010. PDFViewer.MouseLeave += PDFViewer_MouseLeave;
  1011. }
  1012. private void PDFViewer_MouseLeave(object sender, MouseEventArgs e)
  1013. {
  1014. PDFViewer.IsVisibilityMouse(false);
  1015. }
  1016. private void PDFViewer_MouseEnter(object sender, MouseEventArgs e)
  1017. {
  1018. PDFViewer.IsVisibilityMouse(true);
  1019. }
  1020. private void UndoManager_HistoryChanged(object sender, KeyValuePair<UndoAction, IHistory> data)
  1021. {
  1022. IsDocumentModified = true;
  1023. }
  1024. private void CPDFViewerTool_Unloaded(object sender, RoutedEventArgs e)
  1025. {
  1026. PDFViewer.DrawChanged -= PDFViewer_DrawChanged;
  1027. }
  1028. private void PDFViewer_DrawChanged(object sender, EventArgs e)
  1029. {
  1030. SizeChangeds();
  1031. AnnotSelectUpdate();
  1032. AnnotSelectDraw();
  1033. DrawChanged?.Invoke(this, e);
  1034. }
  1035. public void SizeChangeds()
  1036. {
  1037. if (IsLoaded)
  1038. {
  1039. if (cacheHitTestAnnot != null)
  1040. {
  1041. BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
  1042. bool Update = (baseLayer1 as AnnotLayer).GetUpdate(ref cacheHitTestAnnot);
  1043. if (Update)
  1044. {
  1045. if (IsDrawEditAnnot)
  1046. {
  1047. SetEditAnnotObject();
  1048. DrawEditAnnotLayer();
  1049. }
  1050. if (isDrawSelectRect)
  1051. {
  1052. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  1053. {
  1054. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  1055. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  1056. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  1057. };
  1058. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  1059. {
  1060. SetEditAnnotObject();
  1061. DrawEditAnnotLayer();
  1062. }
  1063. else
  1064. {
  1065. SelectedAnnot();
  1066. }
  1067. DrawSelectedLayer();
  1068. }
  1069. }
  1070. else
  1071. {
  1072. SelectedAnnot(null);
  1073. CleanEditAnnot(true);
  1074. }
  1075. }
  1076. else if (selectedPageIndex != -1 && selectedAnnotIndex != -1)
  1077. {
  1078. BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
  1079. cacheHitTestAnnot = (baseLayer1 as AnnotLayer).GetSelectedAnnot(ref selectedPageIndex, ref selectedAnnotIndex);
  1080. if (cacheHitTestAnnot != null)
  1081. {
  1082. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  1083. {
  1084. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  1085. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  1086. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  1087. };
  1088. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  1089. {
  1090. SetEditAnnotObject();
  1091. DrawEditAnnotLayer();
  1092. }
  1093. else
  1094. {
  1095. SelectedAnnot();
  1096. }
  1097. DrawSelectedLayer();
  1098. isDrawSelectRect = true;
  1099. }
  1100. }
  1101. if (PDFViewer.CurrentRenderFrame != null)
  1102. {
  1103. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  1104. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  1105. {
  1106. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  1107. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  1108. {
  1109. DrawSelectedEditAreaForIndex();
  1110. }
  1111. }
  1112. }
  1113. ReDrawSelectedMultiRect();
  1114. ReDrawWidget();
  1115. ReDrawSelectText();
  1116. ReDrawSelectImage();
  1117. UpdateFormHitPop();
  1118. UpdateTextPop();
  1119. }
  1120. }
  1121. private void ScrollViewer_MouseUp(object sender, MouseButtonEventArgs e)
  1122. {
  1123. }
  1124. private void ScrollViewer_MouseDown(object sender, MouseButtonEventArgs e)
  1125. {
  1126. }
  1127. private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
  1128. {
  1129. ScrollChangedHandler?.Invoke(this, e);
  1130. }
  1131. }
  1132. }