BottomToolContentViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. using Prism.Mvvm;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using ComPDFKitViewer.PdfViewer;
  8. using Prism.Commands;
  9. using Prism.Regions;
  10. using System.Windows;
  11. using PDF_Office.Views;
  12. using ComPDFKitViewer;
  13. using System.Windows.Input;
  14. using System.Windows.Controls;
  15. using Prism.Services.Dialogs;
  16. using PDF_Office.CustomControl;
  17. using PDF_Office.Model;
  18. using PDF_Office.ViewModels.BOTA;
  19. using PDF_Office.Views.BOTA;
  20. using PDF_Office.Views.PropertyPanel.AnnotPanel;
  21. using PDF_Office.Views.Tools;
  22. namespace PDF_Office.ViewModels
  23. {
  24. public class BottomToolContentViewModel : BindableBase, INavigationAware
  25. {
  26. public DelegateCommand Load { get; set; }
  27. public DelegateCommand FirstPageCommand { get; set; }
  28. public DelegateCommand LastPageCommand { get; set; }
  29. public DelegateCommand PrePageCommand { get; set; }
  30. public DelegateCommand NextPageCommand { get; set; }
  31. public DelegateCommand MyPropertyCommand { get; set; }
  32. public DelegateCommand FullScreenCommand { get; set; }
  33. public DelegateCommand ReadModeCommand { get; set; }
  34. public DelegateCommand OpenViewCommand { get; set; }
  35. public DelegateCommand ZoomInCommand { get; set; }
  36. public DelegateCommand ZoomOutCommand { get; set; }
  37. public DelegateCommand SetViewModeCommand { get; set; }
  38. public DelegateCommand OpenFullCommand { get; set; }
  39. public DelegateCommand<object> PageTextKeyDownCommand { get; set; }
  40. public DelegateCommand<object> PageTextPreviewKeyDownCommand { get; set; }
  41. public DelegateCommand<object> SelectionChangedCommand { get; set; }
  42. public DelegateCommand<object> ShowViewModularCommand { get; set; }
  43. /// <summary>
  44. /// 上一个视图
  45. /// </summary>
  46. public DelegateCommand PreViewCommand { get; set; }
  47. /// <summary>
  48. /// 下一个视图
  49. /// </summary>
  50. public DelegateCommand NextViewCommnad { get; set; }
  51. private bool isShowViewModular = false;
  52. /// <summary>
  53. /// 属性栏是否展开
  54. /// </summary>
  55. public bool IsShowViewModular
  56. {
  57. get { return isShowViewModular; }
  58. set
  59. {
  60. SetProperty(ref isShowViewModular, value);
  61. }
  62. }
  63. private bool isSingleView;
  64. public bool IsSingleView
  65. {
  66. get { return isSingleView; }
  67. set
  68. {
  69. SetProperty(ref isSingleView, value);
  70. if (value)
  71. {
  72. SetModeView();
  73. }
  74. }
  75. }
  76. private bool isContinue;
  77. public bool IsContinue
  78. {
  79. get { return isContinue; }
  80. set
  81. {
  82. SetProperty(ref isContinue, value);
  83. SetModeView();
  84. }
  85. }
  86. private bool isDoubleView;
  87. public bool IsDoubleView
  88. {
  89. get { return isDoubleView; }
  90. set
  91. {
  92. SetProperty(ref isDoubleView, value);
  93. if (value)
  94. {
  95. SetModeView();
  96. }
  97. }
  98. }
  99. private bool isBookMode;
  100. public bool IsBookMode
  101. {
  102. get { return isBookMode; }
  103. set
  104. {
  105. SetProperty(ref isBookMode, value);
  106. if (value)
  107. {
  108. SetModeView();
  109. }
  110. }
  111. }
  112. private int pageCount;
  113. public int PageCount
  114. {
  115. get { return pageCount; }
  116. set
  117. {
  118. SetProperty(ref pageCount, value);
  119. }
  120. }
  121. private int currentPage;
  122. public int CurrentPage
  123. {
  124. get { return currentPage; }
  125. set
  126. {
  127. SetProperty(ref currentPage, value);
  128. }
  129. }
  130. private double currenZoom;
  131. public double CurrentZoom
  132. {
  133. get { return currenZoom; }
  134. set { SetProperty(ref currenZoom, value); }
  135. }
  136. private int selectedIndex;
  137. public int SelectedIndex
  138. {
  139. get { return selectedIndex; }
  140. set
  141. {
  142. SetProperty(ref selectedIndex, value);
  143. }
  144. }
  145. private bool rightPanelEnable = true;
  146. public bool RightPanelEnable
  147. {
  148. get { return rightPanelEnable; }
  149. set
  150. {
  151. SetProperty(ref rightPanelEnable, value);
  152. }
  153. }
  154. private bool isPreEnable = false;
  155. /// <summary>
  156. /// 前进是否启用
  157. /// </summary>
  158. public bool IsPreEnable
  159. {
  160. get { return isPreEnable; }
  161. set
  162. {
  163. SetProperty(ref isPreEnable, value);
  164. }
  165. }
  166. private bool isNextEnable = false;
  167. public bool IsNextEnable
  168. {
  169. get { return isNextEnable; }
  170. set
  171. {
  172. SetProperty(ref isNextEnable, value);
  173. }
  174. }
  175. private Stack<int> UndoStack = new Stack<int>();
  176. private Stack<int> RedoStack = new Stack<int>();
  177. public CPDFViewer PDFViewer { get; set; }
  178. public ViewContentViewModel ViewContentViewModel { get; set; }
  179. /// <summary>
  180. /// 来自undo redo方法的跳页 不计入栈
  181. /// </summary>
  182. private bool isFromMethod = false;
  183. /// <summary>
  184. /// 记录上一个页面索引值
  185. /// </summary>
  186. private int oldPageIndex = 0;
  187. private IRegionManager region;
  188. private IDialogService dialogs;
  189. public BottomToolContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  190. {
  191. region = regionManager;
  192. dialogs = dialogService;
  193. PrePageCommand = new DelegateCommand(GoPrePage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  194. NextPageCommand = new DelegateCommand(GoNextPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  195. FirstPageCommand = new DelegateCommand(GoFirstPage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  196. LastPageCommand = new DelegateCommand(GoLastPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  197. ZoomInCommand = new DelegateCommand(ZoomIn, CanZoomIn).ObservesProperty(() => CurrentZoom);
  198. ZoomOutCommand = new DelegateCommand(ZoomOut, CanZoomOut).ObservesProperty(() => CurrentZoom);
  199. SetViewModeCommand = new DelegateCommand(SetModeView);
  200. PageTextKeyDownCommand = new DelegateCommand<object>(PageNumText_KeyDown);
  201. PageTextPreviewKeyDownCommand = new DelegateCommand<object>(PageNumText_PreviewKeyDown);
  202. SelectionChangedCommand = new DelegateCommand<object>(SelectionChangedEvent);
  203. ShowViewModularCommand = new DelegateCommand<object>(ShowViewModularEvent);
  204. PreViewCommand = new DelegateCommand(undo);
  205. NextViewCommnad = new DelegateCommand(redo);
  206. }
  207. private void undo()
  208. {
  209. RedoStack.Push(PDFViewer.CurrentIndex);
  210. IsNextEnable = true;
  211. int index = UndoStack.Pop();
  212. PDFViewer.GoToPage(index);
  213. if(UndoStack.Count<=0)
  214. {
  215. IsPreEnable = false;
  216. }
  217. isFromMethod = true;
  218. }
  219. private void redo()
  220. {
  221. App.Current.Dispatcher.Invoke(() =>
  222. {
  223. UndoStack.Push(PDFViewer.CurrentIndex);
  224. IsPreEnable = true;
  225. int index = RedoStack.Pop();
  226. PDFViewer.GoToPage(index);
  227. if (RedoStack.Count <= 0)
  228. {
  229. IsNextEnable = false;
  230. }
  231. });
  232. isFromMethod = true;
  233. }
  234. private void ShowViewModularEvent(object obj)
  235. {
  236. if (ViewContentViewModel.IsPropertyOpen == false)
  237. {
  238. ViewContentViewModel.IsPropertyOpen = true;
  239. NavigationParameters param = new NavigationParameters();
  240. param.Add(ParameterNames.PDFViewer, PDFViewer);
  241. param.Add(ParameterNames.BottomToolContentViewModel, this);
  242. region.RequestNavigate(RegionNames.PropertyRegionName, "ViewModularContent", param);
  243. if (region.Regions.ContainsRegionWithName(ViewContentViewModel.PropertyRegionName))
  244. {
  245. var views = region.Regions[ViewContentViewModel.PropertyRegionName].Views;
  246. foreach (var item in views)
  247. {
  248. if (item is LinkAnnotProperty linkAnnotProperty)
  249. {
  250. var contentRegion = region.Regions[ViewContentViewModel.PropertyRegionName];
  251. contentRegion.Remove(item);
  252. break;
  253. }
  254. }
  255. }
  256. }
  257. else
  258. {
  259. ViewContentViewModel.IsPropertyOpen = false;
  260. }
  261. IsShowViewModular = ViewContentViewModel.IsPropertyOpen;
  262. }
  263. private void SelectionChangedEvent(object e)
  264. {
  265. var args = e as SelectionChangedEventArgs;
  266. if (args != null)
  267. {
  268. if (SelectedIndex <= 2)
  269. {
  270. switch (SelectedIndex)
  271. {
  272. case 0:
  273. PDFViewer.ChangeFitMode(FitMode.FitSize);
  274. break;
  275. case 1:
  276. PDFViewer.ChangeFitMode(FitMode.FitWidth);
  277. break;
  278. case 2:
  279. PDFViewer.ChangeFitMode(FitMode.FitHeight);
  280. break;
  281. default:
  282. break;
  283. }
  284. }
  285. else
  286. {
  287. if (args.AddedItems[0] is ComboBoxItem)
  288. {
  289. if ((args.AddedItems[0] as ComboBoxItem).Tag != null)
  290. {
  291. PDFViewer.Zoom(Convert.ToDouble((args.AddedItems[0] as ComboBoxItem).Tag.ToString()) / 100.0);
  292. }
  293. }
  294. }
  295. }
  296. }
  297. private void PageNumText_PreviewKeyDown(object e)
  298. {
  299. var args = e as KeyEventArgs;
  300. if (args != null)
  301. {
  302. //显示文本框输入内容
  303. List<Key> NumberKeys = new List<Key>() { Key.D0,Key.D1,Key.D2,Key.D3,Key.D4,Key.D5,Key.D6,Key.D7,Key.D8,Key.D9,Key.NumPad0,Key.NumPad1,Key.NumPad2,
  304. Key.NumPad3,Key.NumPad4,Key.NumPad5,Key.NumPad6,Key.NumPad7,Key.NumPad8,Key.NumPad9,Key.Delete,Key.Back,Key.Enter,Key.Right,Key.Left};
  305. if (!NumberKeys.Contains(args.Key))
  306. {
  307. args.Handled = true;
  308. }
  309. }
  310. }
  311. private void PageNumText_KeyDown(object e)
  312. {
  313. if (!(e is KeyEventArgs))
  314. return;
  315. //回车提交输入
  316. if ((e as KeyEventArgs).Key == System.Windows.Input.Key.Enter)
  317. {
  318. int pagenum = 0;
  319. string text = ((e as KeyEventArgs).OriginalSource as TextBox).Text.ToString();
  320. char[] chs = { ' ', '/', '\\' };//字符截取 拒止非法输入
  321. int i = text.LastIndexOfAny(chs);
  322. if (i > 0)
  323. text = text.Substring(0, i - 1);
  324. if (!int.TryParse(text, out pagenum))
  325. {
  326. CurrentPage = PDFViewer.Document.PageCount;
  327. ((e as KeyEventArgs).OriginalSource as TextBox).Text = PDFViewer.Document.PageCount.ToString();
  328. return;
  329. }
  330. if (pagenum < 1)
  331. pagenum = 1;
  332. else if (pagenum > PDFViewer.Document.PageCount)
  333. pagenum = PDFViewer.Document.PageCount;
  334. PDFViewer.GoToPage(pagenum - 1);
  335. CurrentPage = pagenum;
  336. ((e as KeyEventArgs).OriginalSource as TextBox).Text = pagenum.ToString();
  337. }
  338. }
  339. private void GoPrePage()
  340. {
  341. if (PDFViewer != null)
  342. {
  343. PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
  344. }
  345. }
  346. private bool CanPrePageExcute()
  347. {
  348. if (PDFViewer != null)
  349. {
  350. if (PDFViewer.CurrentIndex <= 0)
  351. return false;
  352. else
  353. return true;
  354. }
  355. return false;
  356. }
  357. private void GoNextPage()
  358. {
  359. if (PDFViewer != null)
  360. {
  361. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 1);
  362. //双页模式 一次性跳两页
  363. if (((int)PDFViewer.ModeView > 2))
  364. {
  365. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 2);
  366. }
  367. }
  368. }
  369. private bool CanNextPageExcute()
  370. {
  371. if (PDFViewer != null)
  372. {
  373. if (PDFViewer.CurrentIndex >= PDFViewer.Document.PageCount - 1)
  374. return false;
  375. else
  376. return true;
  377. }
  378. return false;
  379. }
  380. private void GoFirstPage()
  381. {
  382. if (PDFViewer != null)
  383. {
  384. PDFViewer.GoToPage(0);
  385. }
  386. }
  387. private void GoLastPage()
  388. {
  389. if (PDFViewer != null)
  390. {
  391. PDFViewer.GoToPage(PDFViewer.Document.PageCount - 1);
  392. }
  393. }
  394. private void ZoomIn()
  395. {
  396. double zoom = SetPageZoomFactor(true);
  397. PDFViewer.Zoom(zoom / 100);
  398. }
  399. private void ZoomOut()
  400. {
  401. double zoom = SetPageZoomFactor(false);
  402. PDFViewer.Zoom(zoom / 100);
  403. }
  404. private double SetPageZoomFactor(bool flag)
  405. {
  406. double zoom = PDFViewer.ZoomFactor * 100;
  407. if (flag)
  408. {
  409. if (zoom >= 25 && zoom <= 300)
  410. {
  411. zoom = zoom + 25;
  412. }
  413. else if (zoom >= 301 && zoom <= 1000)
  414. {
  415. zoom = zoom + 40;
  416. }
  417. else if (zoom >= 1001 && zoom <= 10000)
  418. {
  419. zoom = zoom + 100;
  420. }
  421. }
  422. else
  423. {
  424. if (zoom >= 25 && zoom <= 300)
  425. {
  426. zoom = zoom - 25;
  427. }
  428. else if (zoom >= 301 && zoom <= 1000)
  429. {
  430. zoom = zoom - 40;
  431. }
  432. else if (zoom >= 1001 && zoom <= 10000)
  433. {
  434. zoom = zoom - 100;
  435. }
  436. }
  437. if (zoom < 25)
  438. {
  439. zoom = 25;
  440. }
  441. if (zoom > 10000)
  442. {
  443. zoom = 10000;
  444. }
  445. return zoom;
  446. }
  447. private bool CanZoomIn()
  448. {
  449. if (PDFViewer != null)
  450. {
  451. if (PDFViewer.ZoomFactor * 100 >= 10000)
  452. return false;
  453. else
  454. return true;
  455. }
  456. return false;
  457. }
  458. private bool CanZoomOut()
  459. {
  460. if (PDFViewer != null)
  461. {
  462. if (PDFViewer.ZoomFactor * 100 <= 25)
  463. return false;
  464. else
  465. return true;
  466. }
  467. return false;
  468. }
  469. public void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  470. {
  471. if (e.Key == "PageNum")
  472. {
  473. RenderData renderData = e.Value as RenderData;
  474. if (renderData != null)
  475. {
  476. CurrentPage = renderData.PageIndex;
  477. PageCount = PDFViewer.Document.PageCount;
  478. if(!renderData.IsMouseWheel&&!isFromMethod)
  479. {
  480. UndoStack.Push(oldPageIndex);
  481. RedoStack.Clear();
  482. IsNextEnable = false;
  483. IsPreEnable = true;
  484. }
  485. isFromMethod = false;
  486. }
  487. oldPageIndex = renderData.PageIndex - 1;
  488. }
  489. if (e.Key == "ViewMode")
  490. {
  491. GetModeView((ViewMode)e.Value);
  492. }
  493. if (e.Key == "Zoom")
  494. {
  495. CurrentZoom = (double)e.Value * 100;
  496. SelectedIndex = -1;
  497. }
  498. }
  499. private void ViewContentViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  500. {
  501. if (e.PropertyName == "OCRContentVisible")
  502. {
  503. if (ViewContentViewModel.OCRContentVisible == Visibility.Visible)
  504. {
  505. RightPanelEnable = false;
  506. }
  507. else
  508. {
  509. RightPanelEnable = true;
  510. }
  511. }
  512. }
  513. private void GetModeView(ViewMode mode)
  514. {
  515. if ((int)mode % 2 == 0)
  516. {
  517. if (!IsContinue)
  518. {
  519. IsContinue = true;
  520. }
  521. }
  522. else
  523. {
  524. if (IsContinue)
  525. {
  526. IsContinue = false;
  527. }
  528. }
  529. if ((int)mode <= 2)
  530. {
  531. if (!isSingleView)
  532. {
  533. IsSingleView = true;
  534. }
  535. }
  536. else if ((int)mode <= 4)
  537. {
  538. if (!isDoubleView)
  539. {
  540. IsDoubleView = true;
  541. }
  542. }
  543. else
  544. {
  545. if (!isBookMode)
  546. {
  547. IsBookMode = true;
  548. }
  549. }
  550. }
  551. public void SetModeView()
  552. {
  553. if (PDFViewer != null)
  554. {
  555. if (IsContinue)
  556. {
  557. if (IsSingleView)
  558. {
  559. PDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
  560. }
  561. else if (IsDoubleView)
  562. {
  563. PDFViewer.ChangeViewMode(ViewMode.DoubleContinuous);
  564. }
  565. else
  566. {
  567. PDFViewer.ChangeViewMode(ViewMode.BookContinuous);
  568. }
  569. }
  570. else
  571. {
  572. if (IsSingleView)
  573. {
  574. PDFViewer.ChangeViewMode(ViewMode.Single);
  575. }
  576. else if (IsDoubleView)
  577. {
  578. PDFViewer.ChangeViewMode(ViewMode.Double);
  579. }
  580. else
  581. {
  582. PDFViewer.ChangeViewMode(ViewMode.Book);
  583. }
  584. }
  585. }
  586. }
  587. #region Navigation
  588. public void OnNavigatedTo(NavigationContext navigationContext)
  589. {
  590. var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  591. if (viewContentViewModel != null)
  592. {
  593. this.ViewContentViewModel = viewContentViewModel;
  594. IsShowViewModular = viewContentViewModel.IsPropertyOpen;
  595. viewContentViewModel.PropertyChanged -= ViewContentViewModel_PropertyChanged;
  596. viewContentViewModel.PropertyChanged += ViewContentViewModel_PropertyChanged;
  597. }
  598. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  599. if (pdfview != null)
  600. {
  601. //获取页面设置等信息
  602. PDFViewer = pdfview;
  603. oldPageIndex = pdfview.CurrentIndex;
  604. PageCount = PDFViewer.Document.PageCount;
  605. GetModeView(PDFViewer.ModeView);
  606. CurrentPage = PDFViewer.CurrentIndex + 1;
  607. CurrentZoom = PDFViewer.ZoomFactor * 100;
  608. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  609. }
  610. }
  611. public bool IsNavigationTarget(NavigationContext navigationContext)
  612. {
  613. return true;
  614. }
  615. public void OnNavigatedFrom(NavigationContext navigationContext)
  616. {
  617. }
  618. #endregion Navigation
  619. }
  620. }