CPDFToolManager.cs 116 KB

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