CPDFToolManager.cs 113 KB

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