CPDFViewerTool.xaml.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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 class PopupControlInfo
  66. {
  67. public BaseAnnot Annot { get; set; }
  68. public UIElement PopUpControl { get; set; }
  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. }
  100. public void ReplaceCPDFViewer(CPDFViewer newViewer)
  101. {
  102. ScrollContainer.Content = newViewer;
  103. PDFViewer = newViewer;
  104. InsertSelectImageView();
  105. InsertAnnotView();
  106. InsertAnnotEditView();
  107. InsertWidgetView();
  108. InsertSelectedRectView();
  109. InsertMultiSelectedRectView();
  110. InsertCustomizeToolView();
  111. InsertSelectTextView();
  112. InsertTextEditView();
  113. InsertPageSelectedRectView();
  114. }
  115. private void Current_Exit(object sender, ExitEventArgs e)
  116. {
  117. GetCPDFViewer().Dispose();
  118. }
  119. protected override Visual GetVisualChild(int index)
  120. {
  121. return base.GetVisualChild(index);
  122. }
  123. protected override int VisualChildrenCount => base.VisualChildrenCount;
  124. public event EventHandler<MouseEventObject> MouseLeftButtonDownHandler;
  125. public event EventHandler<MouseEventObject> MouseLeftButtonUpHandler;
  126. public event EventHandler<MouseEventObject> MouseMoveHandler;
  127. public event EventHandler<MouseEventObject> MouseRightButtonDownHandler;
  128. public event EventHandler<ScrollChangedEventArgs> ScrollChangedHandler;
  129. public event EventHandler DrawChanged;
  130. public event EventHandler DocumentModifiedChanged;
  131. public event EventHandler<PopupControlInfo> PopUpControlHandler;
  132. private ToolType currentModel = ToolType.Viewer;
  133. DefaultSettingParam defaultSettingParam = new DefaultSettingParam();
  134. DefaultDrawParam defaultDrawParam = new DefaultDrawParam();
  135. MeasureSetting measureSetting = new MeasureSetting();
  136. Point Point = new Point();
  137. Point CachePoint = new Point();
  138. private bool isMultiSelected;
  139. private bool isDocumentModified = false;
  140. public bool CanAddTextEdit = true;
  141. protected bool isContinueCreateTextEdit = false;
  142. public bool GetIsMultiSelected()
  143. {
  144. return isMultiSelected;
  145. }
  146. /// <summary>
  147. /// Set whether continuous text editing is required
  148. /// </summary>
  149. /// <param name="isContinueCreateTextEdit"></param>
  150. public void SetContinueCreateTextEdit(bool isContinueCreateTextEdit)
  151. {
  152. this.isContinueCreateTextEdit = isContinueCreateTextEdit;
  153. CanAddTextEdit = true;
  154. }
  155. /// <summary>
  156. /// Does it support multiple selection
  157. /// </summary>
  158. /// <param name="isMulti">true Can MultiSelected</param>
  159. public void SetIsMultiSelected(bool isMulti)
  160. {
  161. isMultiSelected = isMulti;
  162. }
  163. public DefaultSettingParam GetDefaultSettingParam()
  164. {
  165. return defaultSettingParam;
  166. }
  167. public DefaultDrawParam GetDefaultDrawParam()
  168. {
  169. return defaultDrawParam;
  170. }
  171. /// <summary>
  172. /// Set default painting parameters
  173. /// </summary>
  174. /// <param name="defaultDrawParam"></param>
  175. public void SetDefaultDrawParam(DefaultDrawParam defaultDrawParam = null)
  176. {
  177. if (defaultDrawParam == null)
  178. {
  179. this.defaultDrawParam = new DefaultDrawParam();
  180. }
  181. else
  182. {
  183. this.defaultDrawParam = defaultDrawParam;
  184. }
  185. }
  186. public MeasureSetting GetMeasureSetting()
  187. {
  188. return measureSetting;
  189. }
  190. public bool IsSelectRectMousePoint()
  191. {
  192. if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
  193. {
  194. return true;
  195. }
  196. return false;
  197. }
  198. private void LinkAnnotAction(BaseAnnot annot)
  199. {
  200. AnnotData data = annot.GetAnnotData();
  201. CPDFLinkAnnotation linkAnnot = data.Annot as CPDFLinkAnnotation;
  202. CPDFAction action = linkAnnot.GetLinkAction();
  203. if (action != null)
  204. {
  205. ActionProcess(action);
  206. }
  207. else
  208. {
  209. CPDFDestination dest = linkAnnot.GetDestination(PDFViewer.GetDocument());
  210. if (dest != null)
  211. {
  212. CPDFGoToAction gotoAction = new CPDFGoToAction();
  213. gotoAction.SetDestination(PDFViewer.GetDocument(), dest);
  214. ActionProcess(gotoAction);
  215. }
  216. }
  217. }
  218. public void ActionProcess(CPDFAction action)
  219. {
  220. if (action == null)
  221. {
  222. return;
  223. }
  224. switch (action.ActionType)
  225. {
  226. case C_ACTION_TYPE.ACTION_TYPE_NAMED:
  227. {
  228. CPDFNamedAction namedAction = action as CPDFNamedAction;
  229. string namedStr = namedAction.GetName();
  230. switch (namedStr)
  231. {
  232. case "FirstPage":
  233. {
  234. PDFViewer?.GoToPage(0, new Point(0, 0));
  235. break;
  236. }
  237. case "LastPage":
  238. {
  239. PDFViewer?.GoToPage(PDFViewer.GetDocument().PageCount - 1, new Point(0, 0));
  240. break;
  241. }
  242. case "NextPage":
  243. if (PDFViewer != null)
  244. {
  245. int nextIndex = PDFViewer.CurrentRenderFrame.PageIndex + 1;
  246. if (nextIndex < PDFViewer.GetDocument().PageCount)
  247. {
  248. PDFViewer.GoToPage(nextIndex, new Point(0, 0));
  249. }
  250. }
  251. break;
  252. case "PrevPage":
  253. if (PDFViewer != null)
  254. {
  255. int prevIndex = PDFViewer.CurrentRenderFrame.PageIndex - 1;
  256. if (prevIndex >= 0)
  257. {
  258. PDFViewer.GoToPage(prevIndex, new Point(0, 0));
  259. }
  260. }
  261. break;
  262. default:
  263. break;
  264. }
  265. break;
  266. }
  267. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  268. if (PDFViewer != null)
  269. {
  270. CPDFGoToAction gotoAction = action as CPDFGoToAction;
  271. CPDFDestination dest = gotoAction.GetDestination(PDFViewer.GetDocument());
  272. if (dest != null)
  273. {
  274. Size pageSize = DataConversionForWPF.CSizeConversionForSize(PDFViewer.GetDocument().GetPageSize(dest.PageIndex));
  275. PDFViewer.GoToPage(dest.PageIndex, new Point(dest.Position_X, pageSize.Height - dest.Position_Y));
  276. }
  277. }
  278. break;
  279. case C_ACTION_TYPE.ACTION_TYPE_GOTOR:
  280. if (PDFViewer != null)
  281. {
  282. CPDFGoToRAction gotorAction = action as CPDFGoToRAction;
  283. CPDFDestination dest = gotorAction.GetDestination(PDFViewer.GetDocument());
  284. if (dest != null)
  285. {
  286. Size pageSize = DataConversionForWPF.CSizeConversionForSize(PDFViewer.GetDocument().GetPageSize(dest.PageIndex));
  287. PDFViewer.GoToPage(dest.PageIndex, new Point(dest.Position_X, pageSize.Height - dest.Position_Y));
  288. }
  289. }
  290. break;
  291. case C_ACTION_TYPE.ACTION_TYPE_URI:
  292. {
  293. CPDFUriAction uriAction = action as CPDFUriAction;
  294. string uri = uriAction.GetUri();
  295. try
  296. {
  297. if (!string.IsNullOrEmpty(uri))
  298. {
  299. Process.Start(uri);
  300. }
  301. }
  302. catch (Exception ex)
  303. {
  304. }
  305. }
  306. break;
  307. default:
  308. break;
  309. }
  310. }
  311. protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
  312. {
  313. if (isContinueCreateTextEdit)
  314. {
  315. if (lastSelectedRect != null)
  316. {
  317. CanAddTextEdit = false;
  318. }
  319. else
  320. {
  321. CanAddTextEdit = true;
  322. }
  323. }
  324. if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null)
  325. {
  326. return;
  327. }
  328. if (!HitTestBorder())
  329. {
  330. RemovePopTextUI();
  331. }
  332. Focus();
  333. Mouse.Capture(this, CaptureMode.SubTree);
  334. MouseEventObject mouseEventObject = new MouseEventObject
  335. {
  336. mouseButtonEventArgs = e,
  337. hitTestType = MouseHitTestType.Unknown,
  338. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  339. IsCreate = false
  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 (currentModel == ToolType.WidgetEdit)
  571. {
  572. bool areaSelectAdd = AnnotSelectAreaSelect(true);
  573. if (AnnotWidgetHitTest())
  574. {
  575. BaseWidget checkItem = PDFViewer?.AnnotHitTest() as BaseWidget;
  576. AnnotSelectAddItem(checkItem?.GetAnnotData(), !AnnotSelectAreaHitTest());
  577. }
  578. if (AnnotSelectGetCount() > 1)
  579. {
  580. CleanSelectedRect();
  581. }
  582. if (IsMoved == false && AnnotSelectAreaHitTest() == false && AnnotWidgetHitTest() == false)
  583. {
  584. AnnotSelectClean();
  585. }
  586. if (AnnotSelectGetCount() == 1)
  587. {
  588. cacheHitTestAnnot = AnnotSelectGetAnnot();
  589. if (cacheHitTestAnnot != null)
  590. {
  591. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  592. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  593. if (selectedRect != null)
  594. {
  595. if (IsMoved && areaSelectAdd == false)
  596. {
  597. selectedRect.UpdateAnnotData(cacheHitTestAnnot.GetAnnotData());
  598. }
  599. else
  600. {
  601. selectedRect.SetAnnotData(cacheHitTestAnnot.GetAnnotData());
  602. }
  603. selectedRect.Draw();
  604. }
  605. isDrawSelectRect = true;
  606. }
  607. }
  608. }
  609. MouseEventObject mouseEventObject = new MouseEventObject
  610. {
  611. mouseButtonEventArgs = e,
  612. hitTestType = MouseHitTestType.Unknown,
  613. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  614. IsCreate = false
  615. };
  616. if (isDrawSelectRect || IsDrawEditAnnot)
  617. {
  618. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  619. if (cacheHitTestAnnot != null)
  620. {
  621. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  622. }
  623. else
  624. {
  625. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  626. }
  627. }
  628. MouseLeftButtonUpHandler?.Invoke(this, mouseEventObject);
  629. ReleaseMouseCapture();
  630. if (currentModel == ToolType.WidgetEdit)
  631. {
  632. AnnotSelectUpdate();
  633. AnnotSelectDraw();
  634. AnnotSelectSave();
  635. AreaMoveData = new AnnotSelectAreaData();
  636. }
  637. IsMoved = false;
  638. }
  639. protected override void OnMouseMove(MouseEventArgs e)
  640. {
  641. IsMoved = e.LeftButton == MouseButtonState.Pressed;
  642. if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null)
  643. {
  644. return;
  645. }
  646. Cursor oldCursor = this.Cursor;
  647. Cursor newCursor = this.Cursor;
  648. MouseEventObject mouseEventObject = new MouseEventObject
  649. {
  650. mouseButtonEventArgs = e,
  651. hitTestType = MouseHitTestType.Unknown,
  652. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  653. IsCreate = false
  654. };
  655. if (Mouse.LeftButton != MouseButtonState.Pressed)
  656. {
  657. List<ToolType> allowModeList = new List<ToolType>()
  658. {
  659. ToolType.Pan,
  660. ToolType.Viewer
  661. };
  662. if (allowModeList.Contains(currentModel))
  663. {
  664. newCursor = Cursors.Arrow;
  665. if (caheMoveAnnot is BaseWidget)
  666. {
  667. BaseWidget widget = (BaseWidget)caheMoveAnnot;
  668. if (widget.GetFormType() == PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON && PDFViewer != null)
  669. {
  670. newCursor = Cursors.Hand;
  671. }
  672. }
  673. if (caheMoveAnnot is LinkAnnot)
  674. {
  675. newCursor = Cursors.Hand;
  676. }
  677. }
  678. }
  679. if (!isDrawSelectRect && !IsDrawEditAnnot)
  680. {
  681. if (AnnotMoveHitTest())
  682. {
  683. if (isCacheRedaction)
  684. {
  685. (caheMoveAnnot as RedactionAnnot).SetIsMouseHover(true);
  686. (caheMoveAnnot as RedactionAnnot).Draw();
  687. }
  688. mouseEventObject.annotType = caheMoveAnnot.GetAnnotData().AnnotType;
  689. mouseEventObject.IsMersured = caheMoveAnnot.GetAnnotData().Annot.IsMeasured();
  690. }
  691. else
  692. {
  693. if (isCacheRedaction)
  694. {
  695. isCacheRedaction = false;
  696. (caheMoveAnnot as RedactionAnnot).SetIsMouseHover(false);
  697. (caheMoveAnnot as RedactionAnnot).Draw();
  698. }
  699. caheMoveAnnot = null;
  700. if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer))
  701. {
  702. if (GetTextSelectInfo() == null)
  703. {
  704. DrawMoveSelectImage();
  705. }
  706. }
  707. }
  708. }
  709. else
  710. {
  711. if (AnnotMoveHitTest())
  712. {
  713. if (DrawSelectRectDownEvent() == false && Mouse.LeftButton != MouseButtonState.Pressed)
  714. {
  715. mouseEventObject.annotType = caheMoveAnnot.GetAnnotData().AnnotType;
  716. }
  717. if (isCacheRedaction)
  718. {
  719. (caheMoveAnnot as RedactionAnnot)?.SetIsMouseHover(true);
  720. (caheMoveAnnot as RedactionAnnot)?.Draw();
  721. }
  722. }
  723. else
  724. {
  725. if (isCacheRedaction)
  726. {
  727. (caheMoveAnnot as RedactionAnnot)?.SetIsMouseHover(false);
  728. (caheMoveAnnot as RedactionAnnot)?.Draw();
  729. }
  730. }
  731. if (mouseEventObject.annotType == C_ANNOTATION_TYPE.C_ANNOTATION_NONE)
  732. {
  733. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  734. if (cacheHitTestAnnot != null)
  735. {
  736. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  737. mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
  738. }
  739. else
  740. {
  741. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  742. }
  743. }
  744. }
  745. if (currentModel == ToolType.WidgetEdit)
  746. {
  747. AnnotSelectAreaDraw();
  748. if (!AnnotSelectMoveDraw())
  749. {
  750. AnnotSelectAreaHover();
  751. }
  752. }
  753. MouseMoveHandler?.Invoke(this, mouseEventObject);
  754. PDFViewer.SetCustomMousePoint(Mouse.GetPosition(this).Y, Mouse.GetPosition(this).X);
  755. if (oldCursor != newCursor)
  756. {
  757. this.Cursor = newCursor;
  758. }
  759. }
  760. protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
  761. {
  762. MouseEventObject mouseEventObject = new MouseEventObject
  763. {
  764. mouseButtonEventArgs = e,
  765. hitTestType = MouseHitTestType.Unknown,
  766. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  767. IsCreate = false
  768. };
  769. if (currentModel == ToolType.Pan || currentModel == ToolType.Viewer)
  770. {
  771. if (GetMousePointToTextSelectInfo())
  772. {
  773. mouseEventObject.hitTestType = MouseHitTestType.Text;
  774. }
  775. }
  776. if (isDrawSelectRect)
  777. {
  778. // Click inside the selected rectangle area
  779. if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
  780. {
  781. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  782. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  783. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  784. return;
  785. }
  786. }
  787. if (IsDrawEditAnnot)
  788. {
  789. // Click inside the selected rectangle area
  790. if (DrawEditAnnotDownEvent() && cacheHitTestAnnot != null)
  791. {
  792. mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
  793. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  794. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  795. return;
  796. }
  797. }
  798. Point = e.GetPosition(this);
  799. // Annotation selection effect
  800. if ((currentModel == ToolType.Pan || currentModel == ToolType.CreateAnnot))
  801. {
  802. if (AnnotHitTest())
  803. {
  804. if (!isCacheRedaction)
  805. {
  806. if (cacheHitTestAnnot?.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK && currentModel != ToolType.CreateAnnot)
  807. {
  808. LinkAnnotAction(cacheHitTestAnnot);
  809. }
  810. else
  811. {
  812. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  813. {
  814. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  815. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  816. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  817. };
  818. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  819. {
  820. SetEditAnnotObject();
  821. DrawEditAnnotLayer();
  822. }
  823. else
  824. {
  825. SelectedAnnot();
  826. DrawSelectedLayer();
  827. }
  828. isDrawSelectRect = true;
  829. }
  830. }
  831. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  832. if (cacheHitTestAnnot != null)
  833. {
  834. mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
  835. }
  836. else
  837. {
  838. mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  839. }
  840. }
  841. else
  842. {
  843. CleanSelectedRect();
  844. }
  845. }
  846. // Form selection effect
  847. if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer) && AnnotWidgetHitTest())
  848. {
  849. mouseEventObject.hitTestType = MouseHitTestType.Widget;
  850. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  851. FormClickProcess();
  852. }
  853. // Form creation mode
  854. if (currentModel == ToolType.WidgetEdit)
  855. {
  856. if (!AnnotSelectAreaHitTest())
  857. {
  858. if (AnnotWidgetHitTest())
  859. {
  860. cacheHitTestAnnot = PDFViewer?.AnnotHitTest() as BaseWidget;
  861. SelectedAnnot();
  862. DrawSelectedLayer();
  863. mouseEventObject.hitTestType = MouseHitTestType.Annot;
  864. mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
  865. AnnotSelectClean();
  866. AnnotSelectAddItem(cacheHitTestAnnot?.GetAnnotData());
  867. }
  868. else
  869. {
  870. CleanSelectedRect();
  871. }
  872. }
  873. else
  874. {
  875. mouseEventObject.hitTestType = MouseHitTestType.MultiSelector;
  876. }
  877. }
  878. // Content editing mode
  879. if (currentModel == ToolType.ContentEdit)
  880. {
  881. if (e.ClickCount == 1)
  882. {
  883. DrawTextEditDownEvent(false);
  884. if (GetLastSelectedRect() != null)
  885. {
  886. EditAreaObject editAreaObject = GetEditAreaObjectForRect(lastSelectedRect);
  887. switch (editAreaObject.cPDFEditArea.Type)
  888. {
  889. case CPDFEditType.EditText:
  890. mouseEventObject.hitTestType = MouseHitTestType.TextEdit;
  891. break;
  892. case CPDFEditType.EditImage:
  893. mouseEventObject.hitTestType = MouseHitTestType.ImageEdit;
  894. break;
  895. default:
  896. break;
  897. }
  898. }
  899. }
  900. }
  901. else
  902. {
  903. if ((currentModel == ToolType.Viewer || currentModel == ToolType.Pan) && mouseEventObject.hitTestType == MouseHitTestType.Unknown && DrawDownSelectImage(false))
  904. {
  905. mouseEventObject.hitTestType = MouseHitTestType.ImageSelect;
  906. }
  907. }
  908. MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
  909. }
  910. public bool GetIsCropMode()
  911. {
  912. if (lastSelectedRect != null)
  913. {
  914. return lastSelectedRect.GetCurrentDrawPointType() == DrawPointType.Crop;
  915. }
  916. return false;
  917. }
  918. public void SetCropMode(bool crop)
  919. {
  920. if (lastSelectedRect != null)
  921. {
  922. List<PointControlType> ignoreList = new List<PointControlType>();
  923. if (crop)
  924. {
  925. lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Crop);
  926. ignoreList.Add(PointControlType.Body);
  927. // Initialize ClipRect
  928. ClipThickness = new Thickness(0, 0, 0, 0);
  929. if (editArea.TryGetValue(lastSelectedRect, out EditAreaObject editAreaObject))
  930. {
  931. cropIndex = editAreaObject.EditAreaIndex;
  932. }
  933. lastSelectedRect.DataChanged -= LastSelectedRect_DataChanged;
  934. }
  935. else
  936. {
  937. lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Square);
  938. cropIndex = -1;
  939. lastSelectedRect.DataChanged += LastSelectedRect_DataChanged;
  940. }
  941. lastSelectedRect.SetIgnorePoints(ignoreList);
  942. lastSelectedRect.Draw();
  943. }
  944. }
  945. internal void SetToolType(ToolType model)
  946. {
  947. currentModel = model;
  948. CPDFViewer pdfViewer = GetCPDFViewer();
  949. if (pdfViewer != null)
  950. {
  951. if (currentModel == ToolType.WidgetEdit)
  952. {
  953. pdfViewer.IsHideFormShow = true;
  954. }
  955. else
  956. {
  957. pdfViewer.IsHideFormShow = false;
  958. }
  959. }
  960. }
  961. public ToolType GetToolType()
  962. {
  963. return currentModel;
  964. }
  965. public void SavePoint()
  966. {
  967. CachePoint = Point;
  968. }
  969. public void CleanPoint()
  970. {
  971. CachePoint = new Point();
  972. }
  973. private void CPDFViewerTool_Loaded(object sender, RoutedEventArgs e)
  974. {
  975. PDFViewer.DrawChanged += PDFViewer_DrawChanged;
  976. PDFViewer.UndoManager.HistoryChanged += UndoManager_HistoryChanged;
  977. PDFViewer.MouseEnter += PDFViewer_MouseEnter;
  978. PDFViewer.MouseLeave += PDFViewer_MouseLeave;
  979. }
  980. private void PDFViewer_MouseLeave(object sender, MouseEventArgs e)
  981. {
  982. PDFViewer.IsVisibilityMouse(false);
  983. }
  984. private void PDFViewer_MouseEnter(object sender, MouseEventArgs e)
  985. {
  986. PDFViewer.IsVisibilityMouse(true);
  987. }
  988. private void UndoManager_HistoryChanged(object sender, KeyValuePair<UndoAction, IHistory> data)
  989. {
  990. IsDocumentModified = true;
  991. }
  992. private void CPDFViewerTool_Unloaded(object sender, RoutedEventArgs e)
  993. {
  994. PDFViewer.DrawChanged -= PDFViewer_DrawChanged;
  995. }
  996. private void PDFViewer_DrawChanged(object sender, EventArgs e)
  997. {
  998. SizeChangeds();
  999. AnnotSelectUpdate();
  1000. AnnotSelectDraw();
  1001. DrawChanged?.Invoke(this, e);
  1002. }
  1003. public void SizeChangeds()
  1004. {
  1005. if (IsLoaded)
  1006. {
  1007. if (cacheHitTestAnnot != null)
  1008. {
  1009. BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
  1010. bool Update = (baseLayer1 as AnnotLayer).GetUpdate(ref cacheHitTestAnnot);
  1011. if (Update)
  1012. {
  1013. if (IsDrawEditAnnot)
  1014. {
  1015. SetEditAnnotObject();
  1016. DrawEditAnnotLayer();
  1017. }
  1018. if (isDrawSelectRect)
  1019. {
  1020. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  1021. {
  1022. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  1023. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  1024. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  1025. };
  1026. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  1027. {
  1028. SetEditAnnotObject();
  1029. DrawEditAnnotLayer();
  1030. }
  1031. else
  1032. {
  1033. SelectedAnnot();
  1034. }
  1035. DrawSelectedLayer();
  1036. }
  1037. }
  1038. else
  1039. {
  1040. SelectedAnnot(null);
  1041. }
  1042. }
  1043. else if (selectedPageIndex != -1 && selectedAnnotIndex != -1)
  1044. {
  1045. BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
  1046. cacheHitTestAnnot = (baseLayer1 as AnnotLayer).GetSelectedAnnot(ref selectedPageIndex, ref selectedAnnotIndex);
  1047. if (cacheHitTestAnnot != null)
  1048. {
  1049. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  1050. {
  1051. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  1052. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  1053. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  1054. };
  1055. if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
  1056. {
  1057. SetEditAnnotObject();
  1058. DrawEditAnnotLayer();
  1059. }
  1060. else
  1061. {
  1062. SelectedAnnot();
  1063. }
  1064. DrawSelectedLayer();
  1065. isDrawSelectRect = true;
  1066. }
  1067. }
  1068. if (PDFViewer.CurrentRenderFrame != null)
  1069. {
  1070. currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
  1071. if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
  1072. {
  1073. SetEditTextRect(PDFViewer.CurrentRenderFrame);
  1074. if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
  1075. {
  1076. DrawSelectedEditAreaForIndex();
  1077. }
  1078. }
  1079. }
  1080. ReDrawSelectedMultiRect();
  1081. ReDrawWidget();
  1082. ReDrawSelectText();
  1083. ReDrawSelectImage();
  1084. UpdateFormHitPop();
  1085. UpdateTextPop();
  1086. }
  1087. }
  1088. private void ScrollViewer_MouseUp(object sender, MouseButtonEventArgs e)
  1089. {
  1090. }
  1091. private void ScrollViewer_MouseDown(object sender, MouseButtonEventArgs e)
  1092. {
  1093. }
  1094. private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
  1095. {
  1096. ScrollChangedHandler?.Invoke(this, e);
  1097. }
  1098. }
  1099. }