AnnotToolContentViewModel.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer.PdfViewer;
  5. using Microsoft.Office.Interop.Word;
  6. using Microsoft.Win32;
  7. using PDF_Office.CustomControl;
  8. using PDF_Office.EventAggregators;
  9. using PDF_Office.Helper;
  10. using PDF_Office.Model;
  11. using PDF_Office.Properties;
  12. using PDF_Office.ViewModels.BOTA;
  13. using PDF_Office.ViewModels.PropertyPanel;
  14. using PDF_Office.ViewModels.PropertyPanel.AnnotPanel;
  15. using PDF_Office.Views.BOTA;
  16. using PDF_Office.Views.PropertyPanel.AnnotPanel;
  17. using PDFSettings;
  18. using Prism.Commands;
  19. using Prism.Events;
  20. using Prism.Mvvm;
  21. using Prism.Regions;
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Linq;
  25. using System.Text;
  26. using System.Threading.Tasks;
  27. using System.Windows;
  28. using System.Windows.Controls;
  29. using System.Windows.Controls.Primitives;
  30. using System.Windows.Input;
  31. using System.Windows.Media;
  32. namespace PDF_Office.ViewModels.Tools
  33. {
  34. public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware
  35. {
  36. private IEventAggregator events;
  37. private bool btnSelecttoolIsChecked = false;
  38. public bool BtnSelecttoolIsChecked
  39. {
  40. get { return btnSelecttoolIsChecked; }
  41. set
  42. {
  43. SetProperty(ref btnSelecttoolIsChecked, value);
  44. }
  45. }
  46. private bool btnLinkIsChecked = false;
  47. public bool BtnLinkIsChecked
  48. {
  49. get { return btnLinkIsChecked; }
  50. set
  51. {
  52. SetProperty(ref btnLinkIsChecked, value);
  53. }
  54. }
  55. private SnapshotEditMenuViewModel snapshotEditMenuViewModel = new SnapshotEditMenuViewModel();
  56. public SnapshotEditMenuViewModel SnapshotEditMenuViewModel { get => snapshotEditMenuViewModel; set => snapshotEditMenuViewModel = value; }
  57. public DelegateCommand<object> SetAddAnnotationCommand { get; set; }
  58. public AnnotToolContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
  59. {
  60. region = regionManager;
  61. events = eventAggregator;
  62. MyToolsCommand = new DelegateCommand<CustomIconToggleBtn>(BtnMyTools_Click);
  63. SetAddAnnotationCommand = new DelegateCommand<object>(AddAnnotation_Click);
  64. PropertyRegionName = Guid.NewGuid().ToString();
  65. BindingEvent();
  66. InitDefaultValue();
  67. }
  68. private Dictionary<string, bool> ToolExpandDict = new Dictionary<string, bool>();
  69. public void BtnMyTools_Click(CustomIconToggleBtn annotBtn)
  70. {
  71. //不创建注释,属于注释模板
  72. bool isTemplateAnnot = false;
  73. bool isSnapshotEdit = false;
  74. AnnotHandlerEventArgs annotArgs = null;
  75. if (annotBtn.IsChecked == true)
  76. {
  77. switch (annotBtn.Tag.ToString())
  78. {
  79. case "SnapshotEdit"://内容选择
  80. annotArgs = GetSnapshotEdit(annotBtn);
  81. isSnapshotEdit = true;
  82. break;
  83. case "HighLight"://字体高亮
  84. annotArgs = GetHighLight();
  85. break;
  86. case "UnderLine"://下划线
  87. annotArgs = GetUnderLine();
  88. break;
  89. case "Squiggly"://波浪线
  90. annotArgs = GetSquiggly();
  91. break;
  92. case "Strikeout"://删除线
  93. annotArgs = GetStrikeout();
  94. break;
  95. case "Freehand"://手绘
  96. annotArgs = GetFreehand();
  97. break;
  98. case "Freetext"://文本
  99. annotArgs = GetFreetext();
  100. break;
  101. case "StickyNote"://便签
  102. annotArgs = GetStickyNote();
  103. break;
  104. case "Rect"://矩形
  105. annotArgs = GetRect();
  106. break;
  107. case "Circle"://圆
  108. annotArgs = GetCircle();
  109. break;
  110. case "Arrow"://箭头
  111. case "Line"://线
  112. annotArgs = GetArrowLine(annotBtn.Tag.ToString());
  113. break;
  114. case "Stamp"://图章
  115. annotArgs = GetStamp();
  116. isTemplateAnnot = true;
  117. break;
  118. case "Image":
  119. annotArgs = GetImage(annotBtn);
  120. break;
  121. case "Signature"://签名
  122. annotArgs = GetSignature();
  123. isTemplateAnnot = true;
  124. PDFViewer.SetMouseMode(MouseModes.PanTool);//清空其他注释
  125. break;
  126. case "Link"://链接
  127. viewContentViewModel.IsCreateLink = false;
  128. annotArgs = GetLink();
  129. break;
  130. }
  131. if (annotArgs != null)
  132. {
  133. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  134. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  135. PDFViewer.SetToolParam(annotArgs);
  136. }
  137. }
  138. //当不是注释模板,且无创建注释时,属性面板显示为空内容
  139. if (isTemplateAnnot == false && annotArgs == null)
  140. {
  141. PDFViewer.SetMouseMode(MouseModes.PanTool);
  142. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  143. }
  144. if (isSnapshotEdit == true)
  145. {
  146. ShowPropertyPanel(false);
  147. isSnapshotEdit = false;
  148. }
  149. else
  150. {
  151. ShowPropertyPanel((bool)annotBtn.IsChecked);
  152. }
  153. }
  154. private void AnnotProperty_DefaultStored(object sender, object e)
  155. {
  156. }
  157. private void AnnotPropertyPanel_AnnotTypeChanged(object sender, Dictionary<AnnotArgsType, object> e)
  158. {
  159. if (e != null)
  160. {
  161. AnnotHandlerEventArgs annotArgs = null;
  162. foreach (AnnotArgsType argsType in e.Keys)
  163. {
  164. switch (argsType)
  165. {
  166. case AnnotArgsType.AnnotSquare:
  167. annotArgs = GetRect();
  168. break;
  169. case AnnotArgsType.AnnotCircle:
  170. annotArgs = GetCircle();
  171. break;
  172. case AnnotArgsType.AnnotLine:
  173. var LineTag = e[argsType] as string;
  174. annotArgs = GetArrowLine(LineTag);
  175. break;
  176. }
  177. if (annotArgs != null)
  178. {
  179. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  180. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  181. PDFViewer.SetToolParam(annotArgs);
  182. }
  183. ShowPropertyPanel();
  184. }
  185. }
  186. }
  187. private void AnnotPropertyPanel_DataChanged(object sender, Dictionary<AnnotArgsType, object> e)
  188. {
  189. if (e != null)
  190. {
  191. foreach (AnnotArgsType argsType in e.Keys)
  192. {
  193. switch (argsType)
  194. {
  195. case AnnotArgsType.AnnotHighlight:
  196. if (e[argsType] is Color)
  197. {
  198. HighLightColor = new SolidColorBrush((Color)e[argsType]);
  199. }
  200. if (e[argsType] is double)
  201. {
  202. HighLightOpacity = (double)e[argsType];
  203. }
  204. break;
  205. case AnnotArgsType.AnnotUnderline:
  206. if (e[argsType] is Color)
  207. {
  208. UnderLineColor = new SolidColorBrush((Color)e[argsType]);
  209. }
  210. if (e[argsType] is double)
  211. {
  212. underLineOpacity = (double)e[argsType];
  213. }
  214. break;
  215. case AnnotArgsType.AnnotSquiggly:
  216. if (e[argsType] is Color)
  217. {
  218. SquigglyColor = new SolidColorBrush((Color)e[argsType]);
  219. }
  220. if (e[argsType] is double)
  221. {
  222. SquigglyOpacity = (double)e[argsType];
  223. }
  224. break;
  225. case AnnotArgsType.AnnotStrikeout:
  226. if (e[argsType] is Color)
  227. {
  228. StrikeoutColor = new SolidColorBrush((Color)e[argsType]);
  229. }
  230. if (e[argsType] is double)
  231. {
  232. StrikeoutOpacity = (double)e[argsType];
  233. }
  234. break;
  235. case AnnotArgsType.AnnotFreehand:
  236. if (e[argsType] is Color)
  237. {
  238. // FreehandPath.Fill = new SolidColorBrush((Color)e[argsType]);
  239. }
  240. if (e[argsType] is double)
  241. {
  242. // FreehandPath.Opacity = (double)e[argsType];
  243. }
  244. break;
  245. case AnnotArgsType.AnnotErase:
  246. if (e[argsType] is ToggleButton)
  247. {
  248. ToggleButton clickBtn = e[argsType] as ToggleButton;
  249. //PDFViewerCtrl viewCtrl = GetCurrentViewer();
  250. // ClearSelectedToolPanel(BtnFreeHand);
  251. // if (viewCtrl != null && viewCtrl.PdfViewer != null)
  252. {
  253. if (clickBtn.IsChecked == true)
  254. {
  255. if (clickBtn.Tag.ToString() == "PenBtn")
  256. {
  257. CustomIconToggleBtn btn = new CustomIconToggleBtn();
  258. btn.Tag = "Freehand"; btn.IsChecked = true;
  259. BtnMyTools_Click(btn);
  260. break;
  261. }
  262. EraseArgs eraseArgs = new EraseArgs();
  263. eraseArgs.UIBorderColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  264. eraseArgs.UIFillColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  265. eraseArgs.Thickness = 10;
  266. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotErase);
  267. if (annotProperty != null)
  268. {
  269. eraseArgs.Thickness = annotProperty.Thickness;
  270. }
  271. PDFViewer.ClearSelectAnnots(false);
  272. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  273. PDFViewer.SetToolParam(eraseArgs);
  274. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  275. annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
  276. annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
  277. annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
  278. AddToPropertyPanel("FreehandAnnotProperty", "Freehand", eraseArgs, annotAttribsList);
  279. //清空选中注释
  280. //viewCtrl.PdfViewer.ClearSelectAnnots(false);
  281. //viewCtrl.PdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  282. //viewCtrl.PdfViewer.SetToolParam(eraseArgs);
  283. //if (viewCtrl.PropPanel.Children.Count > 0)
  284. //{
  285. // FreehandAnnotProperty freehandPropPanel = viewCtrl.PropPanel.Children[0] as FreehandAnnotProperty;
  286. // if (freehandPropPanel == null)
  287. // {
  288. // viewCtrl.PropPanel.Children.Clear();
  289. // freehandPropPanel = new FreehandAnnotProperty();
  290. // freehandPropPanel.EraseSet = true;
  291. // freehandPropPanel.SetInkThickness(eraseArgs.Thickness);
  292. // freehandPropPanel.DataChanged += AnnotPropertyPanel_DataChanged;
  293. // viewCtrl.PropPanel.Children.Add(freehandPropPanel);
  294. // }
  295. // Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  296. // annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
  297. // annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
  298. // annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
  299. // freehandPropPanel.AnnotEvent = null;
  300. // freehandPropPanel.SetEraseMode(true);
  301. // freehandPropPanel.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(eraseArgs, annotAttribsList);
  302. //}
  303. //else
  304. //{
  305. // FreehandAnnotProperty freehandProperty = new FreehandAnnotProperty();
  306. // freehandProperty.DataChanged += AnnotPropertyPanel_DataChanged;
  307. // Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  308. // annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
  309. // annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
  310. // annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
  311. // freehandProperty.SetInkThickness(eraseArgs.Thickness);
  312. // freehandProperty.EraseSet = true;
  313. // freehandProperty.SetEraseMode(true);
  314. // freehandProperty.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(eraseArgs, annotAttribsList);
  315. // viewCtrl.AddAnnotPropertyPanel(freehandProperty);
  316. //}
  317. }
  318. else
  319. {
  320. // BtnFreeHand.IsChecked = true;
  321. // BtnTool_Click(BtnFreeHand, new RoutedEventArgs());
  322. }
  323. }
  324. }
  325. break;
  326. }
  327. }
  328. }
  329. }
  330. public IRegionManager region;
  331. public string PropertyRegionName { get; set; }
  332. public bool IsNavigationTarget(NavigationContext navigationContext)
  333. {
  334. return true;
  335. }
  336. public void OnNavigatedFrom(NavigationContext navigationContext)
  337. {
  338. }
  339. public void OnNavigatedTo(NavigationContext navigationContext)
  340. {
  341. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  342. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  343. // navigationContext.Parameters.TryGetValue<PropertyPanelContentViewModel>(ParameterNames.PropertyPanelContentViewModel, out propertyPanelContentViewModel);
  344. if (PDFViewer != null)
  345. {
  346. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  347. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  348. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  349. PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  350. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  351. PDFViewer.AnnotCommandHandler += PDFViewer_AnnotCommandHandler;
  352. PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
  353. PDFViewer.WidgetClickHander += PDFViewer_WidgetClickHander;
  354. PDFViewer.SnapshotCommandHandler -= PDFViewer_SnapshotCommandHandler;
  355. PDFViewer.SnapshotCommandHandler += PDFViewer_SnapshotCommandHandler;
  356. }
  357. }
  358. private void PDFViewer_SnapshotCommandHandler(object sender, SnapshotCommandArgs e)
  359. {
  360. SnapshotEditToolArgs snapToolArgs = (SnapshotEditToolArgs)PDFViewer.ToolManager.CurrentAnnotArgs;
  361. SnapshotEditMenuViewModel.SnapToolArgs = snapToolArgs;
  362. SnapshotEditMenuViewModel.PDFViewer = PDFViewer;
  363. SnapshotEditMenuViewModel.SnapToolEvent += SnapshotEditMenuViewModel_SnapToolEvent; ;
  364. var popMenu = App.Current.FindResource("SnapshotContextMenu") as ContextMenu;
  365. if (e.HitSnapshotTool && e.SnapshotRect.Width > 0 && e.SnapshotRect.Height > 0)
  366. {
  367. e.PopupMenu = popMenu;
  368. e.Handle = true;
  369. if (popMenu != null && popMenu.Items.Count == 5)
  370. {
  371. //复制
  372. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  373. menuItem.CommandTarget = PDFViewer;
  374. menuItem.Command = SnapshotEditMenuViewModel.SnapCopyCommand;
  375. //导出
  376. menuItem = popMenu.Items[1] as MenuItem;
  377. menuItem.CommandTarget = PDFViewer;
  378. if (menuItem.Items.Count == 3)
  379. {
  380. MenuItem menuItem1 = menuItem.Items[0] as MenuItem;
  381. menuItem1.CommandTarget = PDFViewer;
  382. menuItem1.Command = SnapshotEditMenuViewModel.ExportPNGCommand;
  383. menuItem1 = menuItem.Items[1] as MenuItem;
  384. menuItem1.CommandTarget = PDFViewer;
  385. menuItem1.Command = SnapshotEditMenuViewModel.ExportJPGCommand;
  386. menuItem1 = menuItem.Items[2] as MenuItem;
  387. menuItem1.CommandTarget = PDFViewer;
  388. menuItem1.Command = SnapshotEditMenuViewModel.ExportPDFCommand;
  389. }
  390. //裁剪
  391. menuItem = popMenu.Items[2] as MenuItem;
  392. menuItem.CommandTarget = PDFViewer;
  393. menuItem.Command = SnapshotEditMenuViewModel.CroppingCommand;
  394. //缩放至所选区域
  395. menuItem = popMenu.Items[3] as MenuItem;
  396. menuItem.CommandTarget = PDFViewer;
  397. menuItem.Visibility = Visibility.Collapsed;
  398. //menuItem.Command = snapshotEditMenuViewModel.CroppingCommand;
  399. //打印
  400. menuItem = popMenu.Items[4] as MenuItem;
  401. menuItem.CommandTarget = PDFViewer;
  402. menuItem.Command = SnapshotEditMenuViewModel.PrintCommand;
  403. }
  404. }
  405. }
  406. private void SnapshotEditMenuViewModel_SnapToolEvent(object sender, KeyValuePair<string, object> e)
  407. {
  408. switch (e.Key)
  409. {
  410. case "CloseSnap":
  411. {
  412. #region to do
  413. //var item = PDFViewerTab.SelectedItem as TabItem;
  414. //if (item == null)
  415. //{
  416. // ClearSelectedToolPanel();
  417. // return;
  418. //}
  419. //Grid grid = item.Content as Grid;
  420. //if (grid == null || grid.Children.Count == 0)
  421. //{
  422. // ClearSelectedToolPanel();
  423. // return;
  424. //}
  425. //PDFViewerCtrl pdfViewer = grid.Children[0] as PDFViewerCtrl;
  426. //if (pdfViewer == null)
  427. //{
  428. // ClearSelectedToolPanel();
  429. // return;
  430. //}
  431. #endregion to do
  432. switch (PDFViewer.MouseMode)
  433. {
  434. case MouseModes.SelectTextTool:
  435. break;
  436. default:
  437. BtnSelecttoolIsChecked = false;
  438. break;
  439. }
  440. }
  441. break;
  442. default:
  443. break;
  444. }
  445. }
  446. private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
  447. {
  448. }
  449. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  450. {
  451. if (e != null && e.Count > 0)
  452. {
  453. for (int i = 0; i < e.Count; i++)
  454. {
  455. AnnotEditEvent editEvent = e[i];
  456. switch (editEvent.EditAction)
  457. {
  458. case ActionType.Add:
  459. BOTAContentViewModel bOTAContentViewModel = null;
  460. bool isTabItemAnnotation = IsTabItemAnnotationShow(out bOTAContentViewModel);
  461. if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true)
  462. {
  463. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  464. if (viewModel != null)
  465. {
  466. int pageindex = editEvent.PageIndex;
  467. int annotindex = editEvent.AnnotIndex;
  468. viewModel.UpdateAddedAnnot(pageindex, annotindex);
  469. }
  470. }
  471. break;
  472. case ActionType.Del:
  473. isTabItemAnnotation = IsTabItemAnnotationShow(out bOTAContentViewModel);
  474. if (isTabItemAnnotation)
  475. {
  476. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  477. if (viewModel != null)
  478. {
  479. int pageindex = editEvent.PageIndex;
  480. int annotindex = editEvent.AnnotIndex;
  481. viewModel.UpdateModifiedAnnot(pageindex, annotindex, true);
  482. }
  483. }
  484. break;
  485. case ActionType.Modify:
  486. isTabItemAnnotation = IsTabItemAnnotationShow(out bOTAContentViewModel);
  487. if (isTabItemAnnotation)
  488. {
  489. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  490. if (viewModel != null)
  491. {
  492. int pageindex = editEvent.PageIndex;
  493. int annotindex = editEvent.AnnotIndex;
  494. viewModel.UpdateModifiedAnnot(pageindex, annotindex, false);
  495. }
  496. }
  497. break;
  498. case ActionType.TextEdit:
  499. break;
  500. default:
  501. break;
  502. }
  503. }
  504. }
  505. }
  506. private AnnotationContentViewModel GetAnnotationContentViewModel(BOTAContentViewModel bOTAContentViewModel)
  507. {
  508. AnnotationContentViewModel viewModel = null;
  509. if (bOTAContentViewModel != null)
  510. {
  511. if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName))
  512. {
  513. var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views;
  514. foreach (var item in views)
  515. {
  516. if (item is AnnotationContent annotation)
  517. {
  518. viewModel = annotation.DataContext as AnnotationContentViewModel;
  519. break;
  520. }
  521. }
  522. }
  523. }
  524. return viewModel;
  525. }
  526. private bool IsTabItemAnnotationShow(out BOTAContentViewModel bOTAContentViewModel)
  527. {
  528. bool isTabItemAnnotation = false;
  529. bOTAContentViewModel = null;
  530. if (region.Regions.ContainsRegionWithName(viewContentViewModel.BOTARegionName))
  531. {
  532. var views = region.Regions[viewContentViewModel.BOTARegionName].Views;
  533. foreach (var item in views)
  534. {
  535. if (item is BOTAContent bOTAContent)
  536. {
  537. bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  538. if (bOTAContentViewModel.CurrentBar == "TabItemAnnotation")
  539. {
  540. isTabItemAnnotation = true;
  541. break;
  542. }
  543. }
  544. }
  545. }
  546. return isTabItemAnnotation;
  547. }
  548. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  549. {
  550. if (e != null)
  551. {
  552. var annot = e.AnnotItemsList[0];
  553. if (annot != null)
  554. {
  555. if (e.AnnotItemsList.Count == 1 && e.IsAnnotCreateReset == false)
  556. {
  557. switch (annot.EventType)
  558. {
  559. case AnnotArgsType.AnnotHighlight:
  560. e.IsAnnotCreateReset = false;
  561. GetHighLight(annot as TextHighlightAnnotArgs);
  562. break;
  563. case AnnotArgsType.AnnotUnderline:
  564. GetUnderLine(annot as TextUnderlineAnnotArgs);
  565. break;
  566. case AnnotArgsType.AnnotStrikeout:
  567. GetStrikeout(annot as TextStrikeoutAnnotArgs);
  568. break;
  569. case AnnotArgsType.AnnotSquiggly:
  570. GetSquiggly(annot as TextSquigglyAnnotArgs);
  571. break;
  572. case AnnotArgsType.AnnotFreehand:
  573. GetFreehand(annot as FreehandAnnotArgs);
  574. break;
  575. case AnnotArgsType.AnnotFreeText:
  576. GetFreetext(annot as FreeTextAnnotArgs);
  577. break;
  578. case AnnotArgsType.AnnotSquare:
  579. GetRect(annot as SquareAnnotArgs);
  580. break;
  581. case AnnotArgsType.AnnotCircle:
  582. GetCircle(annot as CircleAnnotArgs);
  583. break;
  584. case AnnotArgsType.AnnotLine:
  585. bool isLine = true;
  586. if (e.Attribs.ContainsKey(AnnotAttrib.LineStart))
  587. {
  588. 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)
  589. {
  590. isLine = false;
  591. }
  592. }
  593. if (e.Attribs.ContainsKey(AnnotAttrib.LineEnd))
  594. {
  595. 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)
  596. {
  597. isLine = false;
  598. }
  599. }
  600. if (isLine)
  601. GetArrowLine("Line", annot as LineAnnotArgs);
  602. else
  603. GetArrowLine("Arrow", annot as LineAnnotArgs);
  604. break;
  605. case AnnotArgsType.AnnotLink:
  606. viewContentViewModel.IsCreateLink = false;
  607. GetLink(annot as LinkAnnotArgs, e);
  608. break;
  609. case AnnotArgsType.AnnotSticky:
  610. GetStickyNote(annot as StickyAnnotArgs);
  611. break;
  612. }
  613. ShowPropertyPanel();
  614. }
  615. else if (e.AnnotItemsList.Count == 1 && e.IsAnnotCreateReset == true)
  616. {
  617. switch (annot.EventType)
  618. {
  619. case AnnotArgsType.AnnotLink:
  620. viewContentViewModel.IsCreateLink = false;
  621. GetLink(annot as LinkAnnotArgs, e);
  622. break;
  623. //case AnnotArgsType.AnnotStamp://图章
  624. // GetStamp();
  625. // break;
  626. //case AnnotArgsType.AnnotStamp://签名
  627. // annotArgs = GetSignature();
  628. // isTemplateAnnot = true;
  629. // break;
  630. }
  631. ShowPropertyPanel();
  632. }
  633. }
  634. }
  635. else
  636. {
  637. // PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  638. // viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  639. }
  640. }
  641. private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  642. {
  643. if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool))
  644. return;
  645. // var annotlist = e.AnnotEventArgsList;
  646. switch (e.CommandType)
  647. {
  648. case CommandType.Context:
  649. if (e.AnnotEventArgsList.Count > 0)
  650. {
  651. if (App.mainWindowViewModel.SelectedItem.IsInReadctonMode && e.AnnotEventArgsList[0].EventType == AnnotArgsType.AnnotRedaction)
  652. {
  653. //绑定标记密文处右键菜单
  654. events.GetEvent<RedactionCommandEvent>().Publish(new RedactionCommandEventArgs() { UniCode = App.mainWindowViewModel.SelectedItem.Unicode, Sender = sender, args = e });
  655. }
  656. else
  657. {
  658. e.PopupMenu = SelectAnnotContextMenu(sender);
  659. }
  660. if (e.PopupMenu != null)
  661. {
  662. e.Handle = true;
  663. }
  664. }
  665. else
  666. {
  667. if (e.PressOnSelectedText)
  668. {
  669. e.PopupMenu = NoneSelectAnnotContextMenu(sender, e);
  670. if (e.PopupMenu != null)
  671. {
  672. e.Handle = true;
  673. }
  674. }
  675. else
  676. {
  677. e.PopupMenu = ViewerContextMenu();
  678. if (e.PopupMenu != null)
  679. {
  680. e.Handle = true;
  681. }
  682. }
  683. }
  684. break;
  685. }
  686. }
  687. private ContextMenu ViewerContextMenu()
  688. {
  689. ContextMenu contextMenu = App.Current.FindResource("ViewerContextMenu") as ContextMenu;
  690. contextMenu.Loaded += ContextMenu_Loaded;
  691. return contextMenu;
  692. }
  693. private void ContextMenu_Loaded(object sender, RoutedEventArgs e)
  694. {
  695. ContextMenu contextMenu = sender as ContextMenu;
  696. if (contextMenu.Items.Count > 0)
  697. {
  698. //粘贴
  699. MenuItem menuItem = contextMenu.Items[0] as MenuItem;
  700. if (!ApplicationCommands.Paste.CanExecute(null, (UIElement)sender))
  701. {
  702. menuItem.IsEnabled = false;
  703. menuItem.Opacity = 0.5;
  704. }
  705. else
  706. {
  707. menuItem.IsEnabled = true;
  708. menuItem.Opacity = 1;
  709. }
  710. //添加注释 >
  711. MenuItem menuItem1 = contextMenu.Items[2] as MenuItem;
  712. if (menuItem1.Items.Count > 0)
  713. {
  714. SetAddAnnotation(menuItem1.Items);
  715. }
  716. //隐藏注释 >
  717. MenuItem menuItem2 = contextMenu.Items[3] as MenuItem;
  718. //显示注释 >
  719. MenuItem menuItem3 = contextMenu.Items[4] as MenuItem;
  720. if (menuItem2.Visibility == Visibility.Visible)
  721. {
  722. menuItem3.Visibility = Visibility.Collapsed;
  723. }
  724. else
  725. {
  726. menuItem3.Visibility = Visibility.Visible;
  727. }
  728. //添加书签 >
  729. MenuItem menuItem4 = contextMenu.Items[5] as MenuItem;
  730. //删除书签 >
  731. MenuItem menuItem5 = contextMenu.Items[6] as MenuItem;
  732. if (menuItem4.Visibility == Visibility.Visible)
  733. {
  734. menuItem5.Visibility = Visibility.Collapsed;
  735. }
  736. else
  737. {
  738. menuItem5.Visibility = Visibility.Visible;
  739. }
  740. //工具模式 >
  741. MenuItem menuItem6 = contextMenu.Items[8] as MenuItem;
  742. //进入阅读模式 >
  743. MenuItem menuItem7 = contextMenu.Items[9] as MenuItem;
  744. //退出阅读模式 >
  745. MenuItem menuItem8 = contextMenu.Items[10] as MenuItem;
  746. if (App.IsBookMode)
  747. {
  748. menuItem7.Visibility = Visibility.Visible;
  749. }
  750. else
  751. {
  752. menuItem7.Visibility = Visibility.Collapsed;
  753. }
  754. if (menuItem7.Visibility == Visibility.Visible)
  755. {
  756. menuItem8.Visibility = Visibility.Collapsed;
  757. }
  758. else
  759. {
  760. menuItem8.Visibility = Visibility.Visible;
  761. }
  762. //视图缩放 >
  763. MenuItem menuItem9 = contextMenu.Items[12] as MenuItem;
  764. //页面显示 >
  765. MenuItem menuItem10 = contextMenu.Items[13] as MenuItem;
  766. //查找
  767. MenuItem menuItem11 = contextMenu.Items[15] as MenuItem;
  768. //打印...
  769. MenuItem menuItem12 = contextMenu.Items[16] as MenuItem;
  770. }
  771. }
  772. private void SetAddAnnotation(ItemCollection items)
  773. {
  774. foreach (var item in items)
  775. {
  776. if (item is MenuItem menuItem)
  777. {
  778. menuItem.CommandParameter = item;
  779. menuItem.Command = SetAddAnnotationCommand;
  780. }
  781. }
  782. }
  783. /// <summary>
  784. /// 右键菜单-添加注释
  785. /// </summary>
  786. /// <param name="sender"></param>
  787. private void AddAnnotation_Click(object sender)
  788. {
  789. if (sender is MenuItem menuItem)
  790. {
  791. AnnotHandlerEventArgs annotHandler = null;
  792. string str = menuItem.Tag.ToString();
  793. if (str == AddAnnotType.AnnotFreehand.ToString())
  794. {
  795. annotHandler = GetFreehand();
  796. }
  797. if (str == AddAnnotType.AnnotFreeText.ToString())
  798. {
  799. annotHandler = GetFreetext();
  800. }
  801. if (str == AddAnnotType.AnnotSticky.ToString())
  802. {
  803. annotHandler = GetStickyNote();
  804. }
  805. if (str == AddAnnotType.AnnotSquare.ToString())
  806. {
  807. annotHandler = GetRect();
  808. }
  809. if (str == AddAnnotType.AnnotCircle.ToString())
  810. {
  811. annotHandler = GetCircle();
  812. }
  813. if (str == AddAnnotType.AnnotArrow.ToString())
  814. {
  815. annotHandler = GetArrowLine("Arrow");
  816. }
  817. if (str == AddAnnotType.AnnotLine.ToString())
  818. {
  819. annotHandler = GetArrowLine("Line");
  820. }
  821. if (str == AddAnnotType.AnnotLink.ToString())
  822. {
  823. annotHandler = GetLink();
  824. }
  825. if (str == AddAnnotType.AnnotStamp.ToString())
  826. {
  827. annotHandler = GetStamp();
  828. }
  829. if (str == AddAnnotType.AnnotAutograph.ToString())
  830. {
  831. annotHandler = GetSignature();
  832. }
  833. if (annotHandler != null)
  834. {
  835. annotHandler.Author = Settings.Default.AppProperties.Description.Author;
  836. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  837. PDFViewer.SetToolParam(annotHandler);
  838. ShowPropertyPanel(true);
  839. }
  840. }
  841. }
  842. private ContextMenu SelectAnnotContextMenu(object sender)
  843. {
  844. var popMenu = App.Current.FindResource("SelectAnnotContextMenu") as ContextMenu;
  845. if (popMenu != null && popMenu.Items.Count == 4)
  846. {
  847. //剪切
  848. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  849. menuItem.CommandTarget = (UIElement)sender;
  850. menuItem.Command = ApplicationCommands.Cut;
  851. //复制
  852. menuItem = popMenu.Items[1] as MenuItem;
  853. menuItem.CommandTarget = (UIElement)sender;
  854. menuItem.Command = ApplicationCommands.Copy;
  855. //删除
  856. menuItem = popMenu.Items[2] as MenuItem;
  857. menuItem.CommandTarget = (UIElement)sender;
  858. menuItem.Command = ApplicationCommands.Delete;
  859. //笔记
  860. //menuItem = popMenu.Items[2] as MenuItem;
  861. //menuItem.CommandTarget = (UIElement)sender;
  862. //menuItem.Command = ApplicationCommands.no;
  863. }
  864. return popMenu;
  865. }
  866. private ContextMenu NoneSelectAnnotContextMenu(object sender, AnnotCommandArgs annotCommand)
  867. {
  868. var popMenu = new ContextMenu();
  869. MenuItem menuItem = new MenuItem();
  870. menuItem.CommandTarget = (UIElement)sender;
  871. menuItem.Command = ApplicationCommands.Paste;
  872. popMenu.Items.Add(menuItem);
  873. menuItem = new MenuItem();
  874. menuItem.CommandTarget = (UIElement)sender;
  875. menuItem.Command = ApplicationCommands.Copy;
  876. popMenu.Items.Add(menuItem);
  877. menuItem = new MenuItem();
  878. menuItem.CommandTarget = (UIElement)sender;
  879. menuItem.Header = "创建链接";
  880. menuItem.CommandParameter = new object[] { this, annotCommand };
  881. menuItem.Command = viewContentViewModel.CreateLinkCommand;
  882. menuItem.Click += MenuItem_Click;
  883. popMenu.Items.Add(menuItem);
  884. return popMenu;
  885. }
  886. private void MenuItem_Click(object sender, RoutedEventArgs e)
  887. {
  888. var args = PDFViewer.ToolManager.CurrentAnnotArgs;
  889. BtnLinkIsChecked = true;
  890. }
  891. /// <summary>
  892. /// 展开显示属性面板
  893. /// </summary>
  894. private void ShowPropertyPanel(bool show = true)
  895. {
  896. viewContentViewModel.IsPropertyOpen = show;
  897. }
  898. }
  899. }