AnnotToolContentViewModel.cs 41 KB

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