CPDFToolManager.cs 109 KB

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