CPDFToolManager.cs 113 KB

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