CPDFToolManager.cs 116 KB

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