AnnotToolContentViewModel.Command.cs 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument.Action;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKitViewer;
  5. using ComPDFKitViewer.AnnotEvent;
  6. using ComPDFKitViewer.PdfViewer;
  7. using PDF_Office.CustomControl;
  8. using PDF_Office.EventAggregators;
  9. using PDF_Office.Helper;
  10. using PDF_Office.Properties;
  11. using PDF_Office.ViewModels.BOTA;
  12. using PDF_Office.Views.BOTA;
  13. using PDFSettings;
  14. using Prism.Mvvm;
  15. using Prism.Regions;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using System.Windows;
  22. using System.Windows.Controls;
  23. using System.Windows.Controls.Primitives;
  24. using System.Windows.Media;
  25. using System.Diagnostics;
  26. using PDF_Office.Model.AnnotPanel;
  27. using System.Windows.Input;
  28. namespace PDF_Office.ViewModels.Tools
  29. {
  30. public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware
  31. {
  32. #region 事件绑定和解绑
  33. private void BindingEvent()
  34. {
  35. //属性面板与注释工具栏绑定:属性面板的属性变化,会同步注释工具栏的属性值
  36. //比如在属性面板里更改注释颜色,会同时更新工具栏对应的工具颜色
  37. propertyPanel.DataChanged -= AnnotPropertyPanel_DataChanged;
  38. propertyPanel.DataChanged += AnnotPropertyPanel_DataChanged;
  39. propertyPanel.DefaultStored -= AnnotProperty_DefaultStored;
  40. propertyPanel.DefaultStored += AnnotProperty_DefaultStored;
  41. propertyPanel.AnnotTypeChanged -= AnnotPropertyPanel_AnnotTypeChanged;
  42. propertyPanel.AnnotTypeChanged += AnnotPropertyPanel_AnnotTypeChanged;
  43. //快捷键
  44. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  45. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  46. }
  47. private void UnBindingEvent()
  48. {
  49. propertyPanel.DataChanged -= AnnotPropertyPanel_DataChanged;
  50. propertyPanel.DefaultStored -= AnnotProperty_DefaultStored;
  51. propertyPanel.AnnotTypeChanged -= AnnotPropertyPanel_AnnotTypeChanged;
  52. //快捷盘解绑
  53. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  54. }
  55. private void BindingPDFViewerHandler()
  56. {
  57. //来自PDFViewer的响应事件
  58. if (PDFViewer != null)
  59. {
  60. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  61. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  62. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  63. PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  64. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  65. PDFViewer.AnnotCommandHandler += PDFViewer_AnnotCommandHandler;
  66. PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
  67. PDFViewer.WidgetClickHander += PDFViewer_WidgetClickHander;
  68. PDFViewer.SnapshotCommandHandler -= PDFViewer_SnapshotCommandHandler;
  69. PDFViewer.SnapshotCommandHandler += PDFViewer_SnapshotCommandHandler;
  70. PDFViewer.PDFActionHandler -= PDFViewer_PDFActionHandler;
  71. PDFViewer.PDFActionHandler += PDFViewer_PDFActionHandler;
  72. PDFViewer.AnnotHoverHandler -= PDFViewer_AnnotHoverHandler;
  73. PDFViewer.AnnotHoverHandler += PDFViewer_AnnotHoverHandler;
  74. PDFViewer.PreviewMouseLeftButtonDown -= PDFViewer_PreviewMouseLeftButtonDown;
  75. PDFViewer.PreviewMouseLeftButtonDown += PDFViewer_PreviewMouseLeftButtonDown;
  76. }
  77. }
  78. private void PDFViewer_PDFActionHandler(object sender, ComPDFKit.PDFDocument.Action.CPDFAction action)
  79. {
  80. if (action == null)
  81. {
  82. return;
  83. }
  84. switch (action.ActionType)
  85. {
  86. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  87. if (PDFViewer != null)
  88. {
  89. CPDFGoToAction gotoAction = action as CPDFGoToAction;
  90. CPDFDestination dest = gotoAction.GetDestination(PDFViewer.Document);
  91. if (dest != null)
  92. {
  93. PDFViewer.GoToPage(dest.PageIndex, new System.Windows.Point(0, 0));
  94. }
  95. }
  96. break;
  97. case C_ACTION_TYPE.ACTION_TYPE_URI:
  98. {
  99. CPDFUriAction uriAction = action as CPDFUriAction;
  100. string uri = uriAction.GetUri();
  101. try
  102. {
  103. if (!string.IsNullOrEmpty(uri))
  104. {
  105. Process.Start(uri);
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. }
  111. }
  112. break;
  113. }
  114. }
  115. private void UnBindingPDFViewerHandler()
  116. {
  117. if (PDFViewer != null)
  118. {
  119. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  120. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  121. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  122. PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
  123. PDFViewer.SnapshotCommandHandler -= PDFViewer_SnapshotCommandHandler;
  124. PDFViewer.PDFActionHandler -= PDFViewer_PDFActionHandler;
  125. PDFViewer.AnnotHoverHandler -= PDFViewer_AnnotHoverHandler;
  126. PDFViewer.PreviewMouseLeftButtonDown -= PDFViewer_PreviewMouseLeftButtonDown;
  127. }
  128. }
  129. #endregion 事件绑定和解绑
  130. #region 与触发事件调用相关的函数
  131. //鼠标左键双击注释
  132. private void PDFViewer_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  133. {
  134. if (e.ClickCount == 2)
  135. {
  136. if (CurrentSelectedAnnot != null)
  137. {
  138. var type = CurrentSelectedAnnot.EventType;
  139. if (type != AnnotArgsType.AnnotSticky && type != AnnotArgsType.AnnotFreeText &&
  140. type != AnnotArgsType.AnnotHighlight && type != AnnotArgsType.AnnotUnderline && type != AnnotArgsType.AnnotStrikeout)
  141. PopAnnotNoteText(CurrentSelectedAnnot);
  142. }
  143. }
  144. }
  145. //是否为形状注释
  146. private bool isShapAnnot(AnnotHandlerEventArgs annot)
  147. {
  148. if (annot.EventType == AnnotArgsType.AnnotCircle ||
  149. annot.EventType == AnnotArgsType.AnnotSquare ||
  150. annot.EventType == AnnotArgsType.AnnotLine
  151. )
  152. {
  153. return true;
  154. }
  155. else
  156. {
  157. return false;
  158. }
  159. }
  160. //是否为高亮注释
  161. private bool isHightAnnot(AnnotHandlerEventArgs annot)
  162. {
  163. if (annot.EventType == AnnotArgsType.AnnotUnderline ||
  164. annot.EventType == AnnotArgsType.AnnotSquiggly ||
  165. annot.EventType == AnnotArgsType.AnnotHighlight ||
  166. annot.EventType == AnnotArgsType.AnnotStrikeout
  167. )
  168. {
  169. return true;
  170. }
  171. else
  172. {
  173. return false;
  174. }
  175. }
  176. private void GetSelectedAnnots(AnnotAttribEvent e)
  177. {
  178. var annot = e.AnnotItemsList[0];
  179. switch (annot.EventType)
  180. {
  181. case AnnotArgsType.AnnotHighlight:
  182. GetHighLight(e.AnnotItemsList);
  183. break;
  184. case AnnotArgsType.AnnotUnderline:
  185. GetUnderLine(e.AnnotItemsList);
  186. break;
  187. case AnnotArgsType.AnnotStrikeout:
  188. GetStrikeout(e.AnnotItemsList);
  189. break;
  190. case AnnotArgsType.AnnotSquiggly:
  191. GetSquiggly(e.AnnotItemsList);
  192. break;
  193. case AnnotArgsType.AnnotFreehand:
  194. GetFreehand(e.AnnotItemsList);
  195. break;
  196. case AnnotArgsType.AnnotFreeText:
  197. GetFreetext(e.AnnotItemsList);
  198. break;
  199. case AnnotArgsType.AnnotSquare:
  200. GetRect(e.AnnotItemsList);
  201. break;
  202. case AnnotArgsType.AnnotCircle:
  203. GetCircle(e.AnnotItemsList);
  204. break;
  205. case AnnotArgsType.AnnotLine:
  206. bool isLine = true;
  207. if (e.Attribs.ContainsKey(AnnotAttrib.LineStart))
  208. {
  209. if ((C_LINE_TYPE)e.Attribs[AnnotAttrib.LineStart] != C_LINE_TYPE.LINETYPE_UNKNOWN && (C_LINE_TYPE)e.Attribs[AnnotAttrib.LineStart] != C_LINE_TYPE.LINETYPE_NONE)
  210. {
  211. isLine = false;
  212. }
  213. }
  214. if (e.Attribs.ContainsKey(AnnotAttrib.LineEnd))
  215. {
  216. if ((C_LINE_TYPE)e.Attribs[AnnotAttrib.LineEnd] != C_LINE_TYPE.LINETYPE_UNKNOWN && (C_LINE_TYPE)e.Attribs[AnnotAttrib.LineEnd] != C_LINE_TYPE.LINETYPE_NONE)
  217. {
  218. isLine = false;
  219. }
  220. }
  221. if (isLine)
  222. GetArrowLine("Line", e.AnnotItemsList);
  223. else
  224. GetArrowLine("Arrow", e.AnnotItemsList);
  225. break;
  226. case AnnotArgsType.AnnotLink:
  227. //viewContentViewModel.IsCreateLink = false;
  228. //e.IsAnnotCreateReset= true;
  229. if (btnLinkIsChecked == false)
  230. {
  231. propertyPanel.IsAddLink = false;
  232. }
  233. GetLink(e.AnnotItemsList, e);
  234. break;
  235. case AnnotArgsType.AnnotSticky:
  236. GetStickyNote(e.AnnotItemsList);
  237. customStickyPopup.GetCurrentAnnot = e.AnnotItemsList[0] as StickyAnnotArgs;
  238. customStickyPopup.GetPDFViewer = PDFViewer;
  239. break;
  240. case AnnotArgsType.AnnotStamp:
  241. GetStamp();
  242. break;
  243. }
  244. }
  245. #endregion 与触发事件调用相关的函数
  246. #region PDFViewer事件
  247. private AnnotHandlerEventArgs CurrentSelectedAnnot = null;
  248. //选中和非选中注释
  249. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  250. {
  251. if (e != null)
  252. {
  253. var annot = e.AnnotItemsList[0];
  254. if (annot != null)
  255. {
  256. if (e.AnnotItemsList.Count == 1)
  257. {
  258. //IsAnnotCreateReset:是否为创建注释的状态
  259. if (e.IsAnnotCreateReset == false)
  260. {
  261. GetSelectedAnnots(e);
  262. CurrentSelectedAnnot = annot;
  263. }
  264. else
  265. {
  266. switch (annot.EventType)
  267. {
  268. case AnnotArgsType.AnnotLink:
  269. //viewContentViewModel.IsCreateLink = false;
  270. GetLink(e.AnnotItemsList, e);
  271. PDFViewer.SetToolParam(annot);
  272. break;
  273. }
  274. //TODO: 修改注释后,会作用到之后添加的注释中。因此先把此逻辑“创建注释后,会自动回到默认值”注释掉
  275. //注释代码原因:设计已经重新调整逻辑:所有注释创建后,编辑属性,所有属性会作用于下次创建的注释中。
  276. //if (annot.EventType != AnnotArgsType.AnnotStrikeout &&
  277. //annot.EventType != AnnotArgsType.AnnotUnderline &&
  278. //annot.EventType != AnnotArgsType.AnnotHighlight &&
  279. //annot.EventType != AnnotArgsType.AnnotSquiggly &&
  280. //annot.EventType != AnnotArgsType.AnnotLink &&
  281. //annot.EventType != AnnotArgsType.AnnotFreehand &&
  282. //annot.EventType != AnnotArgsType.AnnotSticky &&
  283. //)
  284. //{
  285. // if (ToolExpandDict.ContainsValue(e.AnnotItemsList[0].EventType))
  286. // {
  287. // var strLineAnnotTag = "";
  288. // if (e.AnnotItemsList[0] is LineAnnotArgs)
  289. // {
  290. // var lineAnnot = e.AnnotItemsList[0] as LineAnnotArgs;
  291. // if (lineAnnot.HeadLineType == C_LINE_TYPE.LINETYPE_NONE && lineAnnot.TailLineType == C_LINE_TYPE.LINETYPE_NONE)
  292. // {
  293. // strLineAnnotTag = "Line";
  294. // }
  295. // else
  296. // {
  297. // strLineAnnotTag = "Arrow";
  298. // }
  299. // }
  300. // foreach (var item in ToolExpandDict)
  301. // {
  302. // if (item.Value == e.AnnotItemsList[0].EventType)
  303. // {
  304. // annot = null;//新建注释时,回到默认值
  305. // if (string.IsNullOrEmpty(strLineAnnotTag))
  306. // {
  307. // FindAnnotTypeKey(item.Key, ref annot);
  308. // break;
  309. // }
  310. // else
  311. // {
  312. // if (strLineAnnotTag == item.Key)
  313. // {
  314. // FindAnnotTypeKey(item.Key, ref annot);
  315. // break;
  316. // }
  317. // }
  318. // }
  319. // }
  320. // PDFViewer.SetToolParam(annot);
  321. // }
  322. //}
  323. #region TO DO
  324. //设计重新调整,阅读页空白处,右键菜单,添加链接需要显示,其他和pro mac一样的效果,不显示属性栏
  325. //if (isRightMenuAddAnnot && annot.EventType!=AnnotArgsType.AnnotLink)
  326. //{
  327. // ShowPropertyPanel(false);
  328. //}
  329. //else
  330. //{
  331. // ShowPropertyPanel();
  332. //}
  333. #endregion TO DO
  334. }
  335. }
  336. else
  337. {
  338. bool isDifferentAnnotTyle = false;
  339. var lastAnnot = annot;
  340. foreach (var item in e.AnnotItemsList)
  341. {
  342. if (lastAnnot.EventType != item.EventType)
  343. {
  344. if ((isShapAnnot(annot) == true && isShapAnnot(item) == true) || (isHightAnnot(annot) == true && isHightAnnot(item) == true))
  345. {
  346. lastAnnot = item;
  347. continue;
  348. }
  349. lastAnnot = item;
  350. isDifferentAnnotTyle = true;
  351. break;
  352. }
  353. }
  354. if (isDifferentAnnotTyle)
  355. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  356. else
  357. GetSelectedAnnots(e);
  358. }
  359. }
  360. //注释列表同步选中
  361. var list = e.GetPageAnnotsIndex();
  362. bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  363. if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true && bOTAContent.TabItemAnnotation.IsSelected == true && list != null && list.Count > 0)
  364. {
  365. if (list.Keys.Count == 0)
  366. {
  367. return;
  368. }
  369. var pageindex = new List<int>(list.Keys);
  370. List<int> annotes = new List<int>();
  371. list.TryGetValue(pageindex[0], out annotes);
  372. int annoteindex = annotes[0];
  373. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  374. if (viewModel != null)
  375. {
  376. viewModel.ScrollToAnnot(pageindex[0], annoteindex, annotation.AnnotationList);
  377. }
  378. }
  379. }
  380. else
  381. {
  382. if (BtnLinkIsChecked == false)
  383. {
  384. if (PDFViewer.MouseMode != MouseModes.AnnotCreate)
  385. {
  386. if (PDFViewer.MouseMode == MouseModes.PanTool && propertyPanel.IsAddLink == false && propertyPanel.IsLocationLink == false)
  387. {
  388. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  389. }
  390. }
  391. }
  392. }
  393. }
  394. //在注释工具的状态下,右键菜单
  395. private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  396. {
  397. if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool))
  398. return;
  399. switch (e.CommandType)
  400. {
  401. case CommandType.Context:
  402. if (e.AnnotEventArgsList.Count > 0)
  403. {
  404. if (App.mainWindowViewModel.SelectedItem.IsInReadctonMode && e.AnnotEventArgsList[0].EventType == AnnotArgsType.AnnotRedaction)
  405. {
  406. //绑定标记密文处右键菜单
  407. events.GetEvent<RedactionCommandEvent>().Publish(new RedactionCommandEventArgs() { UniCode = App.mainWindowViewModel.SelectedItem.Unicode, Sender = sender, args = e });
  408. }
  409. else
  410. {
  411. if (e.AnnotEventArgsList.Count == 1)
  412. {
  413. var selectedAnnot = e.AnnotEventArgsList[0];
  414. PopMenuCheckedLineDash(selectedAnnot);
  415. PopMenuCheckedFontFamily(selectedAnnot);
  416. PopMenuCheckedTextAglin(selectedAnnot);
  417. switch (selectedAnnot.EventType)
  418. {
  419. case AnnotArgsType.AnnotHighlight:
  420. case AnnotArgsType.AnnotUnderline:
  421. case AnnotArgsType.AnnotStrikeout:
  422. case AnnotArgsType.AnnotSquiggly:
  423. if (selectedAnnot.EventType == AnnotArgsType.AnnotHighlight)
  424. {
  425. colorContent.ItemSource = AnnotColorList.GetColorList(ColorSelectorType.Highlight);
  426. }
  427. else
  428. {
  429. colorContent.ItemSource = AnnotColorList.GetColorList(ColorSelectorType.Border);
  430. }
  431. e.PopupMenu = HightAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  432. break;
  433. case AnnotArgsType.AnnotFreehand:
  434. e.PopupMenu = FreeHandAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  435. break;
  436. case AnnotArgsType.AnnotFreeText:
  437. e.PopupMenu = FreeTextAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  438. break;
  439. case AnnotArgsType.AnnotSticky:
  440. e.PopupMenu = StrickNoteAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  441. break;
  442. case AnnotArgsType.AnnotSquare:
  443. case AnnotArgsType.AnnotCircle:
  444. ShapeAnnotPopMenu.SetVisual("ShapeDirect", false);
  445. ShapeAnnotPopMenu.SetVisual("ShapeFillColor", true);
  446. e.PopupMenu = ShapeAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  447. break;
  448. case AnnotArgsType.AnnotLine:
  449. ShapeAnnotPopMenu.SetVisual("ShapeFillColor", false);
  450. ShapeAnnotPopMenu.SetVisual("ShapeDirect", true);
  451. e.PopupMenu = ShapeAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  452. break;
  453. case AnnotArgsType.AnnotLink:
  454. e.PopupMenu = LinkAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  455. break;
  456. case AnnotArgsType.AnnotStamp:
  457. e.PopupMenu = StampAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  458. break;
  459. }
  460. }
  461. else
  462. {
  463. bool isHigh = true;//是否为高亮
  464. foreach (var item in e.AnnotEventArgsList)
  465. {
  466. if (isHightAnnot(item) == false)
  467. {
  468. isHigh = false;
  469. break;
  470. }
  471. }
  472. MultiAnnotPopMenu.SetVisual("MultiCopy", !isHigh);
  473. MultiAnnotPopMenu.SetVisual("MultiCut", !isHigh);
  474. e.PopupMenu = MultiAnnotPopMenu.OpenMenu(e.AnnotEventArgsList, sender);//SelectMultiAnnotMenu(e.AnnotEventArgsList, isHigh);
  475. }
  476. }
  477. if (e.PopupMenu != null)
  478. {
  479. e.Handle = true;
  480. }
  481. }
  482. else
  483. {
  484. if (e.PressOnSelectedText || e.CommandTarget == TargetType.ImageSelection)
  485. {
  486. e.PopupMenu = SelectedTextOrImageContextMenu(sender, e);
  487. if (e.PopupMenu != null)
  488. {
  489. e.Handle = true;
  490. }
  491. }
  492. else
  493. {
  494. e.PopupMenu = ViewerContextMenu(sender);
  495. if (e.PopupMenu != null)
  496. {
  497. e.Handle = true;
  498. }
  499. }
  500. }
  501. break;
  502. }
  503. }
  504. //右键菜单,选中字体样式按钮状态
  505. private void PopMenuCheckedFontFamily(AnnotHandlerEventArgs annot)
  506. {
  507. if (annot != null)
  508. {
  509. if (annot.EventType == AnnotArgsType.AnnotFreeText)
  510. {
  511. var freeText = annot as FreeTextAnnotArgs;
  512. FreeTextAnnotPopMenu.SetIsChecked(freeText.FontFamily.ToString(), true);
  513. }
  514. }
  515. }
  516. //右键菜单,选中文本内容对齐按钮状态
  517. private void PopMenuCheckedTextAglin(AnnotHandlerEventArgs annot)
  518. {
  519. if (annot != null)
  520. {
  521. if (annot.EventType == AnnotArgsType.AnnotFreeText)
  522. {
  523. var freeText = annot as FreeTextAnnotArgs;
  524. switch (freeText.Align)
  525. {
  526. case TextAlignment.Left:
  527. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinLeft", true);
  528. break;
  529. case TextAlignment.Center:
  530. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinCenter", true);
  531. break;
  532. case TextAlignment.Right:
  533. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinRight", true);
  534. break;
  535. case TextAlignment.Justify:
  536. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinJustify", true);
  537. break;
  538. default:
  539. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinLeft", false);
  540. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinCenter", false);
  541. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinRight", false);
  542. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinJustify", false);
  543. break;
  544. }
  545. }
  546. }
  547. }
  548. //右键菜单,选中虚实线按钮状态
  549. private void PopMenuCheckedLineDash(AnnotHandlerEventArgs annot)
  550. {
  551. if (annot != null)
  552. {
  553. bool isSolidLineDash = true;
  554. if (annot.EventType == AnnotArgsType.AnnotFreehand)
  555. {
  556. var freeHand = annot as FreehandAnnotArgs;
  557. isSolidLineDash = AnnotPropertyPanel.IsSolidStyle(freeHand.LineDash);
  558. FreeHandAnnotPopMenu.SetIsChecked(isSolidLineDash ? "FreeHandSolid" : "FreeHandDash", true);
  559. }
  560. else if (annot.EventType == AnnotArgsType.AnnotSquare)
  561. {
  562. var square = annot as SquareAnnotArgs;
  563. isSolidLineDash = AnnotPropertyPanel.IsSolidStyle(square.LineDash);
  564. ShapeAnnotPopMenu.SetIsChecked(isSolidLineDash ? "ShapeSolid" : "ShapeDash", true);
  565. }
  566. else if (annot.EventType == AnnotArgsType.AnnotCircle)
  567. {
  568. var circle = annot as CircleAnnotArgs;
  569. isSolidLineDash = AnnotPropertyPanel.IsSolidStyle(circle.LineDash);
  570. ShapeAnnotPopMenu.SetIsChecked(isSolidLineDash ? "ShapeSolid" : "ShapeDash", true);
  571. }
  572. else if (annot.EventType == AnnotArgsType.AnnotLine)
  573. {
  574. var line = annot as LineAnnotArgs;
  575. isSolidLineDash = AnnotPropertyPanel.IsSolidStyle(line.LineDash);
  576. ShapeAnnotPopMenu.SetIsChecked(isSolidLineDash ? "ShapeSolid" : "ShapeDash", true);
  577. }
  578. }
  579. }
  580. /// <summary>
  581. /// 内容选择工具
  582. /// </summary>
  583. /// <param name="sender"></param>
  584. /// <param name="e"></param>
  585. private void PDFViewer_SnapshotCommandHandler(object sender, SnapshotCommandArgs e)
  586. {
  587. SnapshotEditToolArgs snapToolArgs = (SnapshotEditToolArgs)PDFViewer.ToolManager.CurrentAnnotArgs;
  588. SnapshotEditMenuViewModel.SnapToolArgs = snapToolArgs;
  589. SnapshotEditMenuViewModel.PDFViewer = PDFViewer;
  590. SnapshotEditMenuViewModel.SnapToolEvent -= SnapshotEditMenuViewModel_SnapToolEvent; ;
  591. SnapshotEditMenuViewModel.SnapToolEvent += SnapshotEditMenuViewModel_SnapToolEvent;
  592. var popMenu = App.Current.FindResource("SnapshotContextMenu") as ContextMenu;
  593. if (e.HitSnapshotTool && e.SnapshotRect.Width > 0 && e.SnapshotRect.Height > 0)
  594. {
  595. e.PopupMenu = popMenu;
  596. e.Handle = true;
  597. if (popMenu != null && popMenu.Items.Count == 5)
  598. {
  599. //复制
  600. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  601. menuItem.CommandTarget = PDFViewer;
  602. menuItem.Command = SnapshotEditMenuViewModel.SnapCopyCommand;
  603. //导出
  604. menuItem = popMenu.Items[1] as MenuItem;
  605. menuItem.CommandTarget = PDFViewer;
  606. if (menuItem.Items.Count == 3)
  607. {
  608. MenuItem menuItem1 = menuItem.Items[0] as MenuItem;
  609. menuItem1.CommandTarget = PDFViewer;
  610. menuItem1.Command = SnapshotEditMenuViewModel.ExportPNGCommand;
  611. menuItem1 = menuItem.Items[1] as MenuItem;
  612. menuItem1.CommandTarget = PDFViewer;
  613. menuItem1.Command = SnapshotEditMenuViewModel.ExportJPGCommand;
  614. menuItem1 = menuItem.Items[2] as MenuItem;
  615. menuItem1.CommandTarget = PDFViewer;
  616. menuItem1.Command = SnapshotEditMenuViewModel.ExportPDFCommand;
  617. }
  618. //裁剪
  619. menuItem = popMenu.Items[2] as MenuItem;
  620. menuItem.CommandTarget = PDFViewer;
  621. menuItem.Command = SnapshotEditMenuViewModel.CroppingCommand;
  622. //缩放至所选区域
  623. menuItem = popMenu.Items[3] as MenuItem;
  624. menuItem.CommandTarget = PDFViewer;
  625. menuItem.Visibility = Visibility.Collapsed;
  626. //menuItem.Command = snapshotEditMenuViewModel.CroppingCommand;
  627. //打印
  628. menuItem = popMenu.Items[4] as MenuItem;
  629. menuItem.CommandTarget = PDFViewer;
  630. menuItem.Command = SnapshotEditMenuViewModel.PrintCommand;
  631. }
  632. }
  633. }
  634. private void SnapshotEditMenuViewModel_SnapToolEvent(object sender, KeyValuePair<string, object> e)
  635. {
  636. switch (e.Key)
  637. {
  638. case "CloseSnap":
  639. {
  640. #region to do
  641. //var item = PDFViewerTab.SelectedItem as TabItem;
  642. //if (item == null)
  643. //{
  644. // ClearSelectedToolPanel();
  645. // return;
  646. //}
  647. //Grid grid = item.Content as Grid;
  648. //if (grid == null || grid.Children.Count == 0)
  649. //{
  650. // ClearSelectedToolPanel();
  651. // return;
  652. //}
  653. //PDFViewerCtrl pdfViewer = grid.Children[0] as PDFViewerCtrl;
  654. //if (pdfViewer == null)
  655. //{
  656. // ClearSelectedToolPanel();
  657. // return;
  658. //}
  659. #endregion to do
  660. switch (PDFViewer.MouseMode)
  661. {
  662. case MouseModes.SelectTextTool:
  663. break;
  664. default:
  665. BtnSelecttoolIsChecked = false;
  666. BtnHandIsChecked = true;
  667. PDFViewer.EnableZoom(true);
  668. PDFViewer.EnableScroll(true);
  669. break;
  670. }
  671. }
  672. break;
  673. default:
  674. break;
  675. }
  676. }
  677. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  678. {
  679. if (e != null && e.Count > 0)
  680. {
  681. for (int i = 0; i < e.Count; i++)
  682. {
  683. AnnotEditEvent editEvent = e[i];
  684. switch (editEvent.EditAction)
  685. {
  686. case ActionType.Add:
  687. bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  688. if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true)
  689. {
  690. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  691. if (viewModel != null)
  692. {
  693. int pageindex = editEvent.PageIndex;
  694. int annotindex = editEvent.AnnotIndex;
  695. viewModel.UpdateAddedAnnot(pageindex, annotindex);
  696. }
  697. }
  698. var annot = e[0].EditAnnotArgs;
  699. if (
  700. annot.EventType == AnnotArgsType.AnnotSquare ||
  701. annot.EventType == AnnotArgsType.AnnotCircle ||
  702. annot.EventType == AnnotArgsType.AnnotLine ||
  703. annot.EventType == AnnotArgsType.AnnotStamp
  704. )
  705. {
  706. PDFViewer.ClearSelectAnnots();
  707. PDFViewer.SelectAnnotation(annot.PageIndex, annot.AnnotIndex);
  708. }
  709. break;
  710. case ActionType.Del:
  711. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  712. if (isTabItemAnnotation)
  713. {
  714. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  715. if (viewModel != null)
  716. {
  717. int pageindex = editEvent.PageIndex;
  718. int annotindex = editEvent.AnnotIndex;
  719. viewModel.UpdateModifiedAnnot(pageindex, annotindex, true);
  720. }
  721. }
  722. break;
  723. case ActionType.Modify:
  724. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  725. if (bOTAContent.TabItemAnnotation.IsSelected)
  726. {
  727. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  728. if (viewModel != null)
  729. {
  730. int pageindex = editEvent.PageIndex;
  731. int annotindex = editEvent.AnnotIndex;
  732. viewModel.UpdateModifiedAnnot(pageindex, annotindex, false);
  733. }
  734. }
  735. break;
  736. case ActionType.TextEdit:
  737. break;
  738. default:
  739. break;
  740. }
  741. }
  742. //空白处 右键菜单 添加注释 为单次添加,不连续添加
  743. if (IsNoSelectMenu)
  744. {
  745. IsNoSelectMenu = false;
  746. PDFViewer.SetMouseMode(MouseModes.PanTool);
  747. }
  748. }
  749. }
  750. private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
  751. {
  752. }
  753. private void PDFViewer_AnnotHoverHandler(object sender, AnnotHoverData e)
  754. {
  755. if (e != null && e.DrawContext != null)
  756. {
  757. Rect hoverRect = new Rect(
  758. e.PaintRect.Left - 2,
  759. e.PaintRect.Top - 2,
  760. e.PaintRect.Width + 4,
  761. e.PaintRect.Height + 4);
  762. Pen hoverPen = new Pen(new SolidColorBrush(Color.FromArgb(0xff, 0x11, 0x8A, 0xff)), 1);
  763. hoverPen.DashStyle = DashStyles.Dash;
  764. e.DrawContext?.DrawRectangle(null, hoverPen, hoverRect);
  765. //便签,显示ToolTip内容
  766. if (e.Annot != null && e.Annot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_TEXT)
  767. {
  768. if (PDFViewer.ToolTip == null)
  769. {
  770. var content = e.Annot.GetContent();
  771. if (string.IsNullOrEmpty(content) == false)
  772. {
  773. ToolTip TipChild = new ToolTip();
  774. TipChild.Style = App.Current.Resources["FlowToolTip"] as Style;
  775. TipChild.MaxWidth = 246;
  776. TipChild.Content = content;
  777. TipChild.Visibility = Visibility.Visible;
  778. TipChild.IsOpen = true;
  779. TipChild.Placement = PlacementMode.Right;
  780. var rec = e.Annot.GetRect();
  781. TipChild.PlacementRectangle = new Rect(rec.left, rec.top, rec.right, rec.bottom);
  782. TipChild.PlacementTarget = PDFViewer.Parent as ContentControl;
  783. TipChild.Placement = PlacementMode.MousePoint;
  784. PDFViewer.ToolTip = TipChild;
  785. oldRect = hoverRect;
  786. PDFViewer.MouseMove -= PDFViewer_MouseMove;
  787. PDFViewer.MouseMove += PDFViewer_MouseMove;
  788. return;
  789. }
  790. }
  791. }
  792. }
  793. CloseAnnotToolTip();
  794. }
  795. private Rect oldRect = new Rect(0, 0, 0, 0);
  796. private void CloseAnnotToolTip()
  797. {
  798. if (PDFViewer.ToolTip != null && PDFViewer.ToolTip is ToolTip)
  799. {
  800. ToolTip oldTips = (ToolTip)PDFViewer.ToolTip;
  801. oldTips.IsOpen = false;
  802. oldTips.Visibility = Visibility.Collapsed;
  803. PDFViewer.ToolTip = null;
  804. PDFViewer.MouseMove -= PDFViewer_MouseMove;
  805. oldRect = new Rect(0, 0, 0, 0);
  806. }
  807. }
  808. private void PDFViewer_MouseMove(object sender, MouseEventArgs e)
  809. {
  810. if (sender != null && PDFViewer.ToolTip != null && PDFViewer.Parent != null)
  811. {
  812. var newPoint = e.GetPosition(PDFViewer.Parent as ContentControl);
  813. var isOutw = newPoint.X > (oldRect.X + oldRect.Width + 4);
  814. var isOuth = newPoint.Y > (oldRect.Y + oldRect.Height + 4);
  815. if (newPoint.X < oldRect.X || newPoint.Y < oldRect.Y || isOutw || isOuth)
  816. {
  817. CloseAnnotToolTip();
  818. }
  819. }
  820. }
  821. #endregion PDFViewer事件
  822. #region BindingEvent事件
  823. private void AnnotProperty_DefaultStored(object sender, object e)
  824. {
  825. }
  826. //同一属性面板,不同的注释类型
  827. private void AnnotPropertyPanel_AnnotTypeChanged(object sender, Dictionary<AnnotArgsType, object> e)
  828. {
  829. if (e != null)
  830. {
  831. AnnotHandlerEventArgs annotArgs = null;
  832. foreach (AnnotArgsType argsType in e.Keys)
  833. {
  834. switch (argsType)
  835. {
  836. case AnnotArgsType.AnnotSquare:
  837. annotArgs = GetRect();
  838. break;
  839. case AnnotArgsType.AnnotCircle:
  840. annotArgs = GetCircle();
  841. break;
  842. case AnnotArgsType.AnnotLine:
  843. var LineTag = e[argsType] as string;
  844. annotArgs = GetArrowLine(LineTag);
  845. break;
  846. }
  847. if (annotArgs != null)
  848. {
  849. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  850. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  851. PDFViewer.SetToolParam(annotArgs);
  852. }
  853. ShowPropertyPanel();
  854. }
  855. }
  856. }
  857. private void AnnotPropertyPanel_DataChanged(object sender, Dictionary<AnnotArgsType, object> e)
  858. {
  859. if (e != null)
  860. {
  861. foreach (AnnotArgsType argsType in e.Keys)
  862. {
  863. switch (argsType)
  864. {
  865. case AnnotArgsType.AnnotHighlight:
  866. if (e[argsType] is Color)
  867. {
  868. HighLightColor = new SolidColorBrush((Color)e[argsType]);
  869. }
  870. if (e[argsType] is double)
  871. {
  872. HighLightOpacity = (double)e[argsType];
  873. }
  874. break;
  875. case AnnotArgsType.AnnotUnderline:
  876. if (e[argsType] is Color)
  877. {
  878. UnderLineColor = new SolidColorBrush((Color)e[argsType]);
  879. }
  880. if (e[argsType] is double)
  881. {
  882. underLineOpacity = (double)e[argsType];
  883. }
  884. break;
  885. case AnnotArgsType.AnnotSquiggly:
  886. if (e[argsType] is Color)
  887. {
  888. SquigglyColor = new SolidColorBrush((Color)e[argsType]);
  889. }
  890. if (e[argsType] is double)
  891. {
  892. SquigglyOpacity = (double)e[argsType];
  893. }
  894. break;
  895. case AnnotArgsType.AnnotStrikeout:
  896. if (e[argsType] is Color)
  897. {
  898. StrikeoutColor = new SolidColorBrush((Color)e[argsType]);
  899. }
  900. if (e[argsType] is double)
  901. {
  902. StrikeoutOpacity = (double)e[argsType];
  903. }
  904. break;
  905. case AnnotArgsType.AnnotFreehand:
  906. if (e[argsType] is FreehandAnnotArgs)
  907. {
  908. var annot = e[argsType] as FreehandAnnotArgs;
  909. if (annot != null)
  910. {
  911. if (propertyPanel != null)
  912. {
  913. //属性面板,切换橡皮擦后,再回到画笔时,需要恢复最近画笔的属性值
  914. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  915. propertyPanel.AnnotEvent = AnnotEvent;
  916. var AnnotEvents = new List<AnnotAttribEvent>();
  917. AnnotEvents.Add(AnnotEvent);
  918. propertyPanel.AnnotEvents = AnnotEvents;
  919. propertyPanel.annot = annot;
  920. //手绘注释工具按钮的属性
  921. // FreehandPath.Opacity = annot.Transparency;
  922. // FreehandPath.Fill = new SolidColorBrush(annot.InkColor);
  923. }
  924. PDFViewer.SetToolParam(annot);
  925. }
  926. }
  927. break;
  928. case AnnotArgsType.AnnotErase:
  929. if (e[argsType] is ToggleButton)
  930. {
  931. ToggleButton clickBtn = e[argsType] as ToggleButton;
  932. if (clickBtn.IsChecked == true)
  933. {
  934. if (clickBtn.Tag.ToString() == "PenBtn")
  935. {
  936. CustomIconToggleBtn btn = new CustomIconToggleBtn();
  937. btn.Tag = "Freehand"; btn.IsChecked = true;
  938. BtnMyTools_Click(btn);
  939. break;
  940. }
  941. EraseArgs eraseArgs = new EraseArgs();
  942. eraseArgs.UIBorderColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  943. eraseArgs.UIFillColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  944. eraseArgs.Thickness = 10;
  945. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotErase);
  946. if (annotProperty != null)
  947. {
  948. eraseArgs.Thickness = annotProperty.Thickness;
  949. }
  950. PDFViewer.ClearSelectAnnots(false);
  951. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  952. PDFViewer.SetToolParam(eraseArgs);
  953. List<AnnotHandlerEventArgs> eraseArgsList = new List<AnnotHandlerEventArgs>();
  954. if (eraseArgs != null)
  955. eraseArgsList.Add(eraseArgs);
  956. AddToPropertyPanel("FreehandAnnotProperty", "Freehand", eraseArgsList);
  957. }
  958. }
  959. break;
  960. case AnnotArgsType.AnnotSquare:
  961. case AnnotArgsType.AnnotCircle:
  962. case AnnotArgsType.AnnotLine:
  963. case AnnotArgsType.AnnotFreeText:
  964. //创建注释后,修改注释属性,可用于下次创建的注释
  965. if (propertyPanel != null && propertyPanel.annot != null && PDFViewer.MouseMode == MouseModes.AnnotCreate)
  966. {
  967. PDFViewer.SetToolParam(propertyPanel.annot);
  968. }
  969. break;
  970. }
  971. }
  972. }
  973. }
  974. #endregion BindingEvent事件
  975. }
  976. }