CPDFToolManager.cs 113 KB

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