AnnotToolContentViewModel.Command.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Office.CustomControl;
  6. using PDF_Office.EventAggregators;
  7. using PDF_Office.Helper;
  8. using PDF_Office.Properties;
  9. using PDF_Office.ViewModels.BOTA;
  10. using PDF_Office.Views.BOTA;
  11. using PDFSettings;
  12. using Prism.Mvvm;
  13. using Prism.Regions;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Controls.Primitives;
  22. using System.Windows.Media;
  23. namespace PDF_Office.ViewModels.Tools
  24. {
  25. public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware
  26. {
  27. #region 事件绑定和解绑
  28. private void BindingEvent()
  29. {
  30. //属性面板与注释工具栏绑定:属性面板的属性变化,会同步注释工具栏的属性值
  31. //比如在属性面板里更改注释颜色,会同时更新工具栏对应的工具颜色
  32. propertyPanel.DataChanged -= AnnotPropertyPanel_DataChanged;
  33. propertyPanel.DataChanged += AnnotPropertyPanel_DataChanged;
  34. propertyPanel.DefaultStored -= AnnotProperty_DefaultStored;
  35. propertyPanel.DefaultStored += AnnotProperty_DefaultStored;
  36. propertyPanel.AnnotTypeChanged -= AnnotPropertyPanel_AnnotTypeChanged;
  37. propertyPanel.AnnotTypeChanged += AnnotPropertyPanel_AnnotTypeChanged;
  38. }
  39. private void UnBindingEvent()
  40. {
  41. propertyPanel.DataChanged -= AnnotPropertyPanel_DataChanged;
  42. propertyPanel.DefaultStored -= AnnotProperty_DefaultStored;
  43. propertyPanel.AnnotTypeChanged -= AnnotPropertyPanel_AnnotTypeChanged;
  44. }
  45. private void BindingPDFViewerHandler()
  46. {
  47. //来自PDFViewer的响应事件
  48. if (PDFViewer != null)
  49. {
  50. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  51. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  52. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  53. PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  54. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  55. PDFViewer.AnnotCommandHandler += PDFViewer_AnnotCommandHandler;
  56. PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
  57. PDFViewer.WidgetClickHander += PDFViewer_WidgetClickHander;
  58. PDFViewer.SnapshotCommandHandler -= PDFViewer_SnapshotCommandHandler;
  59. PDFViewer.SnapshotCommandHandler += PDFViewer_SnapshotCommandHandler;
  60. }
  61. }
  62. private void UnBindingPDFViewerHandler()
  63. {
  64. if (PDFViewer != null)
  65. {
  66. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  67. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  68. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  69. PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
  70. PDFViewer.SnapshotCommandHandler -= PDFViewer_SnapshotCommandHandler;
  71. }
  72. }
  73. #endregion
  74. #region PDFViewer事件
  75. //选中和非选中注释,右键菜单
  76. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  77. {
  78. if (e != null)
  79. {
  80. var annot = e.AnnotItemsList[0];
  81. if (annot != null)
  82. {
  83. //IsAnnotCreateReset:是否为创建注释的状态
  84. if (e.AnnotItemsList.Count == 1 && e.IsAnnotCreateReset == false)
  85. {
  86. switch (annot.EventType)
  87. {
  88. case AnnotArgsType.AnnotHighlight:
  89. e.IsAnnotCreateReset = false;
  90. GetHighLight(annot as TextHighlightAnnotArgs);
  91. break;
  92. case AnnotArgsType.AnnotUnderline:
  93. GetUnderLine(annot as TextUnderlineAnnotArgs);
  94. break;
  95. case AnnotArgsType.AnnotStrikeout:
  96. GetStrikeout(annot as TextStrikeoutAnnotArgs);
  97. break;
  98. case AnnotArgsType.AnnotSquiggly:
  99. GetSquiggly(annot as TextSquigglyAnnotArgs);
  100. break;
  101. case AnnotArgsType.AnnotFreehand:
  102. GetFreehand(annot as FreehandAnnotArgs);
  103. break;
  104. case AnnotArgsType.AnnotFreeText:
  105. GetFreetext(annot as FreeTextAnnotArgs);
  106. break;
  107. case AnnotArgsType.AnnotSquare:
  108. GetRect(annot as SquareAnnotArgs);
  109. break;
  110. case AnnotArgsType.AnnotCircle:
  111. GetCircle(annot as CircleAnnotArgs);
  112. break;
  113. case AnnotArgsType.AnnotLine:
  114. bool isLine = true;
  115. if (e.Attribs.ContainsKey(AnnotAttrib.LineStart))
  116. {
  117. 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)
  118. {
  119. isLine = false;
  120. }
  121. }
  122. if (e.Attribs.ContainsKey(AnnotAttrib.LineEnd))
  123. {
  124. 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)
  125. {
  126. isLine = false;
  127. }
  128. }
  129. if (isLine)
  130. GetArrowLine("Line", annot as LineAnnotArgs);
  131. else
  132. GetArrowLine("Arrow", annot as LineAnnotArgs);
  133. break;
  134. case AnnotArgsType.AnnotLink:
  135. viewContentViewModel.IsCreateLink = false;
  136. GetLink(annot as LinkAnnotArgs, e);
  137. break;
  138. case AnnotArgsType.AnnotSticky:
  139. GetStickyNote(annot as StickyAnnotArgs);
  140. break;
  141. }
  142. //记录这次选中的注释,之后创建注释会跟随上次选中注释的属性值
  143. PDFViewer.SetToolParam(annot);
  144. }
  145. else if (e.AnnotItemsList.Count == 1 && e.IsAnnotCreateReset == true)
  146. {
  147. switch (annot.EventType)
  148. {
  149. case AnnotArgsType.AnnotLink:
  150. viewContentViewModel.IsCreateLink = false;
  151. GetLink(annot as LinkAnnotArgs, e);
  152. break;
  153. //case AnnotArgsType.AnnotStamp://图章
  154. // GetStamp();
  155. // break;
  156. //case AnnotArgsType.AnnotStamp://签名
  157. // annotArgs = GetSignature();
  158. // isTemplateAnnot = true;
  159. // break;
  160. }
  161. //TODO:设计已重新调整为:修改注释后,会作用到之后添加的注释中。因此先把此逻辑“创建注释后,会自动回到默认值”注释掉
  162. //if (ToolExpandDict.ContainsValue(e.AnnotItemsList[0].EventType))
  163. //{
  164. // foreach(var item in ToolExpandDict)
  165. // {
  166. // if(item.Value == e.AnnotItemsList[0].EventType)
  167. // {
  168. // FindAnnotTypeKey(item.Key, ref annot);
  169. // break;
  170. // }
  171. // }
  172. //}
  173. ShowPropertyPanel();
  174. }
  175. }
  176. }
  177. else
  178. {
  179. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  180. }
  181. }
  182. //在注释工具的状态下,右键菜单
  183. private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  184. {
  185. if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool))
  186. return;
  187. switch (e.CommandType)
  188. {
  189. case CommandType.Context:
  190. if (e.AnnotEventArgsList.Count > 0)
  191. {
  192. if (App.mainWindowViewModel.SelectedItem.IsInReadctonMode && e.AnnotEventArgsList[0].EventType == AnnotArgsType.AnnotRedaction)
  193. {
  194. //绑定标记密文处右键菜单
  195. events.GetEvent<RedactionCommandEvent>().Publish(new RedactionCommandEventArgs() { UniCode = App.mainWindowViewModel.SelectedItem.Unicode, Sender = sender, args = e });
  196. }
  197. else
  198. {
  199. e.PopupMenu = SelectAnnotContextMenu(sender);
  200. }
  201. if (e.PopupMenu != null)
  202. {
  203. e.Handle = true;
  204. }
  205. }
  206. else
  207. {
  208. if (e.PressOnSelectedText)
  209. {
  210. e.PopupMenu = NoneSelectAnnotContextMenu(sender, e);
  211. if (e.PopupMenu != null)
  212. {
  213. e.Handle = true;
  214. }
  215. }
  216. else
  217. {
  218. e.PopupMenu = ViewerContextMenu();
  219. if (e.PopupMenu != null)
  220. {
  221. e.Handle = true;
  222. }
  223. }
  224. }
  225. break;
  226. }
  227. }
  228. private void PDFViewer_SnapshotCommandHandler(object sender, SnapshotCommandArgs e)
  229. {
  230. SnapshotEditToolArgs snapToolArgs = (SnapshotEditToolArgs)PDFViewer.ToolManager.CurrentAnnotArgs;
  231. SnapshotEditMenuViewModel.SnapToolArgs = snapToolArgs;
  232. SnapshotEditMenuViewModel.PDFViewer = PDFViewer;
  233. SnapshotEditMenuViewModel.SnapToolEvent += SnapshotEditMenuViewModel_SnapToolEvent; ;
  234. var popMenu = App.Current.FindResource("SnapshotContextMenu") as ContextMenu;
  235. if (e.HitSnapshotTool && e.SnapshotRect.Width > 0 && e.SnapshotRect.Height > 0)
  236. {
  237. e.PopupMenu = popMenu;
  238. e.Handle = true;
  239. if (popMenu != null && popMenu.Items.Count == 5)
  240. {
  241. //复制
  242. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  243. menuItem.CommandTarget = PDFViewer;
  244. menuItem.Command = SnapshotEditMenuViewModel.SnapCopyCommand;
  245. //导出
  246. menuItem = popMenu.Items[1] as MenuItem;
  247. menuItem.CommandTarget = PDFViewer;
  248. if (menuItem.Items.Count == 3)
  249. {
  250. MenuItem menuItem1 = menuItem.Items[0] as MenuItem;
  251. menuItem1.CommandTarget = PDFViewer;
  252. menuItem1.Command = SnapshotEditMenuViewModel.ExportPNGCommand;
  253. menuItem1 = menuItem.Items[1] as MenuItem;
  254. menuItem1.CommandTarget = PDFViewer;
  255. menuItem1.Command = SnapshotEditMenuViewModel.ExportJPGCommand;
  256. menuItem1 = menuItem.Items[2] as MenuItem;
  257. menuItem1.CommandTarget = PDFViewer;
  258. menuItem1.Command = SnapshotEditMenuViewModel.ExportPDFCommand;
  259. }
  260. //裁剪
  261. menuItem = popMenu.Items[2] as MenuItem;
  262. menuItem.CommandTarget = PDFViewer;
  263. menuItem.Command = SnapshotEditMenuViewModel.CroppingCommand;
  264. //缩放至所选区域
  265. menuItem = popMenu.Items[3] as MenuItem;
  266. menuItem.CommandTarget = PDFViewer;
  267. menuItem.Visibility = Visibility.Collapsed;
  268. //menuItem.Command = snapshotEditMenuViewModel.CroppingCommand;
  269. //打印
  270. menuItem = popMenu.Items[4] as MenuItem;
  271. menuItem.CommandTarget = PDFViewer;
  272. menuItem.Command = SnapshotEditMenuViewModel.PrintCommand;
  273. }
  274. }
  275. }
  276. private void SnapshotEditMenuViewModel_SnapToolEvent(object sender, KeyValuePair<string, object> e)
  277. {
  278. switch (e.Key)
  279. {
  280. case "CloseSnap":
  281. {
  282. #region to do
  283. //var item = PDFViewerTab.SelectedItem as TabItem;
  284. //if (item == null)
  285. //{
  286. // ClearSelectedToolPanel();
  287. // return;
  288. //}
  289. //Grid grid = item.Content as Grid;
  290. //if (grid == null || grid.Children.Count == 0)
  291. //{
  292. // ClearSelectedToolPanel();
  293. // return;
  294. //}
  295. //PDFViewerCtrl pdfViewer = grid.Children[0] as PDFViewerCtrl;
  296. //if (pdfViewer == null)
  297. //{
  298. // ClearSelectedToolPanel();
  299. // return;
  300. //}
  301. #endregion to do
  302. switch (PDFViewer.MouseMode)
  303. {
  304. case MouseModes.SelectTextTool:
  305. break;
  306. default:
  307. BtnSelecttoolIsChecked = false;
  308. break;
  309. }
  310. }
  311. break;
  312. default:
  313. break;
  314. }
  315. }
  316. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  317. {
  318. if (e != null && e.Count > 0)
  319. {
  320. for (int i = 0; i < e.Count; i++)
  321. {
  322. AnnotEditEvent editEvent = e[i];
  323. switch (editEvent.EditAction)
  324. {
  325. case ActionType.Add:
  326. BOTAContentViewModel bOTAContentViewModel = null;
  327. BOTAContent bOTAContent = null;
  328. bool isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  329. if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true)
  330. {
  331. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  332. if (viewModel != null)
  333. {
  334. int pageindex = editEvent.PageIndex;
  335. int annotindex = editEvent.AnnotIndex;
  336. viewModel.UpdateAddedAnnot(pageindex, annotindex);
  337. }
  338. }
  339. break;
  340. case ActionType.Del:
  341. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  342. if (isTabItemAnnotation)
  343. {
  344. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  345. if (viewModel != null)
  346. {
  347. int pageindex = editEvent.PageIndex;
  348. int annotindex = editEvent.AnnotIndex;
  349. viewModel.UpdateModifiedAnnot(pageindex, annotindex, true);
  350. }
  351. }
  352. break;
  353. case ActionType.Modify:
  354. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  355. if (isTabItemAnnotation)
  356. {
  357. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  358. if (viewModel != null)
  359. {
  360. int pageindex = editEvent.PageIndex;
  361. int annotindex = editEvent.AnnotIndex;
  362. viewModel.UpdateModifiedAnnot(pageindex, annotindex, false);
  363. }
  364. }
  365. break;
  366. case ActionType.TextEdit:
  367. break;
  368. default:
  369. break;
  370. }
  371. }
  372. }
  373. }
  374. private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
  375. {
  376. }
  377. #endregion
  378. #region BindingEvent事件
  379. private void AnnotProperty_DefaultStored(object sender, object e)
  380. {
  381. }
  382. private void AnnotPropertyPanel_AnnotTypeChanged(object sender, Dictionary<AnnotArgsType, object> e)
  383. {
  384. if (e != null)
  385. {
  386. AnnotHandlerEventArgs annotArgs = null;
  387. foreach (AnnotArgsType argsType in e.Keys)
  388. {
  389. switch (argsType)
  390. {
  391. case AnnotArgsType.AnnotSquare:
  392. annotArgs = GetRect();
  393. break;
  394. case AnnotArgsType.AnnotCircle:
  395. annotArgs = GetCircle();
  396. break;
  397. case AnnotArgsType.AnnotLine:
  398. var LineTag = e[argsType] as string;
  399. annotArgs = GetArrowLine(LineTag);
  400. break;
  401. }
  402. if (annotArgs != null)
  403. {
  404. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  405. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  406. PDFViewer.SetToolParam(annotArgs);
  407. }
  408. ShowPropertyPanel();
  409. }
  410. }
  411. }
  412. private void AnnotPropertyPanel_DataChanged(object sender, Dictionary<AnnotArgsType, object> e)
  413. {
  414. if (e != null)
  415. {
  416. foreach (AnnotArgsType argsType in e.Keys)
  417. {
  418. switch (argsType)
  419. {
  420. case AnnotArgsType.AnnotHighlight:
  421. if (e[argsType] is Color)
  422. {
  423. HighLightColor = new SolidColorBrush((Color)e[argsType]);
  424. }
  425. if (e[argsType] is double)
  426. {
  427. HighLightOpacity = (double)e[argsType];
  428. }
  429. break;
  430. case AnnotArgsType.AnnotUnderline:
  431. if (e[argsType] is Color)
  432. {
  433. UnderLineColor = new SolidColorBrush((Color)e[argsType]);
  434. }
  435. if (e[argsType] is double)
  436. {
  437. underLineOpacity = (double)e[argsType];
  438. }
  439. break;
  440. case AnnotArgsType.AnnotSquiggly:
  441. if (e[argsType] is Color)
  442. {
  443. SquigglyColor = new SolidColorBrush((Color)e[argsType]);
  444. }
  445. if (e[argsType] is double)
  446. {
  447. SquigglyOpacity = (double)e[argsType];
  448. }
  449. break;
  450. case AnnotArgsType.AnnotStrikeout:
  451. if (e[argsType] is Color)
  452. {
  453. StrikeoutColor = new SolidColorBrush((Color)e[argsType]);
  454. }
  455. if (e[argsType] is double)
  456. {
  457. StrikeoutOpacity = (double)e[argsType];
  458. }
  459. break;
  460. case AnnotArgsType.AnnotFreehand:
  461. if (e[argsType] is Color)
  462. {
  463. // FreehandPath.Fill = new SolidColorBrush((Color)e[argsType]);
  464. }
  465. if (e[argsType] is double)
  466. {
  467. // FreehandPath.Opacity = (double)e[argsType];
  468. }
  469. break;
  470. case AnnotArgsType.AnnotErase:
  471. if (e[argsType] is ToggleButton)
  472. {
  473. ToggleButton clickBtn = e[argsType] as ToggleButton;
  474. if (clickBtn.IsChecked == true)
  475. {
  476. if (clickBtn.Tag.ToString() == "PenBtn")
  477. {
  478. CustomIconToggleBtn btn = new CustomIconToggleBtn();
  479. btn.Tag = "Freehand"; btn.IsChecked = true;
  480. BtnMyTools_Click(btn);
  481. break;
  482. }
  483. EraseArgs eraseArgs = new EraseArgs();
  484. eraseArgs.UIBorderColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  485. eraseArgs.UIFillColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  486. eraseArgs.Thickness = 10;
  487. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotErase);
  488. if (annotProperty != null)
  489. {
  490. eraseArgs.Thickness = annotProperty.Thickness;
  491. }
  492. PDFViewer.ClearSelectAnnots(false);
  493. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  494. PDFViewer.SetToolParam(eraseArgs);
  495. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  496. annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
  497. annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
  498. annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
  499. AddToPropertyPanel("FreehandAnnotProperty", "Freehand", eraseArgs, annotAttribsList);
  500. }
  501. }
  502. break;
  503. }
  504. }
  505. }
  506. }
  507. #endregion
  508. }
  509. }