AnnotToolContentViewModel.cs 23 KB

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