BottomToolContentViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. private bool isShowViewModular = false;
  44. /// <summary>
  45. /// 属性栏是否展开
  46. /// </summary>
  47. public bool IsShowViewModular
  48. {
  49. get { return isShowViewModular; }
  50. set
  51. {
  52. SetProperty(ref isShowViewModular, value);
  53. }
  54. }
  55. private bool isSingleView;
  56. public bool IsSingleView
  57. {
  58. get { return isSingleView; }
  59. set
  60. {
  61. SetProperty(ref isSingleView, value);
  62. if (value)
  63. {
  64. SetModeView();
  65. }
  66. }
  67. }
  68. private bool isContinue;
  69. public bool IsContinue
  70. {
  71. get { return isContinue; }
  72. set
  73. {
  74. SetProperty(ref isContinue, value);
  75. SetModeView();
  76. }
  77. }
  78. private bool isDoubleView;
  79. public bool IsDoubleView
  80. {
  81. get { return isDoubleView; }
  82. set
  83. {
  84. SetProperty(ref isDoubleView, value);
  85. if (value)
  86. {
  87. SetModeView();
  88. }
  89. }
  90. }
  91. private bool isBookMode;
  92. public bool IsBookMode
  93. {
  94. get { return isBookMode; }
  95. set
  96. {
  97. SetProperty(ref isBookMode, value);
  98. if (value)
  99. {
  100. SetModeView();
  101. }
  102. }
  103. }
  104. private int pageCount;
  105. public int PageCount
  106. {
  107. get { return pageCount; }
  108. set
  109. {
  110. SetProperty(ref pageCount, value);
  111. }
  112. }
  113. private int currentPage;
  114. public int CurrentPage
  115. {
  116. get { return currentPage; }
  117. set
  118. {
  119. SetProperty(ref currentPage, value);
  120. }
  121. }
  122. private double currenZoom;
  123. public double CurrentZoom
  124. {
  125. get { return currenZoom; }
  126. set { SetProperty(ref currenZoom, value); }
  127. }
  128. private int selectedIndex;
  129. public int SelectedIndex
  130. {
  131. get { return selectedIndex; }
  132. set
  133. {
  134. SetProperty(ref selectedIndex, value);
  135. }
  136. }
  137. private bool rightPanelEnable = true;
  138. public bool RightPanelEnable
  139. {
  140. get { return rightPanelEnable; }
  141. set
  142. {
  143. SetProperty(ref rightPanelEnable, value);
  144. }
  145. }
  146. public CPDFViewer PDFViewer { get; set; }
  147. public ViewContentViewModel ViewContentViewModel { get; set; }
  148. private IRegionManager region;
  149. private IDialogService dialogs;
  150. public BottomToolContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  151. {
  152. region = regionManager;
  153. dialogs = dialogService;
  154. PrePageCommand = new DelegateCommand(GoPrePage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  155. NextPageCommand = new DelegateCommand(GoNextPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  156. FirstPageCommand = new DelegateCommand(GoFirstPage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  157. LastPageCommand = new DelegateCommand(GoLastPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  158. ZoomInCommand = new DelegateCommand(ZoomIn, CanZoomIn).ObservesProperty(() => CurrentZoom);
  159. ZoomOutCommand = new DelegateCommand(ZoomOut, CanZoomOut).ObservesProperty(() => CurrentZoom);
  160. SetViewModeCommand = new DelegateCommand(SetModeView);
  161. PageTextKeyDownCommand = new DelegateCommand<object>(PageNumText_KeyDown);
  162. PageTextPreviewKeyDownCommand = new DelegateCommand<object>(PageNumText_PreviewKeyDown);
  163. SelectionChangedCommand = new DelegateCommand<object>(SelectionChangedEvent);
  164. ShowViewModularCommand = new DelegateCommand<object>(ShowViewModularEvent);
  165. }
  166. private void ShowViewModularEvent(object obj)
  167. {
  168. if (ViewContentViewModel.IsPropertyOpen == false)
  169. {
  170. ViewContentViewModel.IsPropertyOpen = true;
  171. NavigationParameters param = new NavigationParameters();
  172. param.Add(ParameterNames.PDFViewer, PDFViewer);
  173. param.Add(ParameterNames.BottomToolContentViewModel, this);
  174. region.RequestNavigate(RegionNames.PropertyRegionName, "ViewModularContent", param);
  175. if (region.Regions.ContainsRegionWithName(ViewContentViewModel.PropertyRegionName))
  176. {
  177. var views = region.Regions[ViewContentViewModel.PropertyRegionName].Views;
  178. foreach (var item in views)
  179. {
  180. if (item is LinkAnnotProperty linkAnnotProperty)
  181. {
  182. var contentRegion = region.Regions[ViewContentViewModel.PropertyRegionName];
  183. contentRegion.Remove(item);
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. else
  190. {
  191. ViewContentViewModel.IsPropertyOpen = false;
  192. }
  193. IsShowViewModular = ViewContentViewModel.IsPropertyOpen;
  194. }
  195. private void SelectionChangedEvent(object e)
  196. {
  197. var args = e as SelectionChangedEventArgs;
  198. if (args != null)
  199. {
  200. if (SelectedIndex <= 2)
  201. {
  202. switch (SelectedIndex)
  203. {
  204. case 0:
  205. PDFViewer.ChangeFitMode(FitMode.FitSize);
  206. break;
  207. case 1:
  208. PDFViewer.ChangeFitMode(FitMode.FitWidth);
  209. break;
  210. case 2:
  211. PDFViewer.ChangeFitMode(FitMode.FitHeight);
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. else
  218. {
  219. if (args.AddedItems[0] is ComboBoxItem)
  220. {
  221. if ((args.AddedItems[0] as ComboBoxItem).Tag != null)
  222. {
  223. PDFViewer.Zoom(Convert.ToDouble((args.AddedItems[0] as ComboBoxItem).Tag.ToString()) / 100.0);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. private void PageNumText_PreviewKeyDown(object e)
  230. {
  231. var args = e as KeyEventArgs;
  232. if (args != null)
  233. {
  234. //显示文本框输入内容
  235. 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,
  236. Key.NumPad3,Key.NumPad4,Key.NumPad5,Key.NumPad6,Key.NumPad7,Key.NumPad8,Key.NumPad9,Key.Delete,Key.Back,Key.Enter,Key.Right,Key.Left};
  237. if (!NumberKeys.Contains(args.Key))
  238. {
  239. args.Handled = true;
  240. }
  241. }
  242. }
  243. private void PageNumText_KeyDown(object e)
  244. {
  245. if (!(e is KeyEventArgs))
  246. return;
  247. //回车提交输入
  248. if ((e as KeyEventArgs).Key == System.Windows.Input.Key.Enter)
  249. {
  250. int pagenum = 0;
  251. string text = ((e as KeyEventArgs).OriginalSource as TextBox).Text.ToString();
  252. char[] chs = { ' ', '/', '\\' };//字符截取 拒止非法输入
  253. int i = text.LastIndexOfAny(chs);
  254. if (i > 0)
  255. text = text.Substring(0, i - 1);
  256. if (!int.TryParse(text, out pagenum))
  257. {
  258. CurrentPage = PDFViewer.Document.PageCount;
  259. ((e as KeyEventArgs).OriginalSource as TextBox).Text = PDFViewer.Document.PageCount.ToString();
  260. return;
  261. }
  262. if (pagenum < 1)
  263. pagenum = 1;
  264. else if (pagenum > PDFViewer.Document.PageCount)
  265. pagenum = PDFViewer.Document.PageCount;
  266. PDFViewer.GoToPage(pagenum - 1);
  267. CurrentPage = pagenum;
  268. ((e as KeyEventArgs).OriginalSource as TextBox).Text = pagenum.ToString();
  269. }
  270. }
  271. private void GoPrePage()
  272. {
  273. if (PDFViewer != null)
  274. {
  275. PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
  276. }
  277. }
  278. private bool CanPrePageExcute()
  279. {
  280. if (PDFViewer != null)
  281. {
  282. if (PDFViewer.CurrentIndex <= 0)
  283. return false;
  284. else
  285. return true;
  286. }
  287. return false;
  288. }
  289. private void GoNextPage()
  290. {
  291. if (PDFViewer != null)
  292. {
  293. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 1);
  294. //双页模式 一次性跳两页
  295. if (((int)PDFViewer.ModeView > 2))
  296. {
  297. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 2);
  298. }
  299. }
  300. }
  301. private bool CanNextPageExcute()
  302. {
  303. if (PDFViewer != null)
  304. {
  305. if (PDFViewer.CurrentIndex >= PDFViewer.Document.PageCount - 1)
  306. return false;
  307. else
  308. return true;
  309. }
  310. return false;
  311. }
  312. private void GoFirstPage()
  313. {
  314. if (PDFViewer != null)
  315. {
  316. PDFViewer.GoToPage(0);
  317. }
  318. }
  319. private void GoLastPage()
  320. {
  321. if (PDFViewer != null)
  322. {
  323. PDFViewer.GoToPage(PDFViewer.Document.PageCount - 1);
  324. }
  325. }
  326. private void ZoomIn()
  327. {
  328. double zoom = SetPageZoomFactor(true);
  329. PDFViewer.Zoom(zoom / 100);
  330. }
  331. private void ZoomOut()
  332. {
  333. double zoom = SetPageZoomFactor(false);
  334. PDFViewer.Zoom(zoom / 100);
  335. }
  336. private double SetPageZoomFactor(bool flag)
  337. {
  338. double zoom = PDFViewer.ZoomFactor * 100;
  339. if (flag)
  340. {
  341. if (zoom >= 25 && zoom <= 300)
  342. {
  343. zoom = zoom + 25;
  344. }
  345. else if (zoom >= 301 && zoom <= 1000)
  346. {
  347. zoom = zoom + 40;
  348. }
  349. else if (zoom >= 1001 && zoom <= 10000)
  350. {
  351. zoom = zoom + 100;
  352. }
  353. }
  354. else
  355. {
  356. if (zoom >= 25 && zoom <= 300)
  357. {
  358. zoom = zoom - 25;
  359. }
  360. else if (zoom >= 301 && zoom <= 1000)
  361. {
  362. zoom = zoom - 40;
  363. }
  364. else if (zoom >= 1001 && zoom <= 10000)
  365. {
  366. zoom = zoom - 100;
  367. }
  368. }
  369. if (zoom < 25)
  370. {
  371. zoom = 25;
  372. }
  373. if (zoom > 10000)
  374. {
  375. zoom = 10000;
  376. }
  377. return zoom;
  378. }
  379. private bool CanZoomIn()
  380. {
  381. if (PDFViewer != null)
  382. {
  383. if (PDFViewer.ZoomFactor * 100 >= 10000)
  384. return false;
  385. else
  386. return true;
  387. }
  388. return false;
  389. }
  390. private bool CanZoomOut()
  391. {
  392. if (PDFViewer != null)
  393. {
  394. if (PDFViewer.ZoomFactor * 100 <= 25)
  395. return false;
  396. else
  397. return true;
  398. }
  399. return false;
  400. }
  401. public void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  402. {
  403. if (e.Key == "PageNum")
  404. {
  405. RenderData renderData = e.Value as RenderData;
  406. if (renderData != null)
  407. {
  408. CurrentPage = renderData.PageIndex;
  409. PageCount = PDFViewer.Document.PageCount;
  410. }
  411. }
  412. if (e.Key == "ViewMode")
  413. {
  414. GetModeView((ViewMode)e.Value);
  415. }
  416. if (e.Key == "Zoom")
  417. {
  418. CurrentZoom = (double)e.Value * 100;
  419. SelectedIndex = -1;
  420. }
  421. }
  422. private void GetModeView(ViewMode mode)
  423. {
  424. if ((int)mode % 2 == 0)
  425. {
  426. if (!IsContinue)
  427. {
  428. IsContinue = true;
  429. }
  430. }
  431. else
  432. {
  433. if (IsContinue)
  434. {
  435. IsContinue = false;
  436. }
  437. }
  438. if ((int)mode <= 2)
  439. {
  440. if (!isSingleView)
  441. {
  442. IsSingleView = true;
  443. }
  444. }
  445. else if ((int)mode <= 4)
  446. {
  447. if (!isDoubleView)
  448. {
  449. IsDoubleView = true;
  450. }
  451. }
  452. else
  453. {
  454. if (!isBookMode)
  455. {
  456. IsBookMode = true;
  457. }
  458. }
  459. }
  460. public void SetModeView()
  461. {
  462. if (PDFViewer != null)
  463. {
  464. if (IsContinue)
  465. {
  466. if (IsSingleView)
  467. {
  468. PDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
  469. }
  470. else if (IsDoubleView)
  471. {
  472. PDFViewer.ChangeViewMode(ViewMode.DoubleContinuous);
  473. }
  474. else
  475. {
  476. PDFViewer.ChangeViewMode(ViewMode.BookContinuous);
  477. }
  478. }
  479. else
  480. {
  481. if (IsSingleView)
  482. {
  483. PDFViewer.ChangeViewMode(ViewMode.Single);
  484. }
  485. else if (IsDoubleView)
  486. {
  487. PDFViewer.ChangeViewMode(ViewMode.Double);
  488. }
  489. else
  490. {
  491. PDFViewer.ChangeViewMode(ViewMode.Book);
  492. }
  493. }
  494. }
  495. }
  496. #region Navigation
  497. public void OnNavigatedTo(NavigationContext navigationContext)
  498. {
  499. var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  500. if (viewContentViewModel != null)
  501. {
  502. this.ViewContentViewModel = viewContentViewModel;
  503. IsShowViewModular = viewContentViewModel.IsPropertyOpen;
  504. viewContentViewModel.PropertyChanged -= ViewContentViewModel_PropertyChanged;
  505. viewContentViewModel.PropertyChanged += ViewContentViewModel_PropertyChanged;
  506. }
  507. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  508. if (pdfview != null)
  509. {
  510. //获取页面设置等信息
  511. PDFViewer = pdfview;
  512. PageCount = PDFViewer.Document.PageCount;
  513. GetModeView(PDFViewer.ModeView);
  514. CurrentPage = PDFViewer.CurrentIndex + 1;
  515. CurrentZoom = PDFViewer.ZoomFactor * 100;
  516. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  517. }
  518. }
  519. private void ViewContentViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  520. {
  521. if(e.PropertyName == "OCRContentVisible")
  522. {
  523. if(ViewContentViewModel.OCRContentVisible == Visibility.Visible)
  524. {
  525. RightPanelEnable = false;
  526. }
  527. else
  528. {
  529. RightPanelEnable = true;
  530. }
  531. }
  532. }
  533. public bool IsNavigationTarget(NavigationContext navigationContext)
  534. {
  535. return true;
  536. }
  537. public void OnNavigatedFrom(NavigationContext navigationContext)
  538. {
  539. }
  540. #endregion Navigation
  541. }
  542. }