CPDFToolManager.cs 114 KB

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