AnnotToolContentViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. PropertyRegionName = Guid.NewGuid().ToString();
  59. }
  60. #endregion
  61. #region Command实现
  62. //点击注释工具
  63. private void BtnMyTools_Click(CustomIconToggleBtn annotBtn)
  64. {
  65. //不创建注释,属于注释模板
  66. bool isTemplateAnnot = false;
  67. bool isSnapshotEdit = false;
  68. AnnotHandlerEventArgs annotArgs = null;
  69. if (annotBtn.IsChecked == true)
  70. {
  71. var tag = annotBtn.Tag.ToString();
  72. FindAnnotTypeKey(tag, ref annotArgs);
  73. if (tag == "SnapshotEdit")
  74. {
  75. isSnapshotEdit = true;
  76. }
  77. else if (tag == "Signature" || tag == "Stamp")
  78. {
  79. isTemplateAnnot = true;
  80. }
  81. if (annotArgs != null)
  82. {
  83. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  84. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  85. PDFViewer.SetToolParam(annotArgs);
  86. }
  87. }
  88. //当不是注释模板,且无创建注释时,属性面板显示为空内容
  89. if (isTemplateAnnot == false && annotArgs == null)
  90. {
  91. PDFViewer.SetMouseMode(MouseModes.PanTool);
  92. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  93. }
  94. if (isSnapshotEdit == true)
  95. {
  96. ShowPropertyPanel(false);
  97. isSnapshotEdit = false;
  98. }
  99. else
  100. {
  101. ShowPropertyPanel((bool)annotBtn.IsChecked);
  102. }
  103. }
  104. /// <summary>
  105. /// 右键菜单-添加注释
  106. /// </summary>
  107. /// <param name="sender"></param>
  108. private void AddAnnotation_Click(object sender)
  109. {
  110. if (sender is MenuItem menuItem)
  111. {
  112. AnnotHandlerEventArgs annotHandler = null;
  113. string str = menuItem.Tag.ToString();
  114. if (str == AddAnnotType.AnnotFreehand.ToString())
  115. {
  116. annotHandler = GetFreehand();
  117. }
  118. if (str == AddAnnotType.AnnotFreeText.ToString())
  119. {
  120. annotHandler = GetFreetext();
  121. }
  122. if (str == AddAnnotType.AnnotSticky.ToString())
  123. {
  124. annotHandler = GetStickyNote();
  125. }
  126. if (str == AddAnnotType.AnnotSquare.ToString())
  127. {
  128. annotHandler = GetRect();
  129. }
  130. if (str == AddAnnotType.AnnotCircle.ToString())
  131. {
  132. annotHandler = GetCircle();
  133. }
  134. if (str == AddAnnotType.AnnotArrow.ToString())
  135. {
  136. annotHandler = GetArrowLine("Arrow");
  137. }
  138. if (str == AddAnnotType.AnnotLine.ToString())
  139. {
  140. annotHandler = GetArrowLine("Line");
  141. }
  142. if (str == AddAnnotType.AnnotLink.ToString())
  143. {
  144. annotHandler = GetLink();
  145. }
  146. if (str == AddAnnotType.AnnotStamp.ToString())
  147. {
  148. annotHandler = GetStamp();
  149. }
  150. if (str == AddAnnotType.AnnotAutograph.ToString())
  151. {
  152. annotHandler = GetSignature();
  153. }
  154. if (annotHandler != null)
  155. {
  156. annotHandler.Author = Settings.Default.AppProperties.Description.Author;
  157. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  158. PDFViewer.SetToolParam(annotHandler);
  159. ShowPropertyPanel(true);
  160. }
  161. }
  162. }
  163. #endregion
  164. #region BOTA
  165. private AnnotationContentViewModel GetAnnotationContentViewModel(BOTAContentViewModel bOTAContentViewModel)
  166. {
  167. AnnotationContentViewModel viewModel = null;
  168. if (bOTAContentViewModel != null)
  169. {
  170. if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName))
  171. {
  172. var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views;
  173. foreach (var item in views)
  174. {
  175. if (item is AnnotationContent annotation)
  176. {
  177. viewModel = annotation.DataContext as AnnotationContentViewModel;
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. return viewModel;
  184. }
  185. /// <summary>
  186. /// 判断Bota的TabItem是否已显示
  187. /// </summary>
  188. /// <param name="bOTAContentViewModel"></param>
  189. /// <param name="bOTAContent"></param>
  190. /// <param name="tabItemText"></param>
  191. /// <returns></returns>
  192. private bool IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, string tabItemText)
  193. {
  194. bool isTabItem = false;
  195. bOTAContentViewModel = null;
  196. bOTAContent = null;
  197. if (region.Regions.ContainsRegionWithName(viewContentViewModel.BOTARegionName))
  198. {
  199. var views = region.Regions[viewContentViewModel.BOTARegionName].Views;
  200. var model = views.FirstOrDefault(q => q is BOTAContent);
  201. if (model is BOTAContent bOTAContent1)
  202. {
  203. bOTAContent = bOTAContent1;
  204. bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  205. if (bOTAContentViewModel.CurrentBar == tabItemText)
  206. {
  207. isTabItem = true;
  208. }
  209. }
  210. //foreach (var item in views)
  211. //{
  212. // if (item is BOTAContent bOTAContent1)
  213. // {
  214. // bOTAContent = bOTAContent1;
  215. // bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  216. // if (bOTAContentViewModel.CurrentBar == tabItemText)
  217. // {
  218. // isTabItem = true;
  219. // break;
  220. // }
  221. // }
  222. //}
  223. }
  224. return isTabItem;
  225. }
  226. private void Select_Click(object sender, RoutedEventArgs e)
  227. {
  228. if (viewContentViewModel.OpenBOTA == false)
  229. {
  230. viewContentViewModel.OpenBOTA = true;
  231. }
  232. bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemSearch");
  233. if (isBook == false)
  234. {
  235. bOTAContent.TabItemSearch.IsSelected = true;
  236. }
  237. }
  238. #endregion
  239. #region 阅读页右键菜单
  240. private void PageDisplay(ItemCollection items)
  241. {
  242. foreach (var item in items)
  243. {
  244. if (item is MenuItem menuItem)
  245. {
  246. menuItem.Click -= PageDisplay_Click;
  247. menuItem.Click += PageDisplay_Click;
  248. }
  249. }
  250. }
  251. private void PageDisplay_Click(object sender, RoutedEventArgs e)
  252. {
  253. if (sender is MenuItem menuItem)
  254. {
  255. switch (menuItem.Tag.ToString())
  256. {
  257. case "SingleView":
  258. PDFViewer.ChangeViewMode(ViewMode.Single);
  259. break;
  260. case "SingleContinueView":
  261. PDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
  262. break;
  263. case "DoubleView":
  264. PDFViewer.ChangeViewMode(ViewMode.Double);
  265. break;
  266. case "DoubleContinueView":
  267. PDFViewer.ChangeViewMode(ViewMode.DoubleContinuous);
  268. break;
  269. case "BookMode":
  270. PDFViewer.ChangeViewMode(ViewMode.Book);
  271. break;
  272. }
  273. }
  274. }
  275. private void ViewZoom(ItemCollection items)
  276. {
  277. foreach (var item in items)
  278. {
  279. if (item is MenuItem menuItem)
  280. {
  281. menuItem.Click -= PageDisplay_Click;
  282. menuItem.Click += ViewZoom_Click;
  283. }
  284. }
  285. }
  286. private void ViewZoom_Click(object sender, RoutedEventArgs e)
  287. {
  288. if (sender is MenuItem menuItem)
  289. {
  290. switch (menuItem.Tag.ToString())
  291. {
  292. case "FitWidth":
  293. PDFViewer.ChangeFitMode(FitMode.FitWidth);
  294. break;
  295. case "FitHeight":
  296. PDFViewer.ChangeFitMode(FitMode.FitHeight);
  297. break;
  298. case "FitSize":
  299. PDFViewer.ChangeFitMode(FitMode.FitSize);
  300. break;
  301. case "enlarge":
  302. double newZoom = viewContentViewModel.CheckZoomLevel((PDFViewer.ZoomFactor + 0.01), true);
  303. PDFViewer.Zoom(newZoom);
  304. break;
  305. case "narrow":
  306. newZoom = viewContentViewModel.CheckZoomLevel((PDFViewer.ZoomFactor + -0.01), false);
  307. PDFViewer.Zoom(newZoom);
  308. break;
  309. }
  310. }
  311. }
  312. private void UnReadModel_Click(object sender, RoutedEventArgs e)
  313. {
  314. viewContentViewModel.UnReadModel();
  315. }
  316. private void ReadModel_Click(object sender, RoutedEventArgs e)
  317. {
  318. viewContentViewModel.RbtnReadMode();
  319. }
  320. private void SetToolMode(ItemCollection items)
  321. {
  322. foreach (var item in items)
  323. {
  324. if (item is MenuItem menuItem)
  325. {
  326. menuItem.Click -= PageDisplay_Click;
  327. menuItem.Click += ToolMode_Click;
  328. }
  329. }
  330. }
  331. private void ToolMode_Click(object sender, RoutedEventArgs e)
  332. {
  333. if (sender is MenuItem menuItem)
  334. {
  335. switch (menuItem.Tag.ToString())
  336. {
  337. case "default":
  338. PDFViewer.SetMouseMode(MouseModes.None);
  339. //viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  340. BtnHandIsChecked = true;
  341. ShowPropertyPanel(false);
  342. break;
  343. case "roll":
  344. PDFViewer.SetMouseMode(MouseModes.PanTool);
  345. //viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  346. BtnHandIsChecked = true;
  347. ShowPropertyPanel(false);
  348. break;
  349. case "content":
  350. AnnotHandlerEventArgs annotHandler = GetSnapshotEdit();
  351. BtnSelecttoolIsChecked = true;
  352. if (annotHandler != null)
  353. {
  354. annotHandler.Author = Settings.Default.AppProperties.Description.Author;
  355. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  356. PDFViewer.SetToolParam(annotHandler);
  357. ShowPropertyPanel(false);
  358. }
  359. break;
  360. default:
  361. break;
  362. }
  363. }
  364. }
  365. private void BookMark(MenuItem addBookMark, MenuItem delBookMark)
  366. {
  367. int index = PDFViewer.CurrentIndex;
  368. //检测是否已存在相同数据
  369. CPDFBookmark list = PDFViewer.Document.GetBookmarkList().FirstOrDefault(q => q.PageIndex == index);
  370. if (list != null)
  371. {
  372. isAddBookMark = false;
  373. }
  374. else
  375. {
  376. isAddBookMark = true;
  377. }
  378. if (isAddBookMark)
  379. {
  380. addBookMark.Visibility = Visibility.Visible;
  381. delBookMark.Visibility = Visibility.Collapsed;
  382. addBookMark.Click += AddBookMark_Click;
  383. }
  384. else
  385. {
  386. delBookMark.Visibility = Visibility.Visible;
  387. addBookMark.Visibility = Visibility.Collapsed;
  388. delBookMark.Click += DelBookMark_Click;
  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(object sender, RoutedEventArgs e)
  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 void DisplayAnnot_Click(object sender, RoutedEventArgs e)
  460. {
  461. PDFViewer.SetDrawAnnot(true);
  462. isHiddenAnnot = true;
  463. }
  464. private void HiddenAnnot_Click(object sender, RoutedEventArgs e)
  465. {
  466. PDFViewer.SetDrawAnnot(false);
  467. isHiddenAnnot = false;
  468. }
  469. private void SetAddAnnotation(ItemCollection items)
  470. {
  471. foreach (var item in items)
  472. {
  473. if (item is MenuItem menuItem)
  474. {
  475. menuItem.CommandParameter = item;
  476. menuItem.Command = SetAddAnnotationCommand;
  477. }
  478. }
  479. }
  480. private void MenuItem_Click(object sender, RoutedEventArgs e)
  481. {
  482. var args = PDFViewer.ToolManager.CurrentAnnotArgs;
  483. BtnLinkIsChecked = true;
  484. }
  485. #endregion
  486. #region Navigation
  487. public bool IsNavigationTarget(NavigationContext navigationContext)
  488. {
  489. return true;
  490. }
  491. public void OnNavigatedFrom(NavigationContext navigationContext)
  492. {
  493. UnBindingPDFViewerHandler();
  494. UnBindingEvent();
  495. }
  496. public void OnNavigatedTo(NavigationContext navigationContext)
  497. {
  498. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  499. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  500. BindingPDFViewerHandler();
  501. }
  502. #endregion
  503. }
  504. }