CPDFToolManager.cs 114 KB

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