AnnotToolContentViewModel.cs 22 KB

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