AnnotToolContentViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKitViewer;
  4. using ComPDFKitViewer.AnnotEvent;
  5. using ComPDFKitViewer.PdfViewer;
  6. using Microsoft.Office.Interop.Excel;
  7. using Microsoft.Office.Interop.Word;
  8. using Microsoft.Win32;
  9. using PDF_Office.CustomControl;
  10. using PDF_Office.EventAggregators;
  11. using PDF_Office.Helper;
  12. using PDF_Office.Model;
  13. using PDF_Office.Properties;
  14. using PDF_Office.ViewModels.BOTA;
  15. using PDF_Office.ViewModels.PropertyPanel;
  16. using PDF_Office.ViewModels.PropertyPanel.AnnotPanel;
  17. using PDF_Office.Views.BOTA;
  18. using PDF_Office.Views.PropertyPanel.AnnotPanel;
  19. using PDFSettings;
  20. using Prism.Commands;
  21. using Prism.Events;
  22. using Prism.Mvvm;
  23. using Prism.Regions;
  24. using Prism.Services.Dialogs;
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Linq;
  28. using System.Reflection;
  29. using System.Text;
  30. using System.Threading.Tasks;
  31. using System.Windows;
  32. using System.Windows.Controls;
  33. using System.Windows.Controls.Primitives;
  34. using System.Windows.Forms;
  35. using System.Windows.Input;
  36. using System.Windows.Media;
  37. using ContextMenu = System.Windows.Controls.ContextMenu;
  38. using MenuItem = System.Windows.Controls.MenuItem;
  39. namespace PDF_Office.ViewModels.Tools
  40. {
  41. public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware
  42. {
  43. #region 初始化
  44. public AnnotToolContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogService)
  45. {
  46. region = regionManager;
  47. events = eventAggregator;
  48. dialogs = dialogService;
  49. InitCommand();
  50. BindingEvent();
  51. InitDefaultValue();
  52. InitToolDict();
  53. }
  54. private void InitCommand()
  55. {
  56. MyToolsCommand = new DelegateCommand<CustomIconToggleBtn>(BtnMyTools_Click);
  57. SetAddAnnotationCommand = new DelegateCommand<object>(AddAnnotation_Click);
  58. AddBookMarkCommand = new DelegateCommand(AddBookMark_Click);
  59. PropertyRegionName = Guid.NewGuid().ToString();
  60. #region 注释 - 右键菜单
  61. //公共command
  62. AnnotDefaultValue_MenuCommand = new DelegateCommand<object>(AnnotDefaultValues_Menu);
  63. AnnotColorPalette_MenuCommand = new DelegateCommand<object>(AnnotColorPalette_Menu);
  64. AnnotAddNoteText_MenuCommand = new DelegateCommand<object>(AnnotAddNoteText_Menu);
  65. //高亮、下划线、删除
  66. HightAnnotCopyText_MenuCommand = new DelegateCommand<object>(HightAnnotCopyText_Menu);
  67. //手绘
  68. FreeHandLineStyle_MenuCommand = new DelegateCommand<object>(FreeHandLineStyle_Menu);
  69. //文本
  70. FreeTextFontFamily_MenuCommand = new DelegateCommand<object>(FreeTextFontFamily_Menu);
  71. FreeTextAglin_MenuCommand = new DelegateCommand<object>(FreeTextAglin_Menu);
  72. //便签
  73. StrikeNoteEditStrike_MenuCommand = new DelegateCommand<object>(StrikeNoteEditStrike_Menu);
  74. //形状
  75. ShapeLineStyle_MenuCommand = new DelegateCommand<object>(ShapeLineStyle_Menu);
  76. ShapeLineDirect_MenuCommand = new DelegateCommand<object>(ShapeLineDirect_Menu);
  77. #endregion 注释 - 右键菜单
  78. }
  79. #endregion 初始化
  80. #region Command实现
  81. //点击注释工具
  82. private void BtnMyTools_Click(CustomIconToggleBtn annotBtn)
  83. {
  84. //不创建注释,属于注释模板
  85. bool isTemplateAnnot = false;
  86. bool isSnapshotEdit = false;
  87. AnnotHandlerEventArgs annotArgs = null;
  88. if (annotBtn.IsChecked == true)
  89. {
  90. var tag = annotBtn.Tag.ToString();
  91. FindAnnotTypeKey(tag, ref annotArgs);
  92. if (tag == "SnapshotEdit")
  93. {
  94. isSnapshotEdit = true;
  95. }
  96. else if (tag == "Signature" || tag == "Stamp")
  97. {
  98. isTemplateAnnot = true;
  99. }
  100. if (annotArgs != null)
  101. {
  102. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  103. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  104. PDFViewer.SetToolParam(annotArgs);
  105. isRightMenuAddAnnot = false;
  106. }
  107. }
  108. //当不是注释模板,且无创建注释时,属性面板显示为空内容
  109. if (isTemplateAnnot == false && annotArgs == null)
  110. {
  111. PDFViewer.SetMouseMode(MouseModes.PanTool);
  112. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  113. }
  114. else
  115. {
  116. if (isSnapshotEdit == true)
  117. {
  118. ShowPropertyPanel(false);
  119. isSnapshotEdit = false;
  120. }
  121. else
  122. {
  123. ShowPropertyPanel((bool)annotBtn.IsChecked);
  124. }
  125. }
  126. }
  127. /// <summary>
  128. /// 右键菜单-添加注释
  129. /// </summary>
  130. /// <param name="sender"></param>
  131. private void AddAnnotation_Click(object sender)
  132. {
  133. if (sender is MenuItem menuItem)
  134. {
  135. AnnotHandlerEventArgs annotHandler = null;
  136. string str = menuItem.Tag.ToString();
  137. if (str == AddAnnotType.AnnotFreehand.ToString())
  138. {
  139. annotHandler = GetFreehand();
  140. }
  141. if (str == AddAnnotType.AnnotFreeText.ToString())
  142. {
  143. annotHandler = GetFreetext();
  144. }
  145. if (str == AddAnnotType.AnnotSticky.ToString())
  146. {
  147. annotHandler = GetStickyNote();
  148. }
  149. if (str == AddAnnotType.AnnotSquare.ToString())
  150. {
  151. annotHandler = GetRect();
  152. }
  153. if (str == AddAnnotType.AnnotCircle.ToString())
  154. {
  155. annotHandler = GetCircle();
  156. }
  157. if (str == AddAnnotType.AnnotArrow.ToString())
  158. {
  159. annotHandler = GetArrowLine("Arrow");
  160. }
  161. if (str == AddAnnotType.AnnotLine.ToString())
  162. {
  163. annotHandler = GetArrowLine("Line");
  164. }
  165. if (str == AddAnnotType.AnnotLink.ToString())
  166. {
  167. annotHandler = GetLink();
  168. }
  169. if (str == AddAnnotType.AnnotStamp.ToString())
  170. {
  171. annotHandler = GetStamp();
  172. }
  173. if (str == AddAnnotType.AnnotAutograph.ToString())
  174. {
  175. annotHandler = GetSignature();
  176. }
  177. if (annotHandler != null)
  178. {
  179. annotHandler.Author = Settings.Default.AppProperties.Description.Author;
  180. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  181. PDFViewer.SetToolParam(annotHandler);
  182. isRightMenuAddAnnot = true;
  183. ShowPropertyPanel(false);
  184. }
  185. }
  186. }
  187. #endregion Command实现
  188. #region BOTA
  189. private AnnotationContentViewModel GetAnnotationContentViewModel(BOTAContentViewModel bOTAContentViewModel, out AnnotationContent annotation)
  190. {
  191. AnnotationContentViewModel viewModel = null;
  192. annotation = null;
  193. if (bOTAContentViewModel != null)
  194. {
  195. if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName))
  196. {
  197. var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views;
  198. foreach (var item in views)
  199. {
  200. if (item is AnnotationContent annotation1)
  201. {
  202. annotation = annotation1;
  203. viewModel = annotation1.DataContext as AnnotationContentViewModel;
  204. break;
  205. }
  206. }
  207. }
  208. }
  209. return viewModel;
  210. }
  211. /// <summary>
  212. /// 判断Bota的TabItem是否已显示
  213. /// </summary>
  214. /// <param name="bOTAContentViewModel"></param>
  215. /// <param name="bOTAContent"></param>
  216. /// <param name="tabItemText"></param>
  217. /// <returns></returns>
  218. private bool IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, string tabItemText)
  219. {
  220. bool isTabItem = false;
  221. bOTAContentViewModel = null;
  222. bOTAContent = null;
  223. if (region.Regions.ContainsRegionWithName(viewContentViewModel.BOTARegionName))
  224. {
  225. var views = region.Regions[viewContentViewModel.BOTARegionName].Views;
  226. var model = views.FirstOrDefault(q => q is BOTAContent);
  227. if (model is BOTAContent bOTAContent1)
  228. {
  229. bOTAContent = bOTAContent1;
  230. bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  231. if (bOTAContentViewModel.CurrentBar == tabItemText)
  232. {
  233. isTabItem = true;
  234. }
  235. }
  236. //foreach (var item in views)
  237. //{
  238. // if (item is BOTAContent bOTAContent1)
  239. // {
  240. // bOTAContent = bOTAContent1;
  241. // bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  242. // if (bOTAContentViewModel.CurrentBar == tabItemText)
  243. // {
  244. // isTabItem = true;
  245. // break;
  246. // }
  247. // }
  248. //}
  249. }
  250. return isTabItem;
  251. }
  252. private void Select_Click(object sender, RoutedEventArgs e)
  253. {
  254. if (viewContentViewModel.OpenBOTA == false)
  255. {
  256. viewContentViewModel.OpenBOTA = true;
  257. }
  258. bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemSearch");
  259. if (isBook == false)
  260. {
  261. bOTAContent.TabItemSearch.IsSelected = true;
  262. }
  263. }
  264. #endregion BOTA
  265. #region 阅读页右键菜单
  266. private void PageDisplay(ItemCollection items)
  267. {
  268. foreach (var item in items)
  269. {
  270. if (item is MenuItem menuItem)
  271. {
  272. menuItem.Click -= PageDisplay_Click;
  273. menuItem.Click += PageDisplay_Click;
  274. }
  275. }
  276. }
  277. private void PageDisplay_Click(object sender, RoutedEventArgs e)
  278. {
  279. if (sender is MenuItem menuItem)
  280. {
  281. switch (menuItem.Tag.ToString())
  282. {
  283. case "SingleView":
  284. PDFViewer.ChangeViewMode(ViewMode.Single);
  285. break;
  286. case "SingleContinueView":
  287. PDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
  288. break;
  289. case "DoubleView":
  290. PDFViewer.ChangeViewMode(ViewMode.Double);
  291. break;
  292. case "DoubleContinueView":
  293. PDFViewer.ChangeViewMode(ViewMode.DoubleContinuous);
  294. break;
  295. case "BookMode":
  296. PDFViewer.ChangeViewMode(ViewMode.Book);
  297. break;
  298. }
  299. }
  300. }
  301. private void ViewZoom(ItemCollection items)
  302. {
  303. foreach (var item in items)
  304. {
  305. if (item is MenuItem menuItem)
  306. {
  307. menuItem.Click -= PageDisplay_Click;
  308. menuItem.Click += ViewZoom_Click;
  309. }
  310. }
  311. }
  312. private void ViewZoom_Click(object sender, RoutedEventArgs e)
  313. {
  314. if (sender is MenuItem menuItem)
  315. {
  316. switch (menuItem.Tag.ToString())
  317. {
  318. case "FitWidth":
  319. PDFViewer.ChangeFitMode(FitMode.FitWidth);
  320. break;
  321. case "FitHeight":
  322. PDFViewer.ChangeFitMode(FitMode.FitHeight);
  323. break;
  324. case "FitSize":
  325. PDFViewer.ChangeFitMode(FitMode.FitSize);
  326. break;
  327. case "enlarge":
  328. double newZoom = viewContentViewModel.CheckZoomLevel((PDFViewer.ZoomFactor + 0.01), true);
  329. PDFViewer.Zoom(newZoom);
  330. break;
  331. case "narrow":
  332. newZoom = viewContentViewModel.CheckZoomLevel((PDFViewer.ZoomFactor + -0.01), false);
  333. PDFViewer.Zoom(newZoom);
  334. break;
  335. }
  336. }
  337. }
  338. private void UnReadModel_Click(object sender, RoutedEventArgs e)
  339. {
  340. viewContentViewModel.UnReadModel();
  341. }
  342. private void ReadModel_Click(object sender, RoutedEventArgs e)
  343. {
  344. viewContentViewModel.RbtnReadMode();
  345. }
  346. private void SetToolMode(ItemCollection items)
  347. {
  348. foreach (var item in items)
  349. {
  350. if (item is MenuItem menuItem)
  351. {
  352. menuItem.Click -= PageDisplay_Click;
  353. menuItem.Click += ToolMode_Click;
  354. }
  355. }
  356. }
  357. private void ToolMode_Click(object sender, RoutedEventArgs e)
  358. {
  359. if (sender is MenuItem menuItem)
  360. {
  361. switch (menuItem.Tag.ToString())
  362. {
  363. case "default":
  364. PDFViewer.SetMouseMode(MouseModes.None);
  365. //viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  366. BtnHandIsChecked = true;
  367. ShowPropertyPanel(false);
  368. break;
  369. case "roll":
  370. PDFViewer.SetMouseMode(MouseModes.PanTool);
  371. //viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  372. BtnHandIsChecked = true;
  373. ShowPropertyPanel(false);
  374. break;
  375. case "content":
  376. AnnotHandlerEventArgs annotHandler = GetSnapshotEdit();
  377. BtnSelecttoolIsChecked = true;
  378. if (annotHandler != null)
  379. {
  380. annotHandler.Author = Settings.Default.AppProperties.Description.Author;
  381. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  382. PDFViewer.SetToolParam(annotHandler);
  383. ShowPropertyPanel(false);
  384. }
  385. break;
  386. default:
  387. break;
  388. }
  389. }
  390. }
  391. private void DelBookMark_Click(object sender, RoutedEventArgs e)
  392. {
  393. if (viewContentViewModel.OpenBOTA == false)
  394. {
  395. viewContentViewModel.OpenBOTA = true;
  396. }
  397. bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemBookMark");
  398. if (isBook == false)
  399. {
  400. bOTAContent.TabItemBookMark.IsSelected = true;
  401. }
  402. BookmarkContentViewModel bookmarkContentViewModel = GetBookmarkContentViewModel(bOTAContentViewModel, out BookmarkContent bookmark);
  403. int index = PDFViewer.CurrentIndex;
  404. //检测是否已存在相同数据
  405. CPDFBookmark list = PDFViewer.Document.GetBookmarkList().FirstOrDefault(q => q.PageIndex == index);
  406. if (bookmarkContentViewModel != null && bookmark != null && bOTAContent != null && list != null)
  407. {
  408. bookmarkContentViewModel.DeleteCommand.Execute(list);
  409. isAddBookMark = bookmarkContentViewModel.isAddBookMark;
  410. }
  411. // bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemBookMark");
  412. //if (isBook == false || viewContentViewModel.OpenBOTA == false)
  413. //{
  414. // viewContentViewModel.OpenBOTA = true;
  415. // bOTAContent.TabItemBookMark.IsSelected = true;
  416. //}
  417. }
  418. private void AddBookMark_Click()
  419. {
  420. if (viewContentViewModel.OpenBOTA == false)
  421. {
  422. viewContentViewModel.OpenBOTA = true;
  423. }
  424. bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemBookMark");
  425. if (isBook == false)
  426. {
  427. bOTAContent.TabItemBookMark.IsSelected = true;
  428. }
  429. BookmarkContentViewModel bookmarkContentViewModel = GetBookmarkContentViewModel(bOTAContentViewModel, out BookmarkContent bookmark);
  430. if (bookmarkContentViewModel != null && bookmark != null && bOTAContent != null)
  431. {
  432. bookmarkContentViewModel.AddBookmarkCommand.Execute(bookmark.BookMarkListView);
  433. isAddBookMark = bookmarkContentViewModel.isAddBookMark;
  434. return;
  435. }
  436. }
  437. private BookmarkContentViewModel GetBookmarkContentViewModel(BOTAContentViewModel bOTAContentViewModel, out BookmarkContent bookmark)
  438. {
  439. BookmarkContentViewModel viewModel = null;
  440. bookmark = null;
  441. if (bOTAContentViewModel != null)
  442. {
  443. if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName))
  444. {
  445. var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views;
  446. foreach (var item in views)
  447. {
  448. if (item is BookmarkContent bookmark1)
  449. {
  450. bookmark = bookmark1;
  451. viewModel = bookmark1.DataContext as BookmarkContentViewModel;
  452. break;
  453. }
  454. }
  455. }
  456. }
  457. return viewModel;
  458. }
  459. private OutLineControlViewModel GetOutLineControlViewModel(BOTAContentViewModel bOTAContentViewModel, out OutLineControl outLineControl)
  460. {
  461. OutLineControlViewModel viewModel = null;
  462. outLineControl = null;
  463. if (bOTAContentViewModel != null)
  464. {
  465. if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName))
  466. {
  467. var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views;
  468. foreach (var item in views)
  469. {
  470. if (item is OutLineControl outLineControl1)
  471. {
  472. outLineControl = outLineControl1;
  473. viewModel = outLineControl1.DataContext as OutLineControlViewModel;
  474. break;
  475. }
  476. }
  477. }
  478. }
  479. return viewModel;
  480. }
  481. private void DisplayAnnot_Click(object sender, RoutedEventArgs e)
  482. {
  483. PDFViewer.SetDrawAnnot(true);
  484. isHiddenAnnot = true;
  485. }
  486. private void HiddenAnnot_Click(object sender, RoutedEventArgs e)
  487. {
  488. PDFViewer.SetDrawAnnot(false);
  489. isHiddenAnnot = false;
  490. }
  491. private void SetAddAnnotation(ItemCollection items)
  492. {
  493. foreach (var item in items)
  494. {
  495. if (item is MenuItem menuItem)
  496. {
  497. menuItem.CommandParameter = item;
  498. menuItem.Command = SetAddAnnotationCommand;
  499. }
  500. }
  501. }
  502. private void AddLink_Click(object sender, RoutedEventArgs e)
  503. {
  504. var args = PDFViewer.ToolManager.CurrentAnnotArgs;
  505. BtnLinkIsChecked = true;
  506. }
  507. #endregion 阅读页右键菜单
  508. #region Navigation
  509. public bool IsNavigationTarget(NavigationContext navigationContext)
  510. {
  511. return true;
  512. }
  513. public void OnNavigatedFrom(NavigationContext navigationContext)
  514. {
  515. UnBindingPDFViewerHandler();
  516. UnBindingEvent();
  517. }
  518. public void OnNavigatedTo(NavigationContext navigationContext)
  519. {
  520. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  521. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  522. BindingPDFViewerHandler();
  523. }
  524. #endregion Navigation
  525. }
  526. }