CPDFToolManager.cs 98 KB

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