CPDFToolManager.cs 114 KB

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