CPDFToolManager.cs 115 KB

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