CPDFToolManager.cs 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.Tool.DrawTool;
  4. using ComPDFKitViewer.Widget;
  5. using ComPDFKitViewer.Helper;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Windows;
  10. using System.Windows.Input;
  11. using ComPDFKit.Tool.UndoManger;
  12. using ComPDFKit.PDFAnnotation.Form;
  13. using ComPDFKitViewer.BaseObject;
  14. using ComPDFKit.Tool.SettingParam;
  15. using ComPDFKit.PDFDocument;
  16. using ComPDFKit.PDFPage;
  17. using ComPDFKit.Viewer.Helper;
  18. using ComPDFKit.PDFPage.Edit;
  19. using ComPDFKit.Tool.Help;
  20. using ComPDFKit.Measure;
  21. using System.Dynamic;
  22. using System.Globalization;
  23. using ComPDFKitViewer.Layer;
  24. using ComPDFKitViewer;
  25. using ComPDFKitViewer.Annot;
  26. namespace ComPDFKit.Tool
  27. {
  28. public class CPDFToolManager
  29. {
  30. private CPDFViewerTool viewerTool;
  31. public event EventHandler<MouseEventObject> MouseLeftButtonDownHandler;
  32. public event EventHandler<MouseEventObject> MouseLeftButtonUpHandler;
  33. public event EventHandler<MouseEventObject> MouseMoveHandler;
  34. public event EventHandler<MouseEventObject> MouseRightButtonDownHandler;
  35. public event EventHandler<SelectedAnnotData> AnnotDefaultEditedHandler;
  36. public event EventHandler<MultiSelectedData> MulitDefaultEditedHandler;
  37. /// <summary>
  38. /// Current tool type, pay attention to the use of if writing, reserve the subsequent or operation mode switch
  39. /// </summary>
  40. private ToolType currentToolType = ToolType.None;
  41. /// <summary>
  42. /// Identify whether it is a selected text state or a drag box state for content editing
  43. /// </summary>
  44. private bool editSelected = true;
  45. private bool clickEditSelected = false;
  46. /// <summary>
  47. /// Current creation annotation type
  48. /// </summary>
  49. private C_ANNOTATION_TYPE createAnnotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  50. /// <summary>
  51. /// Current creation Widget type
  52. /// </summary>
  53. private C_WIDGET_TYPE createWidgetType = C_WIDGET_TYPE.WIDGET_NONE;
  54. /// <summary>
  55. /// Current creation content edit type
  56. /// </summary>
  57. private CPDFEditType createContentEditType = CPDFEditType.None;
  58. /// <summary>
  59. /// Current creation data layer annotation object (effective after Down, invalid after Up, if you need to modify the creation attributes, you need to do this)
  60. /// </summary>
  61. private CPDFAnnotation cPDFAnnotation;
  62. /// <summary>
  63. /// Image path cache used for image creation (subject to possible adjustments later)
  64. /// </summary>
  65. private string createImagePath = string.Empty;
  66. private bool isActiveCropping = true;
  67. /// <summary>
  68. /// add TextEdit Cursor
  69. /// </summary>
  70. private Cursor addTextEditCursor = Cursors.IBeam;
  71. /// <summary>
  72. /// add ImageEdit Cursor
  73. /// </summary>
  74. private Cursor addImageEditCursor = Cursors.Arrow;
  75. public bool SaveEmptyStickyAnnot { get; set; } = true;
  76. public void SetActiveCropping(bool isActiveCropping)
  77. {
  78. this.isActiveCropping = isActiveCropping;
  79. }
  80. public CPDFToolManager(CPDFViewerTool cPDFTool) : base()
  81. {
  82. viewerTool = cPDFTool;
  83. viewerTool.MouseLeftButtonDownHandler += ViewerTool_MouseLeftButtonDownHandler;
  84. viewerTool.MouseMoveHandler += ViewerTool_MouseMoveHandler;
  85. viewerTool.MouseLeftButtonUpHandler += ViewerTool_MouseLeftButtonUpHandler;
  86. viewerTool.MouseRightButtonDownHandler += ViewerTool_MouseRightButtonDownHandler;
  87. viewerTool.SelectedDataChanged += ViewerTool_SelectedDataChanged;
  88. viewerTool.SelectedDataChanging += ViewerTool_SelectedDataChanging;
  89. viewerTool.MultiDataChanged += ViewerTool_MulitDataChanged;
  90. viewerTool.MultiDataChanging += ViewerTool_MulitDataChanging;
  91. viewerTool.DeleteChanged += ViewerTool_DeleteChanged;
  92. viewerTool.AnnotEditDataChanged += ViewerTool_AnnotEditDataChanged;
  93. viewerTool.AnnotEditDataChanging += ViewerTool_AnnotEditDataChanging;
  94. }
  95. public void Remove()
  96. {
  97. viewerTool.MouseLeftButtonDownHandler -= ViewerTool_MouseLeftButtonDownHandler;
  98. viewerTool.MouseMoveHandler -= ViewerTool_MouseMoveHandler;
  99. viewerTool.MouseLeftButtonUpHandler -= ViewerTool_MouseLeftButtonUpHandler;
  100. viewerTool.SelectedDataChanged -= ViewerTool_SelectedDataChanged;
  101. viewerTool.SelectedDataChanging -= ViewerTool_SelectedDataChanging;
  102. viewerTool.MultiDataChanged -= ViewerTool_MulitDataChanged;
  103. viewerTool.MultiDataChanging -= ViewerTool_MulitDataChanging;
  104. viewerTool.DeleteChanged -= ViewerTool_DeleteChanged;
  105. viewerTool.AnnotEditDataChanged -= ViewerTool_AnnotEditDataChanged;
  106. viewerTool.AnnotEditDataChanging -= ViewerTool_AnnotEditDataChanging;
  107. viewerTool = null;
  108. }
  109. public BaseAnnot GetCacheHitTestAnnot()
  110. {
  111. return viewerTool.GetCacheHitTestAnnot();
  112. }
  113. public BaseWidget GetCacheHitTestWidget()
  114. {
  115. return viewerTool.GetCacheHitTestWidget();
  116. }
  117. public CPDFViewerTool GetCPDFViewerTool()
  118. {
  119. return viewerTool;
  120. }
  121. public CPDFDocument GetDocument()
  122. {
  123. return viewerTool.GetCPDFViewer().GetDocument();
  124. }
  125. public void SetCreateImagePath(string path)
  126. {
  127. createImagePath = path;
  128. }
  129. public ToolType GetToolType()
  130. {
  131. return currentToolType;
  132. }
  133. public bool SetCreateAnnotType(C_ANNOTATION_TYPE annotType)
  134. {
  135. viewerTool.SetIsHitTestLink(false);
  136. viewerTool.SetIsOnlyHitTestRedact(false);
  137. if (currentToolType != ToolType.CreateAnnot)
  138. {
  139. return false;
  140. }
  141. //MouseEventObject e= new MouseEventObject();
  142. //if (viewerTool.IsCanSave())
  143. //{
  144. // SaveCreateAnnotation(ref cPDFAnnotation, ref e);
  145. // viewerTool.PDFViewer.EnableZoom(true);
  146. // viewerTool.PDFViewer.CanHorizontallyScroll = true;
  147. // viewerTool.PDFViewer.CanVerticallyScroll = true;
  148. //}
  149. if (createAnnotType != annotType)
  150. {
  151. SaveCurrentAnnot();
  152. createAnnotType = annotType;
  153. }
  154. if (createAnnotType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK)
  155. {
  156. viewerTool.SetIsHitTestLink(true);
  157. }
  158. if (createAnnotType == C_ANNOTATION_TYPE.C_ANNOTATION_REDACT)
  159. {
  160. viewerTool.SetIsOnlyHitTestRedact(true);
  161. }
  162. switch (annotType)
  163. {
  164. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  165. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  166. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  167. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  168. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  169. TextSelectInfo textSelectInfo = viewerTool.GetTextSelectInfo();
  170. if (textSelectInfo.PageSelectPointList.Count > 0)
  171. {
  172. GroupHistory historyData = null;
  173. viewerTool.CreateAnnotForSelectText(textSelectInfo, createAnnotType, out historyData);
  174. viewerTool.RemoveSelectTextData();
  175. if (historyData != null && historyData.Histories.Count > 0)
  176. {
  177. List<object> dataList = new List<object>();
  178. if (historyData != null && historyData.Histories.Count > 0)
  179. {
  180. foreach (IHistory historyItem in historyData.Histories)
  181. {
  182. AnnotHistory checkHistory = historyItem as AnnotHistory;
  183. if (checkHistory == null || checkHistory.CurrentParam == null)
  184. {
  185. continue;
  186. }
  187. dynamic expandData = new ExpandoObject();
  188. expandData.AnnotIndex = checkHistory.CurrentParam.AnnotIndex;
  189. expandData.PageIndex = checkHistory.CurrentParam.PageIndex;
  190. expandData.AnnotParam = checkHistory.CurrentParam;
  191. dataList.Add(expandData);
  192. }
  193. }
  194. if (dataList.Count > 0)
  195. {
  196. MouseEventObject eventObject = new MouseEventObject();
  197. eventObject.annotType = createAnnotType;
  198. eventObject.IsCreate = true;
  199. eventObject.Data = dataList.Count > 1 ? dataList : dataList[0];
  200. MouseLeftButtonUpHandler?.Invoke(this, eventObject);
  201. }
  202. }
  203. }
  204. break;
  205. default:
  206. break;
  207. }
  208. return true;
  209. }
  210. public C_ANNOTATION_TYPE GetAnnotType()
  211. {
  212. return createAnnotType;
  213. }
  214. public bool SetCreateWidgetType(C_WIDGET_TYPE widgetType)
  215. {
  216. if (currentToolType != ToolType.WidgetEdit)
  217. {
  218. return false;
  219. }
  220. if (createWidgetType != widgetType)
  221. {
  222. SaveCurrentAnnot();
  223. createWidgetType = widgetType;
  224. }
  225. viewerTool.SetDrawWidgetType(createWidgetType);
  226. return true;
  227. }
  228. public C_WIDGET_TYPE GetCreateWidgetType()
  229. {
  230. return createWidgetType;
  231. }
  232. public bool SetCreateContentEditType(CPDFEditType editType)
  233. {
  234. createContentEditType = editType;
  235. return true;
  236. }
  237. public CPDFEditType GetCreateContentEditType()
  238. {
  239. return createContentEditType;
  240. }
  241. public void ClearSelect()
  242. {
  243. /// Clear some UI effects of other modules
  244. viewerTool.HideWidgetHitPop();
  245. viewerTool.CleanSelectedRect();
  246. viewerTool.CleanEditAnnot();
  247. viewerTool.CleanDrawSelectImage();
  248. viewerTool.ClearDrawWidget();
  249. viewerTool.CleanEditView();
  250. viewerTool.CleanCustomizeTool();
  251. viewerTool.GetCPDFViewer().SetCacheEditPage(false);
  252. }
  253. public void SetToolType(ToolType toolType)
  254. {
  255. if (currentToolType == toolType)
  256. {
  257. return;
  258. }
  259. viewerTool.RemovePopTextUI();
  260. /// Clear some UI effects of other modules
  261. viewerTool.HideWidgetHitPop();
  262. viewerTool.CleanSelectedRect();
  263. viewerTool.CleanEditAnnot();
  264. viewerTool.ClearDrawWidget();
  265. viewerTool.CleanEditView();
  266. viewerTool.CleanCustomizeTool();
  267. viewerTool.CleanDrawSelectImage();
  268. viewerTool.GetCPDFViewer().SetCacheEditPage(false);
  269. viewerTool.ClearDrawAnnot();
  270. viewerTool.CleanPageSelectedRect();
  271. viewerTool.SelectedAnnotForIndex(-1, -1);
  272. ToolType oldToolType = currentToolType;
  273. currentToolType = toolType;
  274. switch (toolType)
  275. {
  276. case ToolType.None:
  277. break;
  278. case ToolType.Viewer:
  279. viewerTool.SetToolType(ToolType.Viewer);
  280. break;
  281. case ToolType.CreateAnnot:
  282. viewerTool.SetToolType(ToolType.CreateAnnot);
  283. break;
  284. case ToolType.Pan:
  285. viewerTool.SetToolType(ToolType.Pan);
  286. break;
  287. case ToolType.WidgetEdit:
  288. viewerTool.SetToolType(ToolType.WidgetEdit);
  289. viewerTool.RemoveSelectTextData();
  290. break;
  291. case ToolType.ContentEdit:
  292. viewerTool.SetToolType(ToolType.ContentEdit);
  293. viewerTool.GetCPDFViewer().SetCacheEditPage(true);
  294. viewerTool.RemoveSelectTextData();
  295. break;
  296. case ToolType.SelectedPage:
  297. viewerTool.SetToolType(ToolType.SelectedPage);
  298. viewerTool.RemoveSelectTextData();
  299. break;
  300. case ToolType.Customize:
  301. viewerTool.SetToolType(ToolType.Customize);
  302. viewerTool.RemoveSelectTextData();
  303. break;
  304. default:
  305. break;
  306. }
  307. if (oldToolType == ToolType.ContentEdit)
  308. {
  309. viewerTool.GetCPDFViewer().GetDocument()?.ReleasePages();
  310. //Undo delete logic
  311. viewerTool.GetCPDFViewer().UndoManager.ClearHistory();
  312. // viewerTool.GetCPDFViewer().UndoManager.RemoveRedoHistoryForType(typeof(PDFEditHistory));
  313. //viewerTool.GetCPDFViewer().UndoManager.RemoveUndoHistoryForType(typeof(PDFEditHistory));
  314. viewerTool.GetCPDFViewer().UpdateVirtualNodes();
  315. viewerTool.GetCPDFViewer().UpdateRenderFrame();
  316. }
  317. }
  318. public CPDFEditArea GetSelectedEditAreaObject(ref int pageIndex)
  319. {
  320. EditAreaObject editAreaObject = viewerTool.GetEditAreaObjectForRect(viewerTool.GetLastSelectedRect());
  321. if (editAreaObject == null)
  322. {
  323. pageIndex = -1;
  324. }
  325. else
  326. {
  327. pageIndex = editAreaObject.PageIndex;
  328. }
  329. return editAreaObject?.cPDFEditArea;
  330. }
  331. /// <summary>
  332. /// Get the index of the multi choice comment list
  333. /// </summary>
  334. /// <param name="pageIndexs"></param>
  335. /// <returns></returns>
  336. public List<CPDFEditArea> GetSelectedEditAreaListObject(ref List<int> pageIndexs)
  337. {
  338. List<CPDFEditArea> editAreaObjectlist = new List<CPDFEditArea>();
  339. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(viewerTool.PDFViewer.GetViewForTag(viewerTool.MultiSelectedRectViewTag));
  340. if (multiSelectedRect != null && multiSelectedRect.Children.Count > 0)
  341. {
  342. foreach (SelectedRect selectedRect in multiSelectedRect.Children)
  343. {
  344. EditAreaObject editAreaObject = viewerTool.GetEditAreaObjectListForRect(selectedRect);
  345. if (editAreaObject == null)
  346. {
  347. //pageIndexs.Add(-1);
  348. }
  349. else
  350. {
  351. if (!editAreaObjectlist.Contains(editAreaObject?.cPDFEditArea))
  352. {
  353. pageIndexs.Add(editAreaObject.PageIndex);
  354. editAreaObjectlist.Add(editAreaObject?.cPDFEditArea);
  355. }
  356. }
  357. }
  358. }
  359. return editAreaObjectlist;
  360. }
  361. public CPDFAnnotation GetCPDFAnnotation()
  362. {
  363. return cPDFAnnotation;
  364. }
  365. public void SetPageSelectText(List<TextSearchItem> pageTextList)
  366. {
  367. viewerTool.Dispatcher.Invoke(() =>
  368. {
  369. viewerTool.SetPageSelectText(pageTextList);
  370. viewerTool.ReDrawSelectText();
  371. });
  372. }
  373. public void HighLightSearchText(List<TextSearchItem> pageTextList)
  374. {
  375. if (pageTextList.Count < 0)
  376. {
  377. return;
  378. }
  379. viewerTool.Dispatcher.Invoke(() =>
  380. {
  381. viewerTool.HighLightSearchText(pageTextList);
  382. viewerTool.ReDrawSearchText();
  383. viewerTool.GetCPDFViewer().GoToPage(pageTextList[0].PageIndex, new Point(pageTextList[0].TextRect.X, pageTextList[0].TextRect.Y));
  384. });
  385. }
  386. private void ViewerTool_DeleteChanged(object sender, List<AnnotParam> e)
  387. {
  388. viewerTool.GetCPDFViewer().UpdateAnnotFrame();
  389. AnnotDefaultEditedHandler?.Invoke(this, null);
  390. }
  391. private void ViewerTool_SelectedDataChanging(object sender, DrawTool.SelectedAnnotData e)
  392. {
  393. }
  394. private void ViewerTool_SelectedDataChanged(object sender, DrawTool.SelectedAnnotData e)
  395. {
  396. if (e.annotData == null)
  397. {
  398. return;
  399. }
  400. AnnotHistory annotHistory = ParamConverter.CreateHistory(e.annotData.Annot);
  401. if (annotHistory == null)
  402. {
  403. return;
  404. }
  405. AnnotParam previousParam;
  406. AnnotParam currentParam;
  407. switch (e.annotData.AnnotType)
  408. {
  409. case C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET:
  410. previousParam = ParamConverter.WidgetConverter(viewerTool.GetCPDFViewer().GetDocument(), e.annotData.Annot);
  411. break;
  412. default:
  413. previousParam = ParamConverter.AnnotConverter(viewerTool.GetCPDFViewer().GetDocument(), e.annotData.Annot);
  414. break;
  415. }
  416. /// Change operation
  417. if (e.annotData.AnnotType != C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  418. {
  419. Rect rect1 = new Rect(
  420. (e.Square.Left - e.annotData.PaintOffset.X + (e.annotData.CropLeft * e.annotData.CurrentZoom)) / e.annotData.CurrentZoom,
  421. (e.Square.Top - e.annotData.PaintOffset.Y + (e.annotData.CropTop * e.annotData.CurrentZoom)) / e.annotData.CurrentZoom,
  422. e.Square.Width / e.annotData.CurrentZoom,
  423. e.Square.Height / e.annotData.CurrentZoom
  424. );
  425. Rect rect = DpiHelper.StandardRectToPDFRect(rect1);
  426. CRect cRect = new CRect((float)rect.Left, (float)rect.Bottom, (float)rect.Right, (float)rect.Top);
  427. if (e.annotData.AnnotType == C_ANNOTATION_TYPE.C_ANNOTATION_STAMP)
  428. {
  429. CPDFStampAnnotation stampAnnot = e.annotData.Annot as CPDFStampAnnotation;
  430. stampAnnot.SetSourceRect(cRect);
  431. stampAnnot.AnnotationRotator.SetRotation(-e.rotationAngle);
  432. }
  433. else
  434. {
  435. e.annotData.Annot.SetRect(cRect);
  436. }
  437. }
  438. switch (e.annotData.AnnotType)
  439. {
  440. case C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET:
  441. {
  442. currentParam = ParamConverter.WidgetConverter(viewerTool.GetCPDFViewer().GetDocument(), e.annotData.Annot);
  443. (e.annotData.Annot as CPDFWidget).UpdateFormAp();
  444. break;
  445. }
  446. default:
  447. {
  448. currentParam = ParamConverter.AnnotConverter(viewerTool.GetCPDFViewer().GetDocument(), e.annotData.Annot);
  449. if (e.annotData.AnnotType != C_ANNOTATION_TYPE.C_ANNOTATION_SOUND)
  450. {
  451. e.annotData.Annot.UpdateAp();
  452. if (e.annotData.Annot is CPDFTextAnnotation)
  453. {
  454. CommonHelper.UpdateStickyAP(e.annotData.Annot as CPDFTextAnnotation);
  455. }
  456. }
  457. break;
  458. }
  459. }
  460. annotHistory.PreviousParam = previousParam;
  461. annotHistory.PDFDoc = viewerTool.GetCPDFViewer().GetDocument();
  462. annotHistory.CurrentParam = currentParam;
  463. annotHistory.Action = HistoryAction.Update;
  464. viewerTool.GetCPDFViewer().UndoManager.AddHistory(annotHistory);
  465. viewerTool.GetCPDFViewer().UpdateAnnotFrame();
  466. AnnotDefaultEditedHandler?.Invoke(this, e);
  467. }
  468. private void ViewerTool_MulitDataChanging(object sender, MultiSelectedData e)
  469. {
  470. }
  471. private void ViewerTool_MulitDataChanged(object sender, MultiSelectedData e)
  472. {
  473. switch (e.ObjectType)
  474. {
  475. case SelectedType.None:
  476. break;
  477. case SelectedType.Annot:
  478. break;
  479. case SelectedType.PDFEdit:
  480. SaveToMulitChanged(e);
  481. break;
  482. default:
  483. break;
  484. }
  485. MulitDefaultEditedHandler?.Invoke(this, e);
  486. }
  487. private void SaveToMulitChanged(MultiSelectedData e)
  488. {
  489. GroupHistory groupHistory = new GroupHistory();
  490. CPDFDocument cPDFDocument = viewerTool.GetCPDFViewer().GetDocument();
  491. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(e.PageIndex);
  492. CPDFEditPage cPDFEditPage = cPDFPage.GetEditPage();
  493. List<CPDFEditArea> cPDFEditAreas = cPDFEditPage.GetEditAreaList(false);
  494. float zoom = (float)viewerTool.PDFViewer.GetZoom();
  495. for (int i = 0; i < e.MultiObjectIndex.Count; i++)
  496. {
  497. if (e.MultiObjectIndex[i] < cPDFEditAreas.Count)
  498. {
  499. PDFEditHistory pDFEditHistory = new PDFEditHistory();
  500. pDFEditHistory.PageIndex = e.PageIndex;
  501. pDFEditHistory.EditPage = cPDFEditPage;
  502. CRect cRect = cPDFEditAreas[e.MultiObjectIndex[i]].GetFrame();
  503. if (cPDFEditAreas[e.MultiObjectIndex[i]].Type == CPDFEditType.EditImage)
  504. {
  505. cRect = (cPDFEditAreas[e.MultiObjectIndex[i]] as CPDFEditImageArea).GetClipRect();
  506. }
  507. Point point = DpiHelper.StandardPointToPDFPoint(e.MoveOffset);
  508. point.X = point.X / zoom;
  509. point.Y = point.Y / zoom;
  510. if (point.X != 0 && point.Y != 0 && e.ZoomX == 1 && e.ZoomY == 1)
  511. {
  512. cRect.left += (float)point.X;
  513. cRect.right += (float)point.X;
  514. cRect.top += (float)point.Y;
  515. cRect.bottom += (float)point.Y;
  516. }
  517. else
  518. {
  519. //Mobile scaling ratio logic
  520. if ((float)point.X == 0)
  521. {
  522. cRect.left += (float)point.X;
  523. cRect.right = cRect.left + (cRect.right - cRect.left) + (float)DpiHelper.StandardNumToPDFNum(e.ChangeX) / zoom;
  524. }
  525. else
  526. {
  527. cRect.left += (float)point.X;
  528. if (e.ZoomX == 1)
  529. {
  530. cRect.right += (float)point.X;
  531. }
  532. }
  533. if ((float)point.Y == 0)
  534. {
  535. cRect.top += (float)point.Y;
  536. cRect.bottom = cRect.top + (cRect.bottom - cRect.top) + (float)DpiHelper.StandardNumToPDFNum(e.ChangeY) / zoom;
  537. }
  538. else
  539. {
  540. cRect.top += (float)point.Y;
  541. if (e.ZoomY == 1)
  542. {
  543. cRect.bottom += (float)point.Y;
  544. }
  545. }
  546. }
  547. //Original Logic
  548. //cRect.left += (float)point.X;
  549. //cRect.right += (float)point.X;
  550. //cRect.top += (float)point.Y;
  551. //cRect.bottom += (float)point.Y;
  552. //cRect.right = cRect.right* e.ZoomX;
  553. //cRect.bottom = cRect.bottom * e.ZoomY;
  554. cPDFEditAreas[e.MultiObjectIndex[i]].SetFrame(cRect);
  555. groupHistory.Histories.Add(pDFEditHistory);
  556. }
  557. }
  558. //Add end edit
  559. cPDFEditPage.EndEdit();
  560. viewerTool.GetCPDFViewer().UndoManager.AddHistory(groupHistory);
  561. viewerTool.GetCPDFViewer().UpdateRenderFrame();
  562. }
  563. private void ViewerTool_AnnotEditDataChanging(object sender, SelectedAnnotData e)
  564. {
  565. }
  566. private void ViewerTool_AnnotEditDataChanged(object sender, SelectedAnnotData e)
  567. {
  568. if (e.annotData == null)
  569. {
  570. return;
  571. }
  572. AnnotHistory annotHistory = ParamConverter.CreateHistory(e.annotData.Annot);
  573. if (annotHistory == null)
  574. {
  575. return;
  576. }
  577. AnnotParam previousParam;
  578. AnnotParam currentParam;
  579. switch (e.annotData.AnnotType)
  580. {
  581. case C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET:
  582. previousParam = ParamConverter.WidgetConverter(viewerTool.GetCPDFViewer().GetDocument(), e.annotData.Annot);
  583. break;
  584. default:
  585. previousParam = ParamConverter.AnnotConverter(viewerTool.GetCPDFViewer().GetDocument(), e.annotData.Annot);
  586. break;
  587. }
  588. switch (e.annotData.AnnotType)
  589. {
  590. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  591. if ((e.annotData.Annot as CPDFLineAnnotation).IsMeasured())
  592. {
  593. List<Point> cPoints = new List<Point>();
  594. for (int i = 0; i < e.Points.Count; i++)
  595. {
  596. Point cPoint = new Point((float)((e.Points[i].X - e.annotData.PaintOffset.X) / e.annotData.CurrentZoom),
  597. (float)((e.Points[i].Y - e.annotData.PaintOffset.Y) / e.annotData.CurrentZoom));
  598. cPoints.Add(cPoint);
  599. }
  600. Vector lineVector = cPoints[0] - cPoints[1];
  601. Point lineCenterPoint = new Point(
  602. (cPoints[0].X + cPoints[2].X) / 2,
  603. (cPoints[0].Y + cPoints[2].Y) / 2
  604. );
  605. Point crossCenterPoint = new Point(
  606. (cPoints[4].X + cPoints[5].X) / 2,
  607. (cPoints[4].Y + cPoints[5].Y) / 2
  608. );
  609. Rect unionRect = new Rect(cPoints[0], cPoints[2]);
  610. unionRect.Union(cPoints[1]);
  611. unionRect.Union(cPoints[3]);
  612. CPDFLineAnnotation annotLine = (e.annotData.Annot as CPDFLineAnnotation);
  613. annotLine.SetLinePoints(
  614. new CPoint(
  615. (float)(cPoints[0].X / 96D * 72D),
  616. (float)(cPoints[0].Y / 96D * 72D)),
  617. new CPoint(
  618. (float)(cPoints[2].X / 96D * 72D),
  619. (float)(cPoints[2].Y / 96D * 72D)
  620. ));
  621. annotLine.SetRect(new CRect(
  622. (float)(unionRect.Left / 96D * 72D),
  623. (float)(unionRect.Bottom / 96D * 72D),
  624. (float)(unionRect.Right / 96D * 72D),
  625. (float)(unionRect.Top / 96D * 72D)
  626. ));
  627. CPDFDistanceMeasure lineMeasure = annotLine.GetDistanceMeasure();
  628. double saveLength = lineVector.Length / 96D * 72D;
  629. if (lineCenterPoint.Y < crossCenterPoint.Y)
  630. {
  631. lineMeasure.SetLeadLength(-(float)saveLength);
  632. }
  633. if (lineCenterPoint.Y > crossCenterPoint.Y)
  634. {
  635. lineMeasure.SetLeadLength((float)saveLength);
  636. }
  637. if (lineCenterPoint.Y == crossCenterPoint.Y)
  638. {
  639. if (lineCenterPoint.X > crossCenterPoint.X)
  640. {
  641. lineMeasure.SetLeadLength(-(float)saveLength);
  642. }
  643. if (lineCenterPoint.X < crossCenterPoint.X)
  644. {
  645. lineMeasure.SetLeadLength((float)saveLength);
  646. }
  647. }
  648. annotLine.UpdateAp();
  649. lineMeasure.UpdateAnnotMeasure();
  650. PostMeasureInfo(this, annotLine);
  651. }
  652. else
  653. {
  654. Point startPoint = new Point(
  655. (float)((e.Points.First().X - e.annotData.PaintOffset.X) / e.annotData.CurrentZoom),
  656. (float)((e.Points.First().Y - e.annotData.PaintOffset.Y) / e.annotData.CurrentZoom)
  657. );
  658. Point endPoint = new Point(
  659. (float)((e.Points.Last().X - e.annotData.PaintOffset.X) / e.annotData.CurrentZoom),
  660. (float)((e.Points.Last().Y - e.annotData.PaintOffset.Y) / e.annotData.CurrentZoom)
  661. );
  662. CPoint cstartPoint = new CPoint((float)DpiHelper.StandardPointToPDFPoint(startPoint).X, (float)DpiHelper.StandardPointToPDFPoint(startPoint).Y);
  663. CPoint cendPoint = new CPoint((float)DpiHelper.StandardPointToPDFPoint(endPoint).X, (float)DpiHelper.StandardPointToPDFPoint(endPoint).Y);
  664. DpiHelper.StandardPointToPDFPoint(endPoint);
  665. CPDFLineAnnotation annotLine = (e.annotData.Annot as CPDFLineAnnotation);
  666. annotLine.SetLinePoints(cstartPoint, cendPoint);
  667. annotLine.UpdateAp();
  668. }
  669. break;
  670. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  671. {
  672. List<CPoint> cPoints = new List<CPoint>();
  673. for (int i = 0; i < e.Points.Count; i++)
  674. {
  675. Point cPoint = new Point((float)((e.Points[i].X - e.annotData.PaintOffset.X) / e.annotData.CurrentZoom),
  676. (float)((e.Points[i].Y - e.annotData.PaintOffset.Y) / e.annotData.CurrentZoom));
  677. cPoints.Add(DataConversionForWPF.PointConversionForCPoint(DpiHelper.StandardPointToPDFPoint(cPoint)));
  678. }
  679. CPDFPolygonAnnotation polygonAnnot = (e.annotData.Annot as CPDFPolygonAnnotation);
  680. polygonAnnot.SetPoints(cPoints);
  681. double left = cPoints.AsEnumerable().Select(x => x.x).Min();
  682. double right = cPoints.AsEnumerable().Select(x => x.x).Max();
  683. double top = cPoints.AsEnumerable().Select(x => x.y).Min();
  684. double bottom = cPoints.AsEnumerable().Select(x => x.y).Max();
  685. polygonAnnot.SetRect(new CRect(
  686. (float)left,
  687. (float)bottom,
  688. (float)right,
  689. (float)top));
  690. polygonAnnot.UpdateAp();
  691. if (polygonAnnot.IsMeasured())
  692. {
  693. polygonAnnot.GetAreaMeasure().UpdateAnnotMeasure();
  694. PostMeasureInfo(this, polygonAnnot);
  695. }
  696. }
  697. break;
  698. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
  699. {
  700. List<CPoint> cPoints = new List<CPoint>();
  701. for (int i = 0; i < e.Points.Count; i++)
  702. {
  703. Point cPoint = new Point((float)((e.Points[i].X - e.annotData.PaintOffset.X) / e.annotData.CurrentZoom),
  704. (float)((e.Points[i].Y - e.annotData.PaintOffset.Y) / e.annotData.CurrentZoom));
  705. cPoints.Add(DataConversionForWPF.PointConversionForCPoint(DpiHelper.StandardPointToPDFPoint(cPoint)));
  706. }
  707. CPDFPolylineAnnotation polylineAnnot = (e.annotData.Annot as CPDFPolylineAnnotation);
  708. polylineAnnot.SetPoints(cPoints);
  709. double left = cPoints.AsEnumerable().Select(x => x.x).Min();
  710. double right = cPoints.AsEnumerable().Select(x => x.x).Max();
  711. double top = cPoints.AsEnumerable().Select(x => x.y).Min();
  712. double bottom = cPoints.AsEnumerable().Select(x => x.y).Max();
  713. polylineAnnot.SetRect(new CRect(
  714. (float)left,
  715. (float)bottom,
  716. (float)right,
  717. (float)top));
  718. polylineAnnot.UpdateAp();
  719. if (polylineAnnot.IsMeasured())
  720. {
  721. polylineAnnot.GetPerimeterMeasure().UpdateAnnotMeasure();
  722. PostMeasureInfo(this, polylineAnnot);
  723. }
  724. }
  725. break;
  726. default:
  727. break;
  728. }
  729. switch (e.annotData.AnnotType)
  730. {
  731. case C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET:
  732. {
  733. currentParam = ParamConverter.WidgetConverter(viewerTool.GetCPDFViewer().GetDocument(), e.annotData.Annot);
  734. (e.annotData.Annot as CPDFWidget).UpdateFormAp();
  735. break;
  736. }
  737. default:
  738. {
  739. currentParam = ParamConverter.AnnotConverter(viewerTool.GetCPDFViewer().GetDocument(), e.annotData.Annot);
  740. if (e.annotData.AnnotType != C_ANNOTATION_TYPE.C_ANNOTATION_SOUND)
  741. {
  742. e.annotData.Annot.UpdateAp();
  743. }
  744. break;
  745. }
  746. }
  747. annotHistory.PreviousParam = previousParam;
  748. annotHistory.PDFDoc = viewerTool.GetCPDFViewer().GetDocument();
  749. annotHistory.CurrentParam = currentParam;
  750. annotHistory.Action = HistoryAction.Update;
  751. viewerTool.GetCPDFViewer().UndoManager.AddHistory(annotHistory);
  752. viewerTool.GetCPDFViewer().UpdateAnnotFrame();
  753. }
  754. private void ViewerTool_MouseLeftButtonUpHandler(object sender, MouseEventObject e)
  755. {
  756. if (viewerTool == null)
  757. return;
  758. viewerTool.DrawEndSelectedMultiRect();
  759. viewerTool.DrawEndPageSelectedRect();
  760. if (currentToolType != ToolType.SelectedPage &&
  761. viewerTool.IsCanSave() &&
  762. cPDFAnnotation?.Type != C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  763. {
  764. viewerTool.PDFViewer.EnableZoom(true);
  765. viewerTool.PDFViewer.CanHorizontallyScroll = true;
  766. viewerTool.PDFViewer.CanVerticallyScroll = true;
  767. }
  768. //To be optimized
  769. if (currentToolType != ToolType.WidgetEdit && currentToolType != ToolType.ContentEdit && viewerTool.IsText())
  770. {
  771. viewerTool.PDFViewer.Cursor = viewerTool.Cursor = Cursors.IBeam;
  772. }
  773. else if (currentToolType == ToolType.ContentEdit)
  774. {
  775. if (createContentEditType == CPDFEditType.EditText)
  776. viewerTool.PDFViewer.Cursor = viewerTool.Cursor = Cursors.IBeam;
  777. else
  778. viewerTool.PDFViewer.Cursor = viewerTool.Cursor = viewerTool.DrawMoveTest(viewerTool.GetLastSelectedRect());
  779. }
  780. else
  781. {
  782. viewerTool.PDFViewer.Cursor = viewerTool.Cursor = Cursors.Arrow;
  783. }
  784. if (currentToolType == ToolType.Customize)
  785. {
  786. viewerTool.CleanCustomizeTool();
  787. }
  788. if (e.hitTestType == MouseHitTestType.SelectRect)
  789. {
  790. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  791. {
  792. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  793. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  794. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  795. };
  796. if (list.Contains(e.annotType))
  797. {
  798. viewerTool.DrawEndEditAnnot();
  799. }
  800. else
  801. {
  802. viewerTool.DrawEndSelectedRect();
  803. }
  804. }
  805. switch (currentToolType)
  806. {
  807. case ToolType.CreateAnnot:
  808. CreateAnnotTypeMouseLeftUp(ref e);
  809. break;
  810. case ToolType.WidgetEdit:
  811. {
  812. if (cPDFAnnotation != null)
  813. {
  814. Rect rect = viewerTool.EndDrawWidget();
  815. CRect cRect = new CRect((float)rect.Left, (float)rect.Bottom, (float)rect.Right, (float)rect.Top);
  816. cPDFAnnotation.SetRect(cRect);
  817. (cPDFAnnotation as CPDFWidget).UpdateFormAp();
  818. CPDFDocument cPDFDocument = viewerTool.GetCPDFViewer().GetDocument();
  819. AnnotHistory annotHistory = ParamConverter.CreateHistory(cPDFAnnotation);
  820. if (annotHistory == null)
  821. {
  822. return;
  823. }
  824. WidgetParm annotParam = ParamConverter.WidgetConverter(viewerTool.GetCPDFViewer().GetDocument(), cPDFAnnotation) as WidgetParm;
  825. annotHistory.CurrentParam = annotParam;
  826. annotHistory.PDFDoc = cPDFDocument;
  827. viewerTool.GetCPDFViewer().UndoManager.AddHistory(annotHistory);
  828. viewerTool.GetCPDFViewer().UpdateAnnotFrame();
  829. e.annotType = cPDFAnnotation.Type;
  830. e.IsCreate = true;
  831. dynamic expandData = new ExpandoObject();
  832. expandData.AnnotIndex = annotParam.AnnotIndex;
  833. expandData.PageIndex = annotParam.PageIndex;
  834. expandData.AnnotParam = annotParam;
  835. e.Data = expandData;
  836. }
  837. }
  838. break;
  839. case ToolType.ContentEdit:
  840. {
  841. if (viewerTool.GetLastSelectedRect() != null)
  842. {
  843. //Crop Save Processing
  844. if (!viewerTool.GetIsCropMode())
  845. {
  846. viewerTool.DrawEndTextEdit(viewerTool.GetLastSelectedRect());
  847. }
  848. else
  849. {
  850. if (isActiveCropping)
  851. {
  852. CropSelectRect();
  853. viewerTool.SetClipThickness();
  854. }
  855. //Originally saved cropping logic
  856. }
  857. editSelected = false;
  858. }
  859. else
  860. {
  861. if (createContentEditType == CPDFEditType.EditImage && viewerTool.PDFViewer.GetIsShowStampMouse())
  862. {
  863. Rect stampRect = viewerTool.PDFViewer.GetStampRect();
  864. Point mousePoint = new Point(
  865. stampRect.X - stampRect.Width / 2 * viewerTool.GetCPDFViewer().GetZoom(),
  866. stampRect.Y - stampRect.Height / 2 * viewerTool.GetCPDFViewer().GetZoom()
  867. );
  868. viewerTool.GetCPDFViewer().GetPointPageInfo(mousePoint, out int pageindex, out Rect paintRect, out Rect pageBound);
  869. if (pageindex >= 0)
  870. {
  871. Rect PDFRect = DpiHelper.StandardRectToPDFRect(new Rect((mousePoint.X - pageBound.X) / viewerTool.GetCPDFViewer().GetZoom(), (mousePoint.Y - pageBound.Y) / viewerTool.GetCPDFViewer().GetZoom(), stampRect.Width, stampRect.Height));
  872. CRect SaveRect = new CRect((float)PDFRect.Left, (float)PDFRect.Bottom, (float)PDFRect.Right, (float)PDFRect.Top);
  873. CPDFPage docPage = viewerTool.PDFViewer.GetDocument().PageAtIndex(pageindex);
  874. CPDFEditPage EditPage = docPage.GetEditPage();
  875. CPDFEditImageArea cPDFEditImageArea = EditPage.CreateNewImageArea(SaveRect, createImagePath, string.Empty);
  876. if (cPDFEditImageArea == null)
  877. {
  878. byte[] imageData = null;
  879. int imageWidth = 0;
  880. int imageHeight = 0;
  881. PDFHelp.ImagePathToByte(createImagePath, ref imageData, ref imageWidth, ref imageHeight);
  882. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  883. {
  884. cPDFEditImageArea = EditPage.CreateNewImageArea(SaveRect, imageData, imageWidth, imageHeight);
  885. }
  886. }
  887. viewerTool.PDFViewer.UpdateRenderFrame();
  888. PDFEditHistory editHistory = new PDFEditHistory();
  889. editHistory.EditPage = EditPage;
  890. editHistory.PageIndex = pageindex;
  891. EditPage.EndEdit();
  892. viewerTool.PDFViewer.UndoManager.AddHistory(editHistory);
  893. e.IsCreate = true;
  894. }
  895. }
  896. else if (createContentEditType == CPDFEditType.EditText)
  897. {
  898. if (viewerTool.CanAddTextEdit)
  899. {
  900. e.IsCreate = viewerTool.DrawEndTest();
  901. }
  902. }
  903. else
  904. {
  905. //Draw a box to select multiple boxes
  906. Rect rectFrameSelect = viewerTool.DrawEndFrameSelect();
  907. viewerTool.FrameSelectAddRect(rectFrameSelect);
  908. e.IsCreate = true;
  909. }
  910. }
  911. }
  912. break;
  913. case ToolType.Customize:
  914. viewerTool.DrawEndCustomizeTool();
  915. break;
  916. default:
  917. break;
  918. }
  919. viewerTool.DrawEndSelectText();
  920. MouseLeftButtonUpHandler?.Invoke(this, e);
  921. if (viewerTool.IsCanSave())
  922. {
  923. cPDFAnnotation = null;
  924. }
  925. }
  926. /// <summary>
  927. /// Create cropping logic
  928. /// </summary>
  929. public void CropSelectRect()
  930. {
  931. if (viewerTool.GetLastSelectedRect() != null)
  932. {
  933. if (viewerTool.GetIsCropMode())
  934. {
  935. viewerTool.DrawEndTextEdit(viewerTool.GetLastSelectedRect());
  936. }
  937. }
  938. viewerTool.DrawEndSelectText();
  939. if (viewerTool.IsCanSave())
  940. {
  941. cPDFAnnotation = null;
  942. }
  943. }
  944. private void SaveCurrentAnnot()
  945. {
  946. viewerTool.SetIsCanSave(true);
  947. MouseEventObject e = new MouseEventObject
  948. {
  949. mouseButtonEventArgs = null,
  950. hitTestType = MouseHitTestType.Unknown,
  951. annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
  952. IsCreate = false
  953. };
  954. switch (currentToolType)
  955. {
  956. case ToolType.CreateAnnot:
  957. CreateAnnotTypeMouseLeftUp(ref e);
  958. break;
  959. case ToolType.WidgetEdit:
  960. {
  961. if (cPDFAnnotation != null)
  962. {
  963. Rect rect = viewerTool.EndDrawWidget();
  964. CRect cRect = new CRect((float)rect.Left, (float)rect.Bottom, (float)rect.Right, (float)rect.Top);
  965. cPDFAnnotation.SetRect(cRect);
  966. (cPDFAnnotation as CPDFWidget).UpdateFormAp();
  967. CPDFDocument cPDFDocument = viewerTool.GetCPDFViewer().GetDocument();
  968. AnnotHistory annotHistory = ParamConverter.CreateHistory(cPDFAnnotation);
  969. if (annotHistory == null)
  970. {
  971. return;
  972. }
  973. WidgetParm annotParam = ParamConverter.WidgetConverter(viewerTool.GetCPDFViewer().GetDocument(), cPDFAnnotation) as WidgetParm;
  974. annotHistory.CurrentParam = annotParam;
  975. annotHistory.PDFDoc = cPDFDocument;
  976. viewerTool.GetCPDFViewer().UndoManager.AddHistory(annotHistory);
  977. viewerTool.GetCPDFViewer().UpdateAnnotFrame();
  978. e.annotType = cPDFAnnotation.Type;
  979. e.IsCreate = true;
  980. dynamic expandData = new ExpandoObject();
  981. expandData.AnnotIndex = annotParam.AnnotIndex;
  982. expandData.PageIndex = annotParam.PageIndex;
  983. expandData.AnnotParam = annotParam;
  984. e.Data = expandData;
  985. }
  986. }
  987. break;
  988. case ToolType.Customize:
  989. viewerTool.DrawEndCustomizeTool();
  990. break;
  991. default:
  992. break;
  993. }
  994. if (viewerTool.IsCanSave())
  995. {
  996. cPDFAnnotation = null;
  997. }
  998. }
  999. #region MouseLeftButtonUpCreateAnnot
  1000. private void CreateAnnotTypeMouseLeftUp(ref MouseEventObject e)
  1001. {
  1002. //Mersured
  1003. if (cPDFAnnotation != null)
  1004. {
  1005. switch (cPDFAnnotation.Type)
  1006. {
  1007. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  1008. {
  1009. if ((cPDFAnnotation as CPDFLineAnnotation).IsMeasured())
  1010. {
  1011. MeasureSetting measureSetting = viewerTool.GetMeasureSetting();
  1012. if (viewerTool.GetMoveLength() > measureSetting.MoveDetectionLength)
  1013. {
  1014. viewerTool.SetIsCanSave(true);
  1015. }
  1016. }
  1017. }
  1018. break;
  1019. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  1020. // if ((cPDFAnnotation as CPDFPolygonAnnotation).IsMeasured())
  1021. {
  1022. DefaultSettingParam defSetting = viewerTool.GetDefaultSettingParam();
  1023. if (defSetting.IsCreateSquarePolygonMeasure)
  1024. {
  1025. MeasureSetting measureSetting = viewerTool.GetMeasureSetting();
  1026. if (viewerTool.GetMoveLength() > measureSetting.MoveDetectionLength)
  1027. {
  1028. viewerTool.SetIsCanSave(true);
  1029. }
  1030. }
  1031. }
  1032. break;
  1033. default:
  1034. break;
  1035. }
  1036. }
  1037. if (viewerTool.IsCanSave())
  1038. {
  1039. SaveCreateAnnotation(ref cPDFAnnotation, ref e);
  1040. viewerTool.PDFViewer.EnableZoom(true);
  1041. viewerTool.PDFViewer.CanHorizontallyScroll = true;
  1042. viewerTool.PDFViewer.CanVerticallyScroll = true;
  1043. }
  1044. }
  1045. private void SaveCreateAnnotation(ref CPDFAnnotation annotation, ref MouseEventObject e)
  1046. {
  1047. if (annotation == null)
  1048. {
  1049. return;
  1050. }
  1051. if (!annotation.IsValid())
  1052. {
  1053. annotation.RemoveAnnot();
  1054. annotation = null;
  1055. return;
  1056. }
  1057. Point StartPoint = viewerTool.GetStartPoint();
  1058. Point EndPoint = viewerTool.GetEndPoint();
  1059. List<Point> points = viewerTool.GetInkDrawPoints();
  1060. List<Point> measurepoints = viewerTool.GetMeasureDrawPoints();
  1061. annotation.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  1062. annotation.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  1063. Rect rect = viewerTool.EndDrawAnnot();
  1064. if (annotation != null)
  1065. {
  1066. switch (annotation.Type)
  1067. {
  1068. case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
  1069. SaveInkCreateAnnotation(ref annotation, points);
  1070. e.IsCreate = true;
  1071. e.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_INK;
  1072. e.Data = GetAnnotExpandObject(annotation);
  1073. if (e.mouseButtonEventArgs != null)
  1074. {
  1075. MouseLeftButtonUpHandler?.Invoke(this, e);
  1076. }
  1077. annotation = null;
  1078. return;
  1079. case C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT:
  1080. e.IsCreate = true;
  1081. e.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT;
  1082. e.Data = GetAnnotExpandObject(annotation);
  1083. if (e.mouseButtonEventArgs != null)
  1084. {
  1085. MouseLeftButtonUpHandler?.Invoke(this, e);
  1086. }
  1087. return;
  1088. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  1089. {
  1090. if ((annotation as CPDFLineAnnotation).IsMeasured())
  1091. {
  1092. if (measurepoints.Count > 1)
  1093. {
  1094. (annotation as CPDFLineAnnotation).SetLinePoints(new CPoint(
  1095. (float)DpiHelper.StandardNumToPDFNum(measurepoints[0].X),
  1096. (float)DpiHelper.StandardNumToPDFNum(measurepoints[0].Y)),
  1097. new CPoint((float)DpiHelper.StandardNumToPDFNum(measurepoints[1].X),
  1098. (float)DpiHelper.StandardNumToPDFNum(measurepoints[1].Y)));
  1099. (annotation as CPDFLineAnnotation).GetDistanceMeasure().UpdateAnnotMeasure();
  1100. PostMeasureInfo(this, annotation);
  1101. }
  1102. else
  1103. {
  1104. annotation.ReleaseAnnot();
  1105. if (e.mouseButtonEventArgs != null)
  1106. {
  1107. MouseLeftButtonUpHandler?.Invoke(this, e);
  1108. }
  1109. annotation = null;
  1110. return;
  1111. }
  1112. }
  1113. else
  1114. {
  1115. if (EndPoint != new Point(-1, -1))
  1116. {
  1117. (annotation as CPDFLineAnnotation).SetLinePoints(new CPoint((float)StartPoint.X, (float)StartPoint.Y), new CPoint((float)EndPoint.X, (float)EndPoint.Y));
  1118. }
  1119. else
  1120. {
  1121. annotation.ReleaseAnnot();
  1122. if (e.mouseButtonEventArgs != null)
  1123. {
  1124. MouseLeftButtonUpHandler?.Invoke(this, e);
  1125. }
  1126. annotation = null;
  1127. return;
  1128. }
  1129. }
  1130. }
  1131. break;
  1132. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  1133. {
  1134. List<CPoint> cPoints = new List<CPoint>();
  1135. foreach (Point item in measurepoints)
  1136. {
  1137. cPoints.Add(DataConversionForWPF.PointConversionForCPoint(DpiHelper.StandardPointToPDFPoint(item)));
  1138. }
  1139. (annotation as CPDFPolygonAnnotation).SetPoints(cPoints);
  1140. if ((annotation as CPDFPolygonAnnotation).IsMeasured())
  1141. {
  1142. (annotation as CPDFPolygonAnnotation).GetAreaMeasure().UpdateAnnotMeasure();
  1143. PostMeasureInfo(this, annotation);
  1144. }
  1145. }
  1146. break;
  1147. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
  1148. {
  1149. if ((annotation as CPDFPolylineAnnotation).IsMeasured())
  1150. {
  1151. List<CPoint> cPoints = new List<CPoint>();
  1152. foreach (Point item in measurepoints)
  1153. {
  1154. cPoints.Add(DataConversionForWPF.PointConversionForCPoint(DpiHelper.StandardPointToPDFPoint(item)));
  1155. }
  1156. (annotation as CPDFPolylineAnnotation).SetPoints(cPoints);
  1157. (annotation as CPDFPolylineAnnotation).GetPerimeterMeasure().UpdateAnnotMeasure();
  1158. PostMeasureInfo(this, annotation);
  1159. }
  1160. }
  1161. break;
  1162. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  1163. if (rect.Width > 0 && rect.Height > 0)
  1164. {
  1165. List<CRect> coreRectList = new List<CRect>
  1166. {
  1167. new CRect((float)rect.Left, (float)rect.Bottom, (float)rect.Right, (float)rect.Top)
  1168. };
  1169. (annotation as CPDFRedactAnnotation).SetQuardRects(coreRectList);
  1170. }
  1171. break;
  1172. default:
  1173. break;
  1174. }
  1175. if (rect.Width <= 0 && rect.Height <= 0)
  1176. {
  1177. switch (createAnnotType)
  1178. {
  1179. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  1180. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  1181. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  1182. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  1183. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  1184. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  1185. {
  1186. annotation.RemoveAnnot();
  1187. viewerTool.DrawEndSelectText();
  1188. TextSelectInfo textSelectInfo = viewerTool.GetTextSelectInfo();
  1189. if (textSelectInfo.PageSelectPointList.Count > 0)
  1190. {
  1191. viewerTool.DrawEndSelectText();
  1192. viewerTool.RemoveSelectTextData();
  1193. GroupHistory historyData = null;
  1194. viewerTool.CreateAnnotForSelectText(textSelectInfo, createAnnotType, out historyData);
  1195. List<object> dataList = new List<object>();
  1196. if (historyData != null && historyData.Histories.Count > 0)
  1197. {
  1198. foreach (IHistory historyItem in historyData.Histories)
  1199. {
  1200. AnnotHistory checkHistory = historyItem as AnnotHistory;
  1201. if (checkHistory == null || checkHistory.CurrentParam == null)
  1202. {
  1203. continue;
  1204. }
  1205. dynamic expandData = new ExpandoObject();
  1206. expandData.AnnotIndex = checkHistory.CurrentParam.AnnotIndex;
  1207. expandData.PageIndex = checkHistory.CurrentParam.PageIndex;
  1208. expandData.AnnotParam = checkHistory.CurrentParam;
  1209. dataList.Add(expandData);
  1210. }
  1211. }
  1212. e.annotType = createAnnotType;
  1213. e.IsCreate = true;
  1214. if (dataList.Count > 0)
  1215. {
  1216. e.Data = dataList.Count > 1 ? dataList : dataList[0];
  1217. }
  1218. }
  1219. if (createAnnotType == C_ANNOTATION_TYPE.C_ANNOTATION_REDACT && textSelectInfo.PageSelectPointList.Count == 0)
  1220. {
  1221. viewerTool.GetCPDFViewer().UpdateAnnotFrame();
  1222. }
  1223. else if (createAnnotType == C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT && textSelectInfo.PageSelectPointList.Count == 0)
  1224. {
  1225. bool isA = annotation.RemoveAnnot();
  1226. annotation = null;
  1227. return;
  1228. }
  1229. }
  1230. break;
  1231. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  1232. {
  1233. if (!viewerTool.PDFViewer.GetIsShowStampMouse())
  1234. {
  1235. break;
  1236. }
  1237. Rect stampRect = viewerTool.PDFViewer.GetStampRect();
  1238. Point mousePoint = new Point(
  1239. stampRect.X - stampRect.Width / 2 * viewerTool.GetCPDFViewer().GetZoom(),
  1240. stampRect.Y - stampRect.Height / 2 * viewerTool.GetCPDFViewer().GetZoom()
  1241. );
  1242. viewerTool.GetCPDFViewer().GetPointPageInfo(mousePoint, out int pageindex, out Rect paintRect, out Rect pageBound);
  1243. if (pageindex < 0)
  1244. {
  1245. annotation.RemoveAnnot();
  1246. annotation = null;
  1247. return;
  1248. }
  1249. else
  1250. {
  1251. CPDFDocument cPDFDocument = viewerTool.GetCPDFViewer().GetDocument();
  1252. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(pageindex);
  1253. Point cropPoint = new Point();
  1254. if (viewerTool.GetCPDFViewer().GetIsCrop())
  1255. {
  1256. CRect cropRect = cPDFPage.GetCropBounds();
  1257. cropPoint.X = DpiHelper.PDFNumToStandardNum(cropRect.left);
  1258. cropPoint.Y = DpiHelper.PDFNumToStandardNum(cropRect.top);
  1259. }
  1260. Rect PDFRect = DpiHelper.StandardRectToPDFRect(new Rect(
  1261. (mousePoint.X - pageBound.X + (cropPoint.X * viewerTool.GetCPDFViewer().GetZoom())) / viewerTool.GetCPDFViewer().GetZoom(),
  1262. (mousePoint.Y - pageBound.Y + (cropPoint.Y * viewerTool.GetCPDFViewer().GetZoom())) / viewerTool.GetCPDFViewer().GetZoom(),
  1263. stampRect.Width, stampRect.Height)
  1264. );
  1265. if (annotation.Page.Rotation % 2 != 0)
  1266. {
  1267. PDFRect = RotateRect90(PDFRect);
  1268. }
  1269. CRect cStampRect = new CRect((float)PDFRect.Left, (float)PDFRect.Bottom, (float)PDFRect.Right, (float)PDFRect.Top);
  1270. annotation.SetSourceRect(cStampRect);
  1271. (annotation as CPDFStampAnnotation).AnnotationRotator.SetRotation(annotation.Page.Rotation * 90);
  1272. annotation.UpdateAp();
  1273. e.IsCreate = true;
  1274. e.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP;
  1275. e.Data = GetAnnotExpandObject(annotation);
  1276. StampAnnotHistory stampAnnotHistory = new StampAnnotHistory();
  1277. AnnotParam annotParam = ParamConverter.AnnotConverter(viewerTool.GetCPDFViewer().GetDocument(), annotation);
  1278. annotParam.AnnotIndex = annotation.Page.GetAnnotCount() - 1;
  1279. stampAnnotHistory.CurrentParam = annotParam;
  1280. stampAnnotHistory.PDFDoc = viewerTool.GetCPDFViewer().GetDocument();
  1281. viewerTool.GetCPDFViewer().UndoManager.AddHistory(stampAnnotHistory);
  1282. }
  1283. viewerTool.GetCPDFViewer().UpdateAnnotFrame();
  1284. }
  1285. break;
  1286. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  1287. {
  1288. Point point = Mouse.GetPosition(viewerTool.PDFViewer);
  1289. viewerTool.GetCPDFViewer().GetPointPageInfo(point, out int pageindex, out Rect paintRect, out Rect pageBound);
  1290. if (pageindex < 0)
  1291. {
  1292. annotation.RemoveAnnot();
  1293. annotation = null;
  1294. return;
  1295. }
  1296. else
  1297. {
  1298. CPDFDocument cPDFDocument = viewerTool.GetCPDFViewer().GetDocument();
  1299. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(pageindex);
  1300. Point cropPoint = new Point();
  1301. if (viewerTool.GetCPDFViewer().GetIsCrop())
  1302. {
  1303. CRect cropRect = cPDFPage.GetCropBounds();
  1304. cropPoint.X = DpiHelper.PDFNumToStandardNum(cropRect.left);
  1305. cropPoint.Y = DpiHelper.PDFNumToStandardNum(cropRect.top);
  1306. }
  1307. CRect x = annotation.GetRect();
  1308. Rect PDFRect = DpiHelper.StandardRectToPDFRect(new Rect(
  1309. (point.X - pageBound.X + (cropPoint.X * viewerTool.GetCPDFViewer().GetZoom())) / viewerTool.GetCPDFViewer().GetZoom(),
  1310. (point.Y - pageBound.Y + (cropPoint.Y * viewerTool.GetCPDFViewer().GetZoom())) / viewerTool.GetCPDFViewer().GetZoom(),
  1311. x.width(), x.height())
  1312. );
  1313. PDFRect.X = PDFRect.X - x.width() / 2;
  1314. PDFRect.Y = PDFRect.Y - x.height() / 2;
  1315. CRect cStampRect = new CRect((float)PDFRect.Left, (float)PDFRect.Bottom, (float)PDFRect.Right, (float)PDFRect.Top);
  1316. annotation.SetRect(cStampRect);
  1317. viewerTool.GetCPDFViewer().UpdateAnnotFrame();
  1318. e.IsCreate = true;
  1319. e.annotType = createAnnotType;
  1320. e.Data = GetAnnotExpandObject(annotation);
  1321. }
  1322. }
  1323. break;
  1324. default:
  1325. break;
  1326. }
  1327. //annotation.ReleaseAnnot();
  1328. if (e.mouseButtonEventArgs != null)
  1329. {
  1330. MouseLeftButtonUpHandler?.Invoke(this, e);
  1331. }
  1332. annotation = null;
  1333. return;
  1334. }
  1335. CRect cRect = new CRect(
  1336. (float)rect.Left,
  1337. (float)rect.Bottom,
  1338. (float)rect.Right,
  1339. (float)rect.Top);
  1340. annotation.SetRect(cRect);
  1341. SaveSharpAnnotBoundText(annotation);
  1342. if (annotation.Type != C_ANNOTATION_TYPE.C_ANNOTATION_TEXT)
  1343. {
  1344. annotation.UpdateAp();
  1345. }
  1346. else
  1347. {
  1348. CommonHelper.UpdateStickyAP(annotation as CPDFTextAnnotation);
  1349. }
  1350. AnnotHistory annotHistory = ParamConverter.CreateHistory(annotation);
  1351. if (annotHistory == null)
  1352. {
  1353. return;
  1354. }
  1355. AnnotParam currentParam;
  1356. switch (annotation.Type)
  1357. {
  1358. case C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET:
  1359. currentParam = ParamConverter.WidgetConverter(viewerTool.GetCPDFViewer().GetDocument(), annotation);
  1360. break;
  1361. default:
  1362. currentParam = ParamConverter.AnnotConverter(viewerTool.GetCPDFViewer().GetDocument(), annotation);
  1363. break;
  1364. }
  1365. annotHistory.CurrentParam = currentParam;
  1366. annotHistory.Action = HistoryAction.Add;
  1367. annotHistory.PDFDoc = viewerTool.PDFViewer.GetDocument();
  1368. viewerTool.ClearDrawAnnot();
  1369. viewerTool.GetCPDFViewer().UpdateAnnotFrame();
  1370. viewerTool.GetCPDFViewer().UndoManager.AddHistory(annotHistory);
  1371. if (annotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_TEXT && SaveEmptyStickyAnnot == false)
  1372. {
  1373. BaseLayer baseLayer1 = viewerTool.GetCPDFViewer().GetViewForTag(viewerTool.GetCPDFViewer().GetAnnotViewTag());
  1374. int checkPageIndex = currentParam.PageIndex;
  1375. int checkAnnotIndex = currentParam.AnnotIndex;
  1376. BaseAnnot selectAnnot = (baseLayer1 as AnnotLayer).GetSelectedAnnot(ref checkPageIndex, ref checkAnnotIndex);
  1377. if (selectAnnot != null)
  1378. {
  1379. StickyNoteAnnot stickyAnnot = selectAnnot as StickyNoteAnnot;
  1380. StickyNoteAnnot.StickyPopupClosed -= StickyAnnot_StickyPopupClosed;
  1381. StickyNoteAnnot.StickyPopupClosed += StickyAnnot_StickyPopupClosed;
  1382. stickyAnnot.PopStickyNote();
  1383. }
  1384. }
  1385. {
  1386. e.annotType = annotation.Type;
  1387. e.IsCreate = true;
  1388. dynamic expandData = new ExpandoObject();
  1389. expandData.AnnotIndex = currentParam.AnnotIndex;
  1390. expandData.PageIndex = currentParam.PageIndex;
  1391. expandData.AnnotParam = currentParam;
  1392. e.Data = expandData;
  1393. }
  1394. }
  1395. }
  1396. private void SaveSharpAnnotBoundText(CPDFAnnotation boundAnnot)
  1397. {
  1398. if (boundAnnot == null || boundAnnot.Page == null || boundAnnot.Page.IsValid() == false)
  1399. {
  1400. return;
  1401. }
  1402. if (boundAnnot.Type != C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE && boundAnnot.Type != C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
  1403. {
  1404. return;
  1405. }
  1406. CPDFTextPage textPage = boundAnnot.Page.GetTextPage();
  1407. if (textPage == null || textPage.IsValid() == false)
  1408. {
  1409. return;
  1410. }
  1411. string boundText = textPage.GetBoundedText(boundAnnot.GetRect());
  1412. if (string.IsNullOrEmpty(boundText) == false)
  1413. {
  1414. boundAnnot.SetContent(boundText);
  1415. }
  1416. }
  1417. private void StickyAnnot_StickyPopupClosed(object sender, EventArgs e)
  1418. {
  1419. StickyNoteAnnot.StickyPopupClosed -= StickyAnnot_StickyPopupClosed;
  1420. StickyNoteAnnot stickyAnnot = sender as StickyNoteAnnot;
  1421. if (stickyAnnot == null)
  1422. {
  1423. return;
  1424. }
  1425. AnnotData annotData = stickyAnnot.GetAnnotData();
  1426. AnnotParam currentParam = ParamConverter.AnnotConverter(viewerTool.GetCPDFViewer().GetDocument(), annotData.Annot);
  1427. AnnotHistory annotHistory = ParamConverter.CreateHistory(annotData.Annot);
  1428. string content = annotData.Annot.GetContent();
  1429. if (string.IsNullOrEmpty(content))
  1430. {
  1431. if (annotData.Annot.RemoveAnnot())
  1432. {
  1433. viewerTool.ClearDrawAnnot();
  1434. viewerTool.GetCPDFViewer().UpdateAnnotFrame();
  1435. viewerTool.SelectedAnnotForIndex(-1, -1);
  1436. annotHistory.CurrentParam = currentParam;
  1437. annotHistory.Action = HistoryAction.Remove;
  1438. annotHistory.PDFDoc = viewerTool.PDFViewer.GetDocument();
  1439. viewerTool.GetCPDFViewer().UndoManager.AddHistory(annotHistory);
  1440. }
  1441. return;
  1442. }
  1443. AnnotParam previousParam = ParamConverter.AnnotConverter(viewerTool.GetCPDFViewer().GetDocument(), annotData.Annot);
  1444. previousParam.Content = string.Empty;
  1445. annotHistory.PreviousParam = previousParam;
  1446. annotHistory.CurrentParam = currentParam;
  1447. annotHistory.Action = HistoryAction.Update;
  1448. annotHistory.PDFDoc = viewerTool.PDFViewer.GetDocument();
  1449. viewerTool.GetCPDFViewer().UndoManager.AddHistory(annotHistory);
  1450. }
  1451. internal void PostMeasureInfo(object sender, CPDFAnnotation rawAnnot)
  1452. {
  1453. if (rawAnnot == null)
  1454. {
  1455. return;
  1456. }
  1457. try
  1458. {
  1459. if (rawAnnot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  1460. {
  1461. CPDFLineAnnotation lineAnnot = rawAnnot as CPDFLineAnnotation;
  1462. if (lineAnnot.IsMeasured() && lineAnnot.Points != null && lineAnnot.Points.Count() == 2)
  1463. {
  1464. CPDFDistanceMeasure lineMeasure = lineAnnot.GetDistanceMeasure();
  1465. CPDFMeasureInfo measureInfo = lineMeasure.MeasureInfo;
  1466. Vector standVector = new Vector(1, 0);
  1467. Point startPoint = new Point(lineAnnot.Points[0].x, lineAnnot.Points[0].y);
  1468. Point endPoint = new Point(lineAnnot.Points[1].x, lineAnnot.Points[1].y);
  1469. Vector movevector = endPoint - startPoint;
  1470. double showLenght = lineMeasure.GetMeasurementResults(CPDFCaptionType.CPDF_CAPTION_LENGTH);
  1471. MeasureEventArgs measureEvent = new MeasureEventArgs();
  1472. measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(movevector, standVector));
  1473. measureEvent.RulerTranslateUnit = measureInfo.RulerTranslateUnit;
  1474. measureEvent.RulerTranslate = measureInfo.RulerTranslate;
  1475. measureEvent.RulerBase = measureInfo.RulerBase;
  1476. measureEvent.RulerBaseUnit = measureInfo.RulerBaseUnit;
  1477. measureEvent.MousePos = new Point(
  1478. (int)Math.Abs(movevector.X),
  1479. (int)Math.Abs(movevector.Y));
  1480. measureEvent.Type = CPDFMeasureType.CPDF_DISTANCE_MEASURE;
  1481. NumberFormatInfo formatInfo = new NumberFormatInfo();
  1482. formatInfo.NumberDecimalDigits = Math.Abs(measureInfo.Precision).ToString().Length - 1;
  1483. measureEvent.Distance = showLenght.ToString("N", formatInfo) + " " + measureInfo.RulerTranslateUnit;
  1484. measureEvent.Precision = GetMeasureShowPrecision(measureInfo.Precision);
  1485. viewerTool?.InvokeMeasureChangeEvent(sender, measureEvent);
  1486. }
  1487. }
  1488. if (rawAnnot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE)
  1489. {
  1490. CPDFPolylineAnnotation polylineAnnot = rawAnnot as CPDFPolylineAnnotation;
  1491. if (polylineAnnot.IsMeasured() && polylineAnnot.Points != null && polylineAnnot.Points.Count() >= 2)
  1492. {
  1493. double totalInch = 0;
  1494. for (int i = 0; i < polylineAnnot.Points.Count - 1; i++)
  1495. {
  1496. Point endLinePoint = new Point(
  1497. polylineAnnot.Points[i + 1].x,
  1498. polylineAnnot.Points[i + 1].y
  1499. );
  1500. Point startLinePoint = new Point(
  1501. polylineAnnot.Points[i].x,
  1502. polylineAnnot.Points[i].y
  1503. );
  1504. Vector subVector = endLinePoint - startLinePoint;
  1505. totalInch += subVector.Length;
  1506. }
  1507. totalInch = totalInch / 72D;
  1508. CPDFPerimeterMeasure lineMeasure = polylineAnnot.GetPerimeterMeasure();
  1509. CPDFMeasureInfo measureInfo = lineMeasure.MeasureInfo;
  1510. double showLenght = lineMeasure.GetMeasurementResults(CPDFCaptionType.CPDF_CAPTION_LENGTH);
  1511. MeasureEventArgs measureEvent = new MeasureEventArgs();
  1512. measureEvent.Angle = 0;
  1513. measureEvent.RulerTranslateUnit = measureInfo.RulerTranslateUnit;
  1514. measureEvent.RulerTranslate = measureInfo.RulerTranslate;
  1515. measureEvent.RulerBase = measureInfo.RulerBase;
  1516. measureEvent.RulerBaseUnit = measureInfo.RulerBaseUnit;
  1517. measureEvent.Precision = GetMeasureShowPrecision(measureInfo.Precision);
  1518. measureEvent.Type = CPDFMeasureType.CPDF_PERIMETER_MEASURE;
  1519. NumberFormatInfo formatInfo = new NumberFormatInfo();
  1520. formatInfo.NumberDecimalDigits = Math.Abs(measureInfo.Precision).ToString().Length - 1;
  1521. measureEvent.Distance = showLenght.ToString("N", formatInfo) + " " + measureInfo.RulerTranslateUnit;
  1522. viewerTool?.InvokeMeasureChangeEvent(sender, measureEvent);
  1523. }
  1524. }
  1525. if (rawAnnot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON)
  1526. {
  1527. CPDFPolygonAnnotation polygonAnnot = rawAnnot as CPDFPolygonAnnotation;
  1528. if (polygonAnnot.IsMeasured() && polygonAnnot.Points != null && polygonAnnot.Points.Count() >= 2)
  1529. {
  1530. double totalInch = 0;
  1531. for (int i = 0; i < polygonAnnot.Points.Count - 1; i++)
  1532. {
  1533. Point endLinePoint = new Point(
  1534. polygonAnnot.Points[i + 1].x,
  1535. polygonAnnot.Points[i + 1].y
  1536. );
  1537. Point startLinePoint = new Point(
  1538. polygonAnnot.Points[i].x,
  1539. polygonAnnot.Points[i].y
  1540. );
  1541. Vector subVector = endLinePoint - startLinePoint;
  1542. totalInch += subVector.Length;
  1543. }
  1544. totalInch = totalInch / 72D;
  1545. CPDFAreaMeasure areaMeasure = polygonAnnot.GetAreaMeasure();
  1546. CPDFMeasureInfo measureInfo = areaMeasure.MeasureInfo;
  1547. double showLenght = areaMeasure.GetMeasurementResults(CPDFCaptionType.CPDF_CAPTION_LENGTH);
  1548. MeasureEventArgs measureEvent = new MeasureEventArgs();
  1549. measureEvent.Angle = 0;
  1550. measureEvent.RulerTranslateUnit = measureInfo.RulerTranslateUnit;
  1551. measureEvent.RulerTranslate = measureInfo.RulerTranslate;
  1552. measureEvent.RulerBase = measureInfo.RulerBase;
  1553. measureEvent.RulerBaseUnit = measureInfo.RulerBaseUnit;
  1554. measureEvent.Precision = GetMeasureShowPrecision(measureInfo.Precision);
  1555. measureEvent.Type = CPDFMeasureType.CPDF_AREA_MEASURE;
  1556. NumberFormatInfo formatInfo = new NumberFormatInfo();
  1557. formatInfo.NumberDecimalDigits = Math.Abs(measureInfo.Precision).ToString().Length - 1;
  1558. measureEvent.Distance = showLenght.ToString("N", formatInfo) + " " + measureInfo.RulerTranslateUnit;
  1559. double area = areaMeasure.GetMeasurementResults(CPDFCaptionType.CPDF_CAPTION_AREA);
  1560. measureEvent.Area = string.Format("{0} sq {1}", GetPrecisionData(area, measureEvent.Precision), measureEvent.RulerTranslateUnit);
  1561. viewerTool?.InvokeMeasureChangeEvent(sender, measureEvent);
  1562. }
  1563. }
  1564. }
  1565. catch (Exception e)
  1566. {
  1567. }
  1568. }
  1569. public string GetPrecisionData(double number, double precision)
  1570. {
  1571. NumberFormatInfo formatInfo = new NumberFormatInfo();
  1572. formatInfo.NumberDecimalDigits = 2;
  1573. if (precision == 1)
  1574. {
  1575. formatInfo.NumberDecimalDigits = 0;
  1576. }
  1577. if (precision == 0.1)
  1578. {
  1579. formatInfo.NumberDecimalDigits = 1;
  1580. }
  1581. if (precision == 0.01)
  1582. {
  1583. formatInfo.NumberDecimalDigits = 2;
  1584. }
  1585. if (precision == 0.001)
  1586. {
  1587. formatInfo.NumberDecimalDigits = 3;
  1588. }
  1589. if (precision == 0.0001)
  1590. {
  1591. formatInfo.NumberDecimalDigits = 4;
  1592. }
  1593. if (precision == 0.00001)
  1594. {
  1595. formatInfo.NumberDecimalDigits = 5;
  1596. }
  1597. return number.ToString("N", formatInfo);
  1598. }
  1599. internal double GetMeasureShowPrecision(int precision)
  1600. {
  1601. if (precision == CPDFMeasure.PRECISION_VALUE_ZERO)
  1602. {
  1603. return 1;
  1604. }
  1605. if (CPDFMeasure.PRECISION_VALUE_ONE == precision)
  1606. {
  1607. return 0.1;
  1608. }
  1609. if (CPDFMeasure.PRECISION_VALUE_TWO == precision)
  1610. {
  1611. return 0.01;
  1612. }
  1613. if (CPDFMeasure.PRECISION_VALUE_THREE == precision)
  1614. {
  1615. return 0.001;
  1616. }
  1617. if (CPDFMeasure.PRECISION_VALUE_FOUR == precision)
  1618. {
  1619. return 0.0001;
  1620. }
  1621. return 0;
  1622. }
  1623. private object GetAnnotExpandObject(CPDFAnnotation annot)
  1624. {
  1625. if (annot != null && annot.IsValid())
  1626. {
  1627. try
  1628. {
  1629. AnnotParam annotParam = null;
  1630. if (annot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  1631. {
  1632. annotParam = ParamConverter.WidgetConverter(viewerTool.GetCPDFViewer().GetDocument(), annot);
  1633. }
  1634. else
  1635. {
  1636. annotParam = ParamConverter.AnnotConverter(viewerTool.GetCPDFViewer().GetDocument(), annot);
  1637. }
  1638. if (annotParam != null)
  1639. {
  1640. annotParam.AnnotIndex = annot.Page.GetAnnotCount() - 1;
  1641. dynamic expandData = new ExpandoObject();
  1642. expandData.AnnotIndex = annotParam.AnnotIndex;
  1643. expandData.PageIndex = annot.Page.PageIndex;
  1644. expandData.AnnotParam = annotParam;
  1645. return expandData;
  1646. }
  1647. }
  1648. catch (Exception ex)
  1649. {
  1650. }
  1651. }
  1652. return null;
  1653. }
  1654. private void SaveInkCreateAnnotation(ref CPDFAnnotation annotation, List<Point> points)
  1655. {
  1656. // Creation of non-custom stamps
  1657. if (!viewerTool.PDFViewer.GetIsShowStampMouse())
  1658. {
  1659. if (points.Count < 5)
  1660. {
  1661. annotation.RemoveAnnot();
  1662. annotation = null;
  1663. return;
  1664. }
  1665. List<List<CPoint>> inkPathList = new List<List<CPoint>>();
  1666. List<CPoint> inkPath = new List<CPoint>();
  1667. foreach (Point inkNode in points)
  1668. {
  1669. inkPath.Add(new CPoint((float)DpiHelper.StandardNumToPDFNum(inkNode.X), (float)DpiHelper.StandardNumToPDFNum(inkNode.Y)));
  1670. }
  1671. inkPathList.Add(inkPath);
  1672. (annotation as CPDFInkAnnotation).SetInkPath(inkPathList);
  1673. (annotation as CPDFInkAnnotation).UpdateAp();
  1674. }
  1675. else
  1676. {
  1677. Rect stampRect = viewerTool.PDFViewer.GetStampRect();
  1678. Point mousePoint = new Point(
  1679. stampRect.X - stampRect.Width / 2 * viewerTool.GetCPDFViewer().GetZoom(),
  1680. stampRect.Y - stampRect.Height / 2 * viewerTool.GetCPDFViewer().GetZoom()
  1681. );
  1682. viewerTool.GetCPDFViewer().GetPointPageInfo(mousePoint, out int pageindex, out Rect paintRect, out Rect pageBound);
  1683. if (pageindex < 0)
  1684. {
  1685. annotation.RemoveAnnot();
  1686. annotation = null;
  1687. return;
  1688. }
  1689. else
  1690. {
  1691. CPDFPage cPDFPage = viewerTool.GetCPDFViewer().GetDocument().PageAtIndex(pageindex);
  1692. Point cropPoint = new Point();
  1693. if (viewerTool.GetCPDFViewer().GetIsCrop())
  1694. {
  1695. CRect cropRect = cPDFPage.GetCropBounds();
  1696. cropPoint.X = DpiHelper.PDFNumToStandardNum(cropRect.left);
  1697. cropPoint.Y = DpiHelper.PDFNumToStandardNum(cropRect.top);
  1698. }
  1699. // Move the point
  1700. CPoint cPoint = new CPoint(
  1701. (float)((mousePoint.X - pageBound.X + (cropPoint.X * viewerTool.GetCPDFViewer().GetZoom())) / viewerTool.GetCPDFViewer().GetZoom()),
  1702. (float)((mousePoint.Y - pageBound.Y + (cropPoint.Y * viewerTool.GetCPDFViewer().GetZoom())) / viewerTool.GetCPDFViewer().GetZoom()));
  1703. List<List<CPoint>> cPoints = (annotation as CPDFInkAnnotation).InkPath;
  1704. List<List<CPoint>> savePointList = new List<List<CPoint>>();
  1705. foreach (List<CPoint> inkNode in cPoints)
  1706. {
  1707. List<CPoint> savePoints = new List<CPoint>();
  1708. foreach (CPoint addPoint in inkNode)
  1709. {
  1710. savePoints.Add(
  1711. new CPoint(
  1712. (addPoint.x + (float)DpiHelper.StandardNumToPDFNum(cPoint.x)),
  1713. (addPoint.y + (float)DpiHelper.StandardNumToPDFNum(cPoint.y))
  1714. ));
  1715. }
  1716. if (savePoints.Count > 0)
  1717. {
  1718. savePointList.Add(savePoints);
  1719. }
  1720. }
  1721. (annotation as CPDFInkAnnotation).SetInkPath(savePointList);
  1722. (annotation as CPDFInkAnnotation).UpdateAp();
  1723. }
  1724. }
  1725. viewerTool.ClearDrawAnnot();
  1726. viewerTool.GetCPDFViewer().UpdateAnnotFrame();
  1727. InkAnnotHistory inkAnnotHistory = new InkAnnotHistory();
  1728. AnnotParam annotParam = ParamConverter.AnnotConverter(viewerTool.PDFViewer.GetDocument(), cPDFAnnotation);
  1729. annotParam.AnnotIndex = cPDFAnnotation.Page.GetAnnotCount() - 1;
  1730. inkAnnotHistory.CurrentParam = (InkParam)annotParam;
  1731. inkAnnotHistory.PDFDoc = viewerTool.PDFViewer.GetDocument();
  1732. viewerTool.GetCPDFViewer().UndoManager.AddHistory(inkAnnotHistory);
  1733. return;
  1734. }
  1735. #endregion
  1736. /// <summary>
  1737. /// Set mouse pattern when creating content editing
  1738. /// </summary>
  1739. /// <param name="AddTextEditCursor">add text</param>
  1740. /// <param name="AddImageEditCursor">add image</param>
  1741. public void SetAddContentEditCursor(Cursor AddTextEditCursor, Cursor AddImageEditCursor)
  1742. {
  1743. if (AddTextEditCursor == null)
  1744. {
  1745. AddTextEditCursor = Cursors.IBeam;
  1746. }
  1747. if (AddImageEditCursor == null)
  1748. {
  1749. AddImageEditCursor = Cursors.Arrow;
  1750. }
  1751. this.addTextEditCursor = AddTextEditCursor;
  1752. this.addImageEditCursor = AddImageEditCursor;
  1753. }
  1754. private void ViewerTool_MouseMoveHandler(object sender, MouseEventObject e)
  1755. {
  1756. if (viewerTool == null)
  1757. return;
  1758. viewerTool.DrawMoveSelectedMultiRect();
  1759. viewerTool.DrawMovePageSelectedRect();
  1760. if (currentToolType != ToolType.ContentEdit)
  1761. {
  1762. if (e.hitTestType == MouseHitTestType.SelectRect)
  1763. {
  1764. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  1765. {
  1766. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  1767. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  1768. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  1769. };
  1770. Cursor oldCursor = viewerTool.Cursor;
  1771. Cursor newCursor = viewerTool.GetMoveSelectedRectCursor();
  1772. if (oldCursor != newCursor)
  1773. {
  1774. viewerTool.PDFViewer.Cursor = viewerTool.Cursor = newCursor;
  1775. }
  1776. //viewerTool.PDFViewer.Cursor = viewerTool.Cursor= viewerTool.GetMoveSelectedRectCursor();
  1777. if (list.Contains(e.annotType))
  1778. {
  1779. viewerTool.DrawMoveEditAnnot();
  1780. }
  1781. else
  1782. {
  1783. if (e.annotType != C_ANNOTATION_TYPE.C_ANNOTATION_LINK)
  1784. {
  1785. bool tag = viewerTool.DrawMoveSelectedRect();
  1786. if (currentToolType == ToolType.WidgetEdit)
  1787. {
  1788. BaseWidget hitWidget = viewerTool?.GetCPDFViewer()?.AnnotHitTest() as BaseWidget;
  1789. if (hitWidget == null)
  1790. {
  1791. viewerTool.MoveDrawWidget(tag);
  1792. }
  1793. else
  1794. {
  1795. viewerTool.MoveDrawWidget(true);
  1796. }
  1797. }
  1798. }
  1799. }
  1800. }
  1801. else
  1802. {
  1803. if (currentToolType == ToolType.CreateAnnot)
  1804. {
  1805. if (!viewerTool.PDFViewer.GetIsShowStampMouse())
  1806. {
  1807. // Annotation drawing only occurs if the mouse is not set to stamp/data application mode.// Annotation drawing only occurs if the mouse is not set to stamp/data application mode.
  1808. viewerTool.MoveDrawAnnot();
  1809. }
  1810. switch (createAnnotType)
  1811. {
  1812. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  1813. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  1814. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  1815. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  1816. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  1817. viewerTool.DrawMoveSelectText(false);
  1818. break;
  1819. default:
  1820. break;
  1821. }
  1822. }
  1823. else if (currentToolType == ToolType.WidgetEdit)
  1824. {
  1825. BaseWidget hitWidget = viewerTool?.GetCPDFViewer()?.AnnotHitTest() as BaseWidget;
  1826. if (hitWidget == null)
  1827. {
  1828. viewerTool.MoveDrawWidget(false);
  1829. }
  1830. else
  1831. {
  1832. viewerTool.MoveDrawWidget(true);
  1833. }
  1834. }
  1835. else if (e.hitTestType == MouseHitTestType.Unknown && currentToolType != ToolType.WidgetEdit)
  1836. {
  1837. if (currentToolType == ToolType.Customize)
  1838. {
  1839. viewerTool.DrawMoveCustomizeTool();
  1840. }
  1841. else
  1842. {
  1843. TextSelectInfo textInfo = viewerTool.GetTextSelectInfo();
  1844. if ((textInfo.StartPage != -1 && e.mouseButtonEventArgs.LeftButton == MouseButtonState.Pressed) || viewerTool.IsText())
  1845. {
  1846. viewerTool.PDFViewer.Cursor = viewerTool.Cursor = Cursors.IBeam;
  1847. }
  1848. else
  1849. {
  1850. viewerTool.PDFViewer.Cursor = viewerTool.Cursor = Cursors.Arrow;
  1851. }
  1852. }
  1853. }
  1854. }
  1855. if (e.annotType == C_ANNOTATION_TYPE.C_ANNOTATION_NONE)
  1856. {
  1857. viewerTool.DrawMoveSelectText(false);
  1858. }
  1859. }
  1860. else
  1861. {
  1862. if (createContentEditType != CPDFEditType.EditImage)
  1863. {
  1864. Cursor cursor = Cursors.Arrow;
  1865. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(viewerTool.PDFViewer.GetViewForTag(viewerTool.MultiSelectedRectViewTag));
  1866. if (viewerTool.GetLastSelectedRect() != null)
  1867. {
  1868. if (editSelected)
  1869. {
  1870. viewerTool.DrawMoveTextEdit(viewerTool.GetLastSelectedRect(), clickEditSelected);
  1871. if (clickEditSelected)
  1872. cursor = Cursors.IBeam;
  1873. else
  1874. cursor = viewerTool.DrawMoveTest(viewerTool.GetLastSelectedRect());
  1875. }
  1876. else
  1877. {
  1878. cursor = viewerTool.DrawMoveTest(viewerTool.GetLastSelectedRect());
  1879. }
  1880. }
  1881. else
  1882. {
  1883. cursor = viewerTool.DrawMoveTest(viewerTool.GetLastSelectedRect());
  1884. if (multiSelectedRect == null || multiSelectedRect.Children.Count == 0)
  1885. {
  1886. //Selection of mobile drawing logic
  1887. if (e.mouseButtonEventArgs.LeftButton == MouseButtonState.Pressed && createContentEditType == CPDFEditType.None)
  1888. {
  1889. viewerTool.DrawMoveFrameSelect();
  1890. }
  1891. }
  1892. }
  1893. if (cursor == Cursors.Arrow && createContentEditType == CPDFEditType.EditText)
  1894. {
  1895. cursor = addTextEditCursor;
  1896. }
  1897. viewerTool.Cursor = cursor;
  1898. viewerTool.PDFViewer.Cursor = cursor;
  1899. }
  1900. else
  1901. {
  1902. Cursor cursor = Cursors.Arrow;
  1903. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(viewerTool.PDFViewer.GetViewForTag(viewerTool.MultiSelectedRectViewTag));
  1904. if (viewerTool.GetLastSelectedRect() != null)
  1905. {
  1906. if (editSelected)
  1907. {
  1908. viewerTool.DrawMoveTextEdit(viewerTool.GetLastSelectedRect(), clickEditSelected);
  1909. if (clickEditSelected)
  1910. cursor = Cursors.IBeam;
  1911. else
  1912. cursor = viewerTool.DrawMoveTest(viewerTool.GetLastSelectedRect());
  1913. }
  1914. else
  1915. {
  1916. cursor = viewerTool.DrawMoveTest(viewerTool.GetLastSelectedRect());
  1917. }
  1918. }
  1919. else
  1920. {
  1921. cursor = viewerTool.DrawMoveTest(viewerTool.GetLastSelectedRect());
  1922. }
  1923. if (cursor == Cursors.Arrow && createContentEditType == CPDFEditType.EditText)
  1924. {
  1925. cursor = Cursors.IBeam;
  1926. }
  1927. if (cursor == Cursors.Arrow && createContentEditType == CPDFEditType.EditImage)
  1928. {
  1929. cursor = addImageEditCursor;
  1930. }
  1931. viewerTool.Cursor = cursor;
  1932. viewerTool.PDFViewer.Cursor = cursor;
  1933. //viewerTool.Cursor = Cursors.None;
  1934. //viewerTool.PDFViewer.Cursor = Cursors.None;
  1935. }
  1936. }
  1937. MouseMoveHandler?.Invoke(this, e);
  1938. }
  1939. private void ViewerTool_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
  1940. {
  1941. viewerTool.RemoveSelectTextData();
  1942. viewerTool.ReDrawSelectText();
  1943. viewerTool.HideWidgetHitPop();
  1944. switch (e.hitTestType)
  1945. {
  1946. case MouseHitTestType.Unknown:
  1947. {
  1948. if (currentToolType == ToolType.CreateAnnot)
  1949. {
  1950. switch ((e.mouseButtonEventArgs as MouseButtonEventArgs).ClickCount)
  1951. {
  1952. case 1:
  1953. if (viewerTool.IsCanSave())
  1954. {
  1955. cPDFAnnotation = viewerTool.StartDrawAnnot(createAnnotType);
  1956. viewerTool.CreateDefaultAnnot(cPDFAnnotation, createAnnotType, null);
  1957. if (cPDFAnnotation != null && createAnnotType == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  1958. {
  1959. viewerTool.CreateTextBox();
  1960. }
  1961. switch (createAnnotType)
  1962. {
  1963. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  1964. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  1965. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  1966. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  1967. {
  1968. viewerTool.DrawStartSelectText();
  1969. viewerTool.EndDrawAnnot();
  1970. }
  1971. break;
  1972. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  1973. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  1974. if (viewerTool.IsText())
  1975. {
  1976. viewerTool.DrawStartSelectText();
  1977. viewerTool.EndDrawAnnot();
  1978. }
  1979. break;
  1980. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  1981. {
  1982. bool cansave = true;
  1983. CPDFLineAnnotation LineAnnotation = (cPDFAnnotation as CPDFLineAnnotation);
  1984. if (LineAnnotation != null)
  1985. {
  1986. if (LineAnnotation.IsMeasured())
  1987. {
  1988. cansave = false;
  1989. }
  1990. }
  1991. viewerTool.SetIsCanSave(cansave);
  1992. }
  1993. break;
  1994. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  1995. {
  1996. bool cansave = true;
  1997. CPDFPolygonAnnotation PolyAnnotation = (cPDFAnnotation as CPDFPolygonAnnotation);
  1998. if (PolyAnnotation != null)
  1999. {
  2000. cansave = false;
  2001. }
  2002. viewerTool.SetIsCanSave(cansave);
  2003. }
  2004. break;
  2005. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
  2006. {
  2007. bool cansave = true;
  2008. CPDFPolylineAnnotation PolyAnnotation = (cPDFAnnotation as CPDFPolylineAnnotation);
  2009. PolyAnnotation?.IsMeasured();
  2010. if (PolyAnnotation != null)
  2011. {
  2012. if (PolyAnnotation.IsMeasured())
  2013. {
  2014. cansave = false;
  2015. }
  2016. }
  2017. viewerTool.SetIsCanSave(cansave);
  2018. }
  2019. break;
  2020. default:
  2021. break;
  2022. }
  2023. }
  2024. else
  2025. {
  2026. viewerTool.MultipleClick();
  2027. e.IsDrawing = true;
  2028. }
  2029. break;
  2030. case 2:
  2031. if (currentToolType != ToolType.Customize)
  2032. {
  2033. if (!viewerTool.IsCanSave())
  2034. {
  2035. BaseLayer baseLayer = viewerTool.PDFViewer.GetViewForTag(viewerTool.CreateAnnotTag);
  2036. bool canSave = (baseLayer as CreateAnnotTool).IsCanSave();
  2037. viewerTool.SetIsCanSave(canSave);
  2038. }
  2039. }
  2040. break;
  2041. }
  2042. }
  2043. else if (currentToolType == ToolType.WidgetEdit)
  2044. {
  2045. cPDFAnnotation = viewerTool.StartDrawWidget(createWidgetType);
  2046. viewerTool.CreateDefaultWidget(cPDFAnnotation, createWidgetType, null);
  2047. viewerTool?.InvokeWidgetCreated(cPDFAnnotation);
  2048. }
  2049. else if (currentToolType == ToolType.Pan || currentToolType == ToolType.Viewer)
  2050. {
  2051. if (viewerTool.IsText())
  2052. {
  2053. viewerTool.DrawStartSelectText();
  2054. e.hitTestType = MouseHitTestType.Text;
  2055. }
  2056. }
  2057. else if (currentToolType == ToolType.Customize)
  2058. {
  2059. viewerTool.DrawStartCustomizeTool(CustomizeToolType.kErase);
  2060. }
  2061. viewerTool.CleanSelectedRect();
  2062. viewerTool.CleanEditAnnot();
  2063. }
  2064. break;
  2065. case MouseHitTestType.Widget:
  2066. {
  2067. viewerTool.CleanSelectedRect();
  2068. viewerTool.CleanEditAnnot();
  2069. BaseWidget hitWidget = viewerTool?.GetCPDFViewer()?.AnnotHitTest() as BaseWidget;
  2070. if (hitWidget != null)
  2071. {
  2072. viewerTool.ShowFormHitPop(hitWidget);
  2073. }
  2074. }
  2075. break;
  2076. case MouseHitTestType.MultiTextEdit:
  2077. viewerTool.DrawStartSelectedMultiRect();
  2078. break;
  2079. case MouseHitTestType.SelectedPageRect:
  2080. viewerTool.DrawStartPageSelectedRect();
  2081. break;
  2082. default:
  2083. {
  2084. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  2085. {
  2086. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  2087. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  2088. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  2089. };
  2090. if (list.Contains(e.annotType))
  2091. {
  2092. viewerTool.CleanSelectedRect();
  2093. if (!e.IsMersured || !list.Contains(createAnnotType))
  2094. {
  2095. viewerTool.StartDrawEditAnnot();
  2096. }
  2097. }
  2098. else
  2099. {
  2100. if (currentToolType != ToolType.Viewer)
  2101. {
  2102. viewerTool.CleanEditAnnot();
  2103. viewerTool.DrawStartSelectedRect();
  2104. if (currentToolType == ToolType.WidgetEdit)
  2105. {
  2106. viewerTool.MoveDrawWidget(true);
  2107. }
  2108. }
  2109. }
  2110. }
  2111. break;
  2112. }
  2113. switch (currentToolType)
  2114. {
  2115. case ToolType.Pan:
  2116. {
  2117. if (e.annotType == C_ANNOTATION_TYPE.C_ANNOTATION_NONE)
  2118. {
  2119. switch ((e.mouseButtonEventArgs as MouseButtonEventArgs).ClickCount)
  2120. {
  2121. case 2:
  2122. viewerTool.DrawMoveSelectText(true);
  2123. break;
  2124. }
  2125. }
  2126. }
  2127. break;
  2128. case ToolType.ContentEdit:
  2129. {
  2130. editSelected = true;
  2131. clickEditSelected = true;
  2132. switch ((e.mouseButtonEventArgs as MouseButtonEventArgs).ClickCount)
  2133. {
  2134. case 1:
  2135. if (viewerTool.GetIsCropMode())
  2136. {
  2137. //Preconditions for determining crop acquisition points
  2138. viewerTool.HandleTextSelectClick(viewerTool.GetLastSelectedRect(), true);
  2139. clickEditSelected = false;
  2140. MouseLeftButtonDownHandler?.Invoke(this, e);
  2141. }
  2142. break;
  2143. case 2:
  2144. viewerTool.HandleTextSelectClick(viewerTool.GetLastSelectedRect(), true);
  2145. clickEditSelected = false;
  2146. MouseLeftButtonDownHandler?.Invoke(this, e);
  2147. return;
  2148. case 3:
  2149. viewerTool.HandleTextSelectClick(viewerTool.GetLastSelectedRect(), false);
  2150. clickEditSelected = false;
  2151. MouseLeftButtonDownHandler?.Invoke(this, e);
  2152. return;
  2153. }
  2154. if (createContentEditType != CPDFEditType.EditImage)
  2155. {
  2156. viewerTool.DrawTextEditDownEvent(true);
  2157. }
  2158. viewerTool.HideDrawSelectedMultiRect();
  2159. if (viewerTool.GetLastSelectedRect() != null)
  2160. {
  2161. viewerTool.DrawEndFrameSelect();
  2162. Point point = Mouse.GetPosition(viewerTool);
  2163. PointControlType pointControlType = PointControlType.None;
  2164. if (viewerTool.GetIsCropMode())
  2165. {
  2166. //Crop acquisition point judgment
  2167. pointControlType = viewerTool.GetLastSelectedRect().GetHitCropControlIndex(point);
  2168. }
  2169. else
  2170. {
  2171. pointControlType = viewerTool.GetLastSelectedRect().GetHitControlIndex(point);
  2172. }
  2173. EditAreaObject editAreaObject = viewerTool.GetEditAreaObjectForRect(viewerTool.GetLastSelectedRect());
  2174. if (pointControlType != PointControlType.None &&
  2175. (editAreaObject.cPDFEditArea.Type != CPDFEditType.EditText || pointControlType != PointControlType.Body))
  2176. {
  2177. switch (pointControlType)
  2178. {
  2179. case PointControlType.LeftTop:
  2180. case PointControlType.RightBottom:
  2181. viewerTool.PDFViewer.Cursor = viewerTool.Cursor = Cursors.SizeNWSE;
  2182. break;
  2183. case PointControlType.LeftMiddle:
  2184. case PointControlType.RightMiddle:
  2185. viewerTool.PDFViewer.Cursor = viewerTool.Cursor = Cursors.SizeWE;
  2186. break;
  2187. case PointControlType.LeftBottom:
  2188. case PointControlType.RightTop:
  2189. viewerTool.PDFViewer.Cursor = viewerTool.Cursor = Cursors.SizeNESW;
  2190. break;
  2191. case PointControlType.MiddleBottom:
  2192. case PointControlType.MiddleTop:
  2193. viewerTool.PDFViewer.Cursor = viewerTool.Cursor = Cursors.SizeNS;
  2194. break;
  2195. case PointControlType.Line:
  2196. case PointControlType.Body:
  2197. viewerTool.PDFViewer.Cursor = viewerTool.Cursor = Cursors.SizeAll;
  2198. break;
  2199. default:
  2200. break;
  2201. }
  2202. if (e.mouseButtonEventArgs.LeftButton == MouseButtonState.Pressed)
  2203. {
  2204. viewerTool.DrawStartTextEdit(viewerTool.GetLastSelectedRect(), editAreaObject);
  2205. }
  2206. clickEditSelected = false;
  2207. }
  2208. }
  2209. else
  2210. {
  2211. //If it is multiple selection, do not create a new input box
  2212. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(viewerTool.PDFViewer.GetViewForTag(viewerTool.MultiSelectedRectViewTag));
  2213. if (multiSelectedRect != null && multiSelectedRect.Children.Count > 0)
  2214. {
  2215. viewerTool.PDFViewer.UpdateRenderFrame();
  2216. return;
  2217. }
  2218. Point point = Mouse.GetPosition(viewerTool);
  2219. viewerTool.GetCPDFViewer().GetPointPageInfo(point, out int index, out Rect paintRect, out Rect pageBound);
  2220. if (index < 0)
  2221. {
  2222. MouseLeftButtonDownHandler?.Invoke(this, e);
  2223. return;
  2224. }
  2225. if (createContentEditType == CPDFEditType.EditText)
  2226. {
  2227. if (viewerTool.CanAddTextEdit)
  2228. {
  2229. viewerTool.DrawTest(pageBound, index);
  2230. }
  2231. }
  2232. if (createContentEditType == CPDFEditType.None)
  2233. {
  2234. viewerTool.DrawFrameSelect();
  2235. }
  2236. else
  2237. {
  2238. viewerTool.DrawEndFrameSelect();
  2239. }
  2240. clickEditSelected = false;
  2241. }
  2242. }
  2243. break;
  2244. default:
  2245. break;
  2246. }
  2247. MouseLeftButtonDownHandler?.Invoke(this, e);
  2248. }
  2249. private void ViewerTool_MouseRightButtonDownHandler(object sender, MouseEventObject e)
  2250. {
  2251. if (e.mouseButtonEventArgs != null)
  2252. {
  2253. viewerTool?.SetPastePoint(e.mouseButtonEventArgs.GetPosition(viewerTool));
  2254. }
  2255. MouseRightButtonDownHandler?.Invoke(sender, e);
  2256. }
  2257. private Rect RotateRect90(Rect rect)
  2258. {
  2259. double centerX = rect.Left + rect.Width / 2;
  2260. double centerY = rect.Top + rect.Height / 2;
  2261. double newWidth = rect.Height;
  2262. double newHeight = rect.Width;
  2263. double newLeft = centerX - newWidth / 2.0;
  2264. double newTop = centerY - newHeight / 2.0;
  2265. return new Rect(newLeft, newTop, newWidth, newHeight);
  2266. }
  2267. }
  2268. }