CPDFToolManager.cs 114 KB

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