CPDFToolManager.cs 114 KB

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