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. PDFViewer.SetToolParam(annot);
  162. //TODO: 设计已重新调整为:修改注释后,会作用到之后添加的注释中。因此先把此逻辑“创建注释后,会自动回到默认值”注释掉
  163. //if (ToolExpandDict.ContainsValue(e.AnnotItemsList[0].EventType))
  164. //{
  165. // foreach (var item in ToolExpandDict)
  166. // {
  167. // if (item.Value == e.AnnotItemsList[0].EventType)
  168. // {
  169. // FindAnnotTypeKey(item.Key, ref annot);
  170. // break;
  171. // }
  172. // }
  173. //}
  174. ShowPropertyPanel();
  175. }
  176. }
  177. }
  178. else
  179. {
  180. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  181. }
  182. }
  183. //在注释工具的状态下,右键菜单
  184. private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  185. {
  186. if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool))
  187. return;
  188. switch (e.CommandType)
  189. {
  190. case CommandType.Context:
  191. if (e.AnnotEventArgsList.Count > 0)
  192. {
  193. if (App.mainWindowViewModel.SelectedItem.IsInReadctonMode && e.AnnotEventArgsList[0].EventType == AnnotArgsType.AnnotRedaction)
  194. {
  195. //绑定标记密文处右键菜单
  196. events.GetEvent<RedactionCommandEvent>().Publish(new RedactionCommandEventArgs() { UniCode = App.mainWindowViewModel.SelectedItem.Unicode, Sender = sender, args = e });
  197. }
  198. else
  199. {
  200. e.PopupMenu = SelectAnnotContextMenu(sender);
  201. }
  202. if (e.PopupMenu != null)
  203. {
  204. e.Handle = true;
  205. }
  206. }
  207. else
  208. {
  209. if (e.PressOnSelectedText)
  210. {
  211. e.PopupMenu = NoneSelectAnnotContextMenu(sender, e);
  212. if (e.PopupMenu != null)
  213. {
  214. e.Handle = true;
  215. }
  216. }
  217. else
  218. {
  219. e.PopupMenu = ViewerContextMenu();
  220. if (e.PopupMenu != null)
  221. {
  222. e.Handle = true;
  223. }
  224. }
  225. }
  226. break;
  227. }
  228. }
  229. private void PDFViewer_SnapshotCommandHandler(object sender, SnapshotCommandArgs e)
  230. {
  231. SnapshotEditToolArgs snapToolArgs = (SnapshotEditToolArgs)PDFViewer.ToolManager.CurrentAnnotArgs;
  232. SnapshotEditMenuViewModel.SnapToolArgs = snapToolArgs;
  233. SnapshotEditMenuViewModel.PDFViewer = PDFViewer;
  234. SnapshotEditMenuViewModel.SnapToolEvent += SnapshotEditMenuViewModel_SnapToolEvent; ;
  235. var popMenu = App.Current.FindResource("SnapshotContextMenu") as ContextMenu;
  236. if (e.HitSnapshotTool && e.SnapshotRect.Width > 0 && e.SnapshotRect.Height > 0)
  237. {
  238. e.PopupMenu = popMenu;
  239. e.Handle = true;
  240. if (popMenu != null && popMenu.Items.Count == 5)
  241. {
  242. //复制
  243. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  244. menuItem.CommandTarget = PDFViewer;
  245. menuItem.Command = SnapshotEditMenuViewModel.SnapCopyCommand;
  246. //导出
  247. menuItem = popMenu.Items[1] as MenuItem;
  248. menuItem.CommandTarget = PDFViewer;
  249. if (menuItem.Items.Count == 3)
  250. {
  251. MenuItem menuItem1 = menuItem.Items[0] as MenuItem;
  252. menuItem1.CommandTarget = PDFViewer;
  253. menuItem1.Command = SnapshotEditMenuViewModel.ExportPNGCommand;
  254. menuItem1 = menuItem.Items[1] as MenuItem;
  255. menuItem1.CommandTarget = PDFViewer;
  256. menuItem1.Command = SnapshotEditMenuViewModel.ExportJPGCommand;
  257. menuItem1 = menuItem.Items[2] as MenuItem;
  258. menuItem1.CommandTarget = PDFViewer;
  259. menuItem1.Command = SnapshotEditMenuViewModel.ExportPDFCommand;
  260. }
  261. //裁剪
  262. menuItem = popMenu.Items[2] as MenuItem;
  263. menuItem.CommandTarget = PDFViewer;
  264. menuItem.Command = SnapshotEditMenuViewModel.CroppingCommand;
  265. //缩放至所选区域
  266. menuItem = popMenu.Items[3] as MenuItem;
  267. menuItem.CommandTarget = PDFViewer;
  268. menuItem.Visibility = Visibility.Collapsed;
  269. //menuItem.Command = snapshotEditMenuViewModel.CroppingCommand;
  270. //打印
  271. menuItem = popMenu.Items[4] as MenuItem;
  272. menuItem.CommandTarget = PDFViewer;
  273. menuItem.Command = SnapshotEditMenuViewModel.PrintCommand;
  274. }
  275. }
  276. }
  277. private void SnapshotEditMenuViewModel_SnapToolEvent(object sender, KeyValuePair<string, object> e)
  278. {
  279. switch (e.Key)
  280. {
  281. case "CloseSnap":
  282. {
  283. #region to do
  284. //var item = PDFViewerTab.SelectedItem as TabItem;
  285. //if (item == null)
  286. //{
  287. // ClearSelectedToolPanel();
  288. // return;
  289. //}
  290. //Grid grid = item.Content as Grid;
  291. //if (grid == null || grid.Children.Count == 0)
  292. //{
  293. // ClearSelectedToolPanel();
  294. // return;
  295. //}
  296. //PDFViewerCtrl pdfViewer = grid.Children[0] as PDFViewerCtrl;
  297. //if (pdfViewer == null)
  298. //{
  299. // ClearSelectedToolPanel();
  300. // return;
  301. //}
  302. #endregion to do
  303. switch (PDFViewer.MouseMode)
  304. {
  305. case MouseModes.SelectTextTool:
  306. break;
  307. default:
  308. BtnSelecttoolIsChecked = false;
  309. break;
  310. }
  311. }
  312. break;
  313. default:
  314. break;
  315. }
  316. }
  317. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  318. {
  319. if (e != null && e.Count > 0)
  320. {
  321. for (int i = 0; i < e.Count; i++)
  322. {
  323. AnnotEditEvent editEvent = e[i];
  324. switch (editEvent.EditAction)
  325. {
  326. case ActionType.Add:
  327. BOTAContentViewModel bOTAContentViewModel = null;
  328. BOTAContent bOTAContent = null;
  329. bool isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  330. if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true)
  331. {
  332. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  333. if (viewModel != null)
  334. {
  335. int pageindex = editEvent.PageIndex;
  336. int annotindex = editEvent.AnnotIndex;
  337. viewModel.UpdateAddedAnnot(pageindex, annotindex);
  338. }
  339. }
  340. break;
  341. case ActionType.Del:
  342. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  343. if (isTabItemAnnotation)
  344. {
  345. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  346. if (viewModel != null)
  347. {
  348. int pageindex = editEvent.PageIndex;
  349. int annotindex = editEvent.AnnotIndex;
  350. viewModel.UpdateModifiedAnnot(pageindex, annotindex, true);
  351. }
  352. }
  353. break;
  354. case ActionType.Modify:
  355. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  356. if (isTabItemAnnotation)
  357. {
  358. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  359. if (viewModel != null)
  360. {
  361. int pageindex = editEvent.PageIndex;
  362. int annotindex = editEvent.AnnotIndex;
  363. viewModel.UpdateModifiedAnnot(pageindex, annotindex, false);
  364. }
  365. }
  366. break;
  367. case ActionType.TextEdit:
  368. break;
  369. default:
  370. break;
  371. }
  372. }
  373. }
  374. }
  375. private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
  376. {
  377. }
  378. #endregion
  379. #region BindingEvent事件
  380. private void AnnotProperty_DefaultStored(object sender, object e)
  381. {
  382. }
  383. private void AnnotPropertyPanel_AnnotTypeChanged(object sender, Dictionary<AnnotArgsType, object> e)
  384. {
  385. if (e != null)
  386. {
  387. AnnotHandlerEventArgs annotArgs = null;
  388. foreach (AnnotArgsType argsType in e.Keys)
  389. {
  390. switch (argsType)
  391. {
  392. case AnnotArgsType.AnnotSquare:
  393. annotArgs = GetRect();
  394. break;
  395. case AnnotArgsType.AnnotCircle:
  396. annotArgs = GetCircle();
  397. break;
  398. case AnnotArgsType.AnnotLine:
  399. var LineTag = e[argsType] as string;
  400. annotArgs = GetArrowLine(LineTag);
  401. break;
  402. }
  403. if (annotArgs != null)
  404. {
  405. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  406. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  407. PDFViewer.SetToolParam(annotArgs);
  408. }
  409. ShowPropertyPanel();
  410. }
  411. }
  412. }
  413. private void AnnotPropertyPanel_DataChanged(object sender, Dictionary<AnnotArgsType, object> e)
  414. {
  415. if (e != null)
  416. {
  417. foreach (AnnotArgsType argsType in e.Keys)
  418. {
  419. switch (argsType)
  420. {
  421. case AnnotArgsType.AnnotHighlight:
  422. if (e[argsType] is Color)
  423. {
  424. HighLightColor = new SolidColorBrush((Color)e[argsType]);
  425. }
  426. if (e[argsType] is double)
  427. {
  428. HighLightOpacity = (double)e[argsType];
  429. }
  430. break;
  431. case AnnotArgsType.AnnotUnderline:
  432. if (e[argsType] is Color)
  433. {
  434. UnderLineColor = new SolidColorBrush((Color)e[argsType]);
  435. }
  436. if (e[argsType] is double)
  437. {
  438. underLineOpacity = (double)e[argsType];
  439. }
  440. break;
  441. case AnnotArgsType.AnnotSquiggly:
  442. if (e[argsType] is Color)
  443. {
  444. SquigglyColor = new SolidColorBrush((Color)e[argsType]);
  445. }
  446. if (e[argsType] is double)
  447. {
  448. SquigglyOpacity = (double)e[argsType];
  449. }
  450. break;
  451. case AnnotArgsType.AnnotStrikeout:
  452. if (e[argsType] is Color)
  453. {
  454. StrikeoutColor = new SolidColorBrush((Color)e[argsType]);
  455. }
  456. if (e[argsType] is double)
  457. {
  458. StrikeoutOpacity = (double)e[argsType];
  459. }
  460. break;
  461. case AnnotArgsType.AnnotFreehand:
  462. if (e[argsType] is Color)
  463. {
  464. // FreehandPath.Fill = new SolidColorBrush((Color)e[argsType]);
  465. }
  466. if (e[argsType] is double)
  467. {
  468. // FreehandPath.Opacity = (double)e[argsType];
  469. }
  470. break;
  471. case AnnotArgsType.AnnotErase:
  472. if (e[argsType] is ToggleButton)
  473. {
  474. ToggleButton clickBtn = e[argsType] as ToggleButton;
  475. if (clickBtn.IsChecked == true)
  476. {
  477. if (clickBtn.Tag.ToString() == "PenBtn")
  478. {
  479. CustomIconToggleBtn btn = new CustomIconToggleBtn();
  480. btn.Tag = "Freehand"; btn.IsChecked = true;
  481. BtnMyTools_Click(btn);
  482. break;
  483. }
  484. EraseArgs eraseArgs = new EraseArgs();
  485. eraseArgs.UIBorderColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  486. eraseArgs.UIFillColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  487. eraseArgs.Thickness = 10;
  488. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotErase);
  489. if (annotProperty != null)
  490. {
  491. eraseArgs.Thickness = annotProperty.Thickness;
  492. }
  493. PDFViewer.ClearSelectAnnots(false);
  494. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  495. PDFViewer.SetToolParam(eraseArgs);
  496. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  497. annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
  498. annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
  499. annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
  500. AddToPropertyPanel("FreehandAnnotProperty", "Freehand", eraseArgs, annotAttribsList);
  501. }
  502. }
  503. break;
  504. }
  505. }
  506. }
  507. }
  508. #endregion
  509. }
  510. }