BottomToolContentViewModel.cs 17 KB

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