CPDFToolManager.cs 120 KB

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