CPDFToolManager.cs 112 KB

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