AnnotToolContentViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer.PdfViewer;
  5. using Microsoft.Win32;
  6. using PDF_Office.CustomControl;
  7. using PDF_Office.Helper;
  8. using PDF_Office.Model;
  9. using PDF_Office.Properties;
  10. using PDF_Office.ViewModels.PropertyPanel;
  11. using PDF_Office.Views.PropertyPanel.AnnotPanel;
  12. using PDFSettings;
  13. using Prism.Commands;
  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.Input;
  25. using System.Windows.Media;
  26. namespace PDF_Office.ViewModels.Tools
  27. {
  28. public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware
  29. {
  30. public AnnotToolContentViewModel(IRegionManager regionManager)
  31. {
  32. region = regionManager;
  33. MyToolsCommand = new DelegateCommand<CustomIconToggleBtn>(BtnMyTools_Click);
  34. PropertyRegionName = Guid.NewGuid().ToString();
  35. BindingEvent();
  36. InitDefaultValue();
  37. }
  38. private Dictionary<string, bool> ToolExpandDict = new Dictionary<string, bool>();
  39. public void BtnMyTools_Click(CustomIconToggleBtn annotBtn)
  40. {
  41. //不创建注释,属于注释模板
  42. bool isTemplateAnnot = false;
  43. AnnotHandlerEventArgs annotArgs = null;
  44. if (annotBtn.IsChecked == true)
  45. {
  46. switch (annotBtn.Tag.ToString())
  47. {
  48. case "SnapshotEdit":
  49. break;
  50. case "HighLight":
  51. annotArgs = GetHighLight();
  52. break;
  53. case "UnderLine":
  54. annotArgs = GetUnderLine();
  55. break;
  56. case "Squiggly":
  57. annotArgs = GetSquiggly();
  58. break;
  59. case "Strikeout":
  60. annotArgs = GetStrikeout();
  61. break;
  62. case "Freehand":
  63. annotArgs = GetFreehand();
  64. break;
  65. case "Freetext":
  66. annotArgs = GetFreetext();
  67. break;
  68. case "StickyNote":
  69. annotArgs = GetStickyNote();
  70. break;
  71. case "Rect":
  72. annotArgs = GetRect();
  73. break;
  74. case "Circle":
  75. annotArgs = GetCircle();
  76. break;
  77. case "Arrow":
  78. case "Line":
  79. annotArgs = GetArrowLine(annotBtn.Tag.ToString());
  80. break;
  81. case "Stamp":
  82. annotArgs = GetStamp();
  83. isTemplateAnnot = true;
  84. break;
  85. case "Image":
  86. annotArgs = GetImage(annotBtn);
  87. break;
  88. case "Signature":
  89. annotArgs = GetSignature();
  90. isTemplateAnnot = true;
  91. break;
  92. case "Link":
  93. break;
  94. }
  95. if (annotArgs != null)
  96. {
  97. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  98. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  99. PDFViewer.SetToolParam(annotArgs);
  100. }
  101. }
  102. //当不是注释模板,且无创建注释时,属性面板显示为空内容
  103. if(isTemplateAnnot == false && annotArgs == null)
  104. {
  105. PDFViewer.SetMouseMode(MouseModes.PanTool);
  106. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  107. }
  108. ShowPropertyPanel((bool)annotBtn.IsChecked);
  109. }
  110. private void AnnotProperty_DefaultStored(object sender, object e)
  111. {
  112. }
  113. private void AnnotPropertyPanel_AnnotTypeChanged(object sender, Dictionary<AnnotArgsType, object> e)
  114. {
  115. if (e != null)
  116. {
  117. AnnotHandlerEventArgs annotArgs = null;
  118. foreach (AnnotArgsType argsType in e.Keys)
  119. {
  120. switch (argsType)
  121. {
  122. case AnnotArgsType.AnnotSquare:
  123. annotArgs = GetRect();
  124. break;
  125. case AnnotArgsType.AnnotCircle:
  126. annotArgs = GetCircle();
  127. break;
  128. case AnnotArgsType.AnnotLine:
  129. var LineTag = e[argsType] as string;
  130. annotArgs = GetArrowLine(LineTag);
  131. break;
  132. }
  133. if (annotArgs != null)
  134. {
  135. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  136. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  137. PDFViewer.SetToolParam(annotArgs);
  138. }
  139. ShowPropertyPanel();
  140. }
  141. }
  142. }
  143. private void AnnotPropertyPanel_DataChanged(object sender, Dictionary<AnnotArgsType, object> e)
  144. {
  145. if (e != null)
  146. {
  147. foreach (AnnotArgsType argsType in e.Keys)
  148. {
  149. switch (argsType)
  150. {
  151. case AnnotArgsType.AnnotHighlight:
  152. if (e[argsType] is Color)
  153. {
  154. HighLightColor = new SolidColorBrush((Color)e[argsType]);
  155. }
  156. if (e[argsType] is double)
  157. {
  158. HighLightOpacity = (double)e[argsType];
  159. }
  160. break;
  161. case AnnotArgsType.AnnotUnderline:
  162. if (e[argsType] is Color)
  163. {
  164. UnderLineColor = new SolidColorBrush((Color)e[argsType]);
  165. }
  166. if (e[argsType] is double)
  167. {
  168. underLineOpacity = (double)e[argsType];
  169. }
  170. break;
  171. case AnnotArgsType.AnnotSquiggly:
  172. if (e[argsType] is Color)
  173. {
  174. SquigglyColor = new SolidColorBrush((Color)e[argsType]);
  175. }
  176. if (e[argsType] is double)
  177. {
  178. SquigglyOpacity = (double)e[argsType];
  179. }
  180. break;
  181. case AnnotArgsType.AnnotStrikeout:
  182. if (e[argsType] is Color)
  183. {
  184. StrikeoutColor = new SolidColorBrush((Color)e[argsType]);
  185. }
  186. if (e[argsType] is double)
  187. {
  188. StrikeoutOpacity = (double)e[argsType];
  189. }
  190. break;
  191. case AnnotArgsType.AnnotFreehand:
  192. if (e[argsType] is Color)
  193. {
  194. // FreehandPath.Fill = new SolidColorBrush((Color)e[argsType]);
  195. }
  196. if (e[argsType] is double)
  197. {
  198. // FreehandPath.Opacity = (double)e[argsType];
  199. }
  200. break;
  201. case AnnotArgsType.AnnotErase:
  202. if (e[argsType] is ToggleButton)
  203. {
  204. ToggleButton clickBtn = e[argsType] as ToggleButton;
  205. //PDFViewerCtrl viewCtrl = GetCurrentViewer();
  206. // ClearSelectedToolPanel(BtnFreeHand);
  207. // if (viewCtrl != null && viewCtrl.PdfViewer != null)
  208. {
  209. if (clickBtn.IsChecked == true)
  210. {
  211. if (clickBtn.Tag.ToString() == "PenBtn")
  212. {
  213. CustomIconToggleBtn btn = new CustomIconToggleBtn();
  214. btn.Tag = "Freehand"; btn.IsChecked = true;
  215. BtnMyTools_Click(btn);
  216. break;
  217. }
  218. EraseArgs eraseArgs = new EraseArgs();
  219. eraseArgs.UIBorderColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  220. eraseArgs.UIFillColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  221. eraseArgs.Thickness = 10;
  222. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotErase);
  223. if (annotProperty != null)
  224. {
  225. eraseArgs.Thickness = annotProperty.Thickness;
  226. }
  227. PDFViewer.ClearSelectAnnots(false);
  228. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  229. PDFViewer.SetToolParam(eraseArgs);
  230. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  231. annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
  232. annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
  233. annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
  234. AddToPropertyPanel("FreehandAnnotProperty", "Freehand", eraseArgs, annotAttribsList);
  235. //清空选中注释
  236. //viewCtrl.PdfViewer.ClearSelectAnnots(false);
  237. //viewCtrl.PdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  238. //viewCtrl.PdfViewer.SetToolParam(eraseArgs);
  239. //if (viewCtrl.PropPanel.Children.Count > 0)
  240. //{
  241. // FreehandAnnotProperty freehandPropPanel = viewCtrl.PropPanel.Children[0] as FreehandAnnotProperty;
  242. // if (freehandPropPanel == null)
  243. // {
  244. // viewCtrl.PropPanel.Children.Clear();
  245. // freehandPropPanel = new FreehandAnnotProperty();
  246. // freehandPropPanel.EraseSet = true;
  247. // freehandPropPanel.SetInkThickness(eraseArgs.Thickness);
  248. // freehandPropPanel.DataChanged += AnnotPropertyPanel_DataChanged;
  249. // viewCtrl.PropPanel.Children.Add(freehandPropPanel);
  250. // }
  251. // Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  252. // annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
  253. // annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
  254. // annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
  255. // freehandPropPanel.AnnotEvent = null;
  256. // freehandPropPanel.SetEraseMode(true);
  257. // freehandPropPanel.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(eraseArgs, annotAttribsList);
  258. //}
  259. //else
  260. //{
  261. // FreehandAnnotProperty freehandProperty = new FreehandAnnotProperty();
  262. // freehandProperty.DataChanged += AnnotPropertyPanel_DataChanged;
  263. // Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  264. // annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
  265. // annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
  266. // annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
  267. // freehandProperty.SetInkThickness(eraseArgs.Thickness);
  268. // freehandProperty.EraseSet = true;
  269. // freehandProperty.SetEraseMode(true);
  270. // freehandProperty.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(eraseArgs, annotAttribsList);
  271. // viewCtrl.AddAnnotPropertyPanel(freehandProperty);
  272. //}
  273. }
  274. else
  275. {
  276. // BtnFreeHand.IsChecked = true;
  277. // BtnTool_Click(BtnFreeHand, new RoutedEventArgs());
  278. }
  279. }
  280. }
  281. break;
  282. }
  283. }
  284. }
  285. }
  286. public IRegionManager region;
  287. public string PropertyRegionName { get; set; }
  288. public bool IsNavigationTarget(NavigationContext navigationContext)
  289. {
  290. return true;
  291. }
  292. public void OnNavigatedFrom(NavigationContext navigationContext)
  293. {
  294. }
  295. public void OnNavigatedTo(NavigationContext navigationContext)
  296. {
  297. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  298. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  299. // navigationContext.Parameters.TryGetValue<PropertyPanelContentViewModel>(ParameterNames.PropertyPanelContentViewModel, out propertyPanelContentViewModel);
  300. if (PDFViewer != null)
  301. {
  302. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  303. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  304. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  305. PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  306. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  307. PDFViewer.AnnotCommandHandler += PDFViewer_AnnotCommandHandler;
  308. PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
  309. PDFViewer.WidgetClickHander += PDFViewer_WidgetClickHander;
  310. }
  311. }
  312. private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
  313. {
  314. }
  315. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  316. {
  317. }
  318. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  319. {
  320. if (e != null)
  321. {
  322. var annot = e.AnnotItemsList[0];
  323. if (annot != null)
  324. {
  325. switch (annot.EventType)
  326. {
  327. case AnnotArgsType.AnnotHighlight:
  328. GetHighLight(annot as TextHighlightAnnotArgs);
  329. break;
  330. case AnnotArgsType.AnnotUnderline:
  331. GetUnderLine(annot as TextUnderlineAnnotArgs);
  332. break;
  333. case AnnotArgsType.AnnotStrikeout:
  334. GetStrikeout(annot as TextStrikeoutAnnotArgs);
  335. break;
  336. case AnnotArgsType.AnnotSquiggly:
  337. GetSquiggly(annot as TextSquigglyAnnotArgs);
  338. break;
  339. case AnnotArgsType.AnnotFreehand:
  340. GetFreehand(annot as FreehandAnnotArgs);
  341. break;
  342. case AnnotArgsType.AnnotFreeText:
  343. GetFreetext(annot as FreeTextAnnotArgs);
  344. break;
  345. case AnnotArgsType.AnnotSquare:
  346. GetRect(annot as SquareAnnotArgs);
  347. break;
  348. case AnnotArgsType.AnnotCircle:
  349. GetCircle(annot as CircleAnnotArgs);
  350. break;
  351. case AnnotArgsType.AnnotLine:
  352. bool isLine = true;
  353. if (e.Attribs.ContainsKey(AnnotAttrib.LineStart))
  354. {
  355. 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)
  356. {
  357. isLine = false;
  358. }
  359. }
  360. if (e.Attribs.ContainsKey(AnnotAttrib.LineEnd))
  361. {
  362. 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)
  363. {
  364. isLine = false;
  365. }
  366. }
  367. if (isLine)
  368. GetArrowLine("Line", annot as LineAnnotArgs);
  369. else
  370. GetArrowLine("Arrow", annot as LineAnnotArgs);
  371. break;
  372. }
  373. ShowPropertyPanel();
  374. }
  375. }
  376. else
  377. {
  378. // PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  379. // viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  380. }
  381. }
  382. private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  383. {
  384. if (e.AnnotEventArgsList == null)
  385. return;
  386. // var annotlist = e.AnnotEventArgsList;
  387. switch (e.CommandType)
  388. {
  389. case CommandType.Context:
  390. if (e.AnnotEventArgsList.Count > 0)
  391. {
  392. e.PopupMenu = SelectAnnotContextMenu(sender);
  393. if (e.PopupMenu != null)
  394. {
  395. e.Handle = true;
  396. }
  397. }
  398. else
  399. {
  400. e.PopupMenu = NoneSelectAnnotContextMenu(sender);
  401. if (e.PopupMenu != null)
  402. {
  403. e.Handle = true;
  404. }
  405. }
  406. break;
  407. }
  408. }
  409. private ContextMenu SelectAnnotContextMenu(object sender)
  410. {
  411. var popMenu = App.Current.FindResource("SelectAnnotContextMenu") as ContextMenu;
  412. if (popMenu != null && popMenu.Items.Count == 4)
  413. {
  414. //剪切
  415. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  416. menuItem.CommandTarget = (UIElement)sender;
  417. menuItem.Command = ApplicationCommands.Cut;
  418. //复制
  419. menuItem = popMenu.Items[1] as MenuItem;
  420. menuItem.CommandTarget = (UIElement)sender;
  421. menuItem.Command = ApplicationCommands.Copy;
  422. //删除
  423. menuItem = popMenu.Items[2] as MenuItem;
  424. menuItem.CommandTarget = (UIElement)sender;
  425. menuItem.Command = ApplicationCommands.Delete;
  426. //笔记
  427. //menuItem = popMenu.Items[2] as MenuItem;
  428. //menuItem.CommandTarget = (UIElement)sender;
  429. //menuItem.Command = ApplicationCommands.no;
  430. }
  431. return popMenu;
  432. }
  433. private ContextMenu NoneSelectAnnotContextMenu(object sender)
  434. {
  435. var popMenu = new ContextMenu();
  436. MenuItem menuItem = new MenuItem();
  437. menuItem.CommandTarget = (UIElement)sender;
  438. menuItem.Command = ApplicationCommands.Paste;
  439. popMenu.Items.Add(menuItem);
  440. menuItem = new MenuItem();
  441. menuItem.CommandTarget = (UIElement)sender;
  442. menuItem.Command = ApplicationCommands.Copy;
  443. popMenu.Items.Add(menuItem);
  444. return popMenu;
  445. }
  446. /// <summary>
  447. /// 展开显示属性面板
  448. /// </summary>
  449. private void ShowPropertyPanel(bool show = true)
  450. {
  451. viewContentViewModel.IsPropertyOpen = show;
  452. }
  453. }
  454. }