AnnotToolContentViewModel.cs 21 KB

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