BottomToolContentViewModel.cs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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_Master.Views;
  12. using ComPDFKitViewer;
  13. using System.Windows.Input;
  14. using System.Windows.Controls;
  15. using Prism.Services.Dialogs;
  16. using PDF_Master.CustomControl;
  17. using PDF_Master.Model;
  18. using PDF_Master.ViewModels.BOTA;
  19. using PDF_Master.Views.BOTA;
  20. using PDF_Master.Views.PropertyPanel.AnnotPanel;
  21. using PDF_Master.Views.Tools;
  22. using PDFSettings;
  23. using PDF_Master.Helper;
  24. using System.IO;
  25. using ImTools;
  26. using PDF_Master.Views.PropertyPanel.ViewModular;
  27. namespace PDF_Master.ViewModels
  28. {
  29. public class BottomToolContentViewModel : BindableBase, INavigationAware
  30. {
  31. #region 文案
  32. private string T_actual;
  33. public string T_Actual
  34. {
  35. get { return T_actual; }
  36. set
  37. {
  38. SetProperty(ref T_actual, value);
  39. }
  40. }
  41. private string T_fitWidth;
  42. public string T_FitWidth
  43. {
  44. get { return T_fitWidth; }
  45. set
  46. {
  47. SetProperty(ref T_fitWidth, value);
  48. }
  49. }
  50. private string T_fitPage;
  51. public string T_FitPage
  52. {
  53. get { return T_fitPage; }
  54. set
  55. {
  56. SetProperty(ref T_fitPage, value);
  57. }
  58. }
  59. private string T_continuousText;
  60. public string T_ContinuousText
  61. {
  62. get { return T_continuousText; }
  63. set
  64. {
  65. SetProperty(ref T_continuousText, value);
  66. }
  67. }
  68. private string T_continuousTip;
  69. public string T_ContinuousTip
  70. {
  71. get { return T_continuousTip; }
  72. set
  73. {
  74. SetProperty(ref T_continuousTip, value);
  75. }
  76. }
  77. private string T_singlePageText;
  78. public string T_SinglePageText
  79. {
  80. get { return T_singlePageText; }
  81. set
  82. {
  83. SetProperty(ref T_singlePageText, value);
  84. }
  85. }
  86. private string T_singlePageTip;
  87. public string T_SinglePageTip
  88. {
  89. get { return T_singlePageTip; }
  90. set
  91. {
  92. SetProperty(ref T_singlePageTip, value);
  93. }
  94. }
  95. private string T_facingText;
  96. public string T_FacingText
  97. {
  98. get { return T_facingText; }
  99. set
  100. {
  101. SetProperty(ref T_facingText, value);
  102. }
  103. }
  104. private string T_facingTip;
  105. public string T_FacingTip
  106. {
  107. get { return T_facingTip; }
  108. set
  109. {
  110. SetProperty(ref T_facingTip, value);
  111. }
  112. }
  113. private string T_bookModeTip;
  114. public string T_BookModeTip
  115. {
  116. get { return T_bookModeTip; }
  117. set
  118. {
  119. SetProperty(ref T_bookModeTip, value);
  120. }
  121. }
  122. private string T_bookModeText;
  123. public string T_BookModeText
  124. {
  125. get { return T_bookModeText; }
  126. set
  127. {
  128. SetProperty(ref T_bookModeText, value);
  129. }
  130. }
  131. //private string T_showViewModularTip;
  132. //public string T_ShowViewModularTip
  133. //{
  134. // get { return T_showViewModularTip; }
  135. // set
  136. // {
  137. // SetProperty(ref T_showViewModularTip, value);
  138. // }
  139. //}
  140. private string T_showViewModularText;
  141. public string T_ShowViewModularText
  142. {
  143. get { return T_showViewModularText; }
  144. set
  145. {
  146. SetProperty(ref T_showViewModularText, value);
  147. }
  148. }
  149. private void InitString()
  150. {
  151. T_Actual = App.MainPageLoader.GetString("Zoom_Actual");
  152. T_FitWidth = App.MainPageLoader.GetString("Zoom_FitWidth");
  153. T_FitPage = App.MainPageLoader.GetString("Zoom_FitPage");
  154. T_ContinuousText = App.MainPageLoader.GetString("MainPage_Continuous");
  155. T_ContinuousTip = App.MainPageLoader.GetString("MainPage_ViewPageContinousTip");
  156. T_SinglePageText = App.MainPageLoader.GetString("MainPage_SinglePage");
  157. T_SinglePageTip = App.MainPageLoader.GetString("MainPage_SinglePageTip");
  158. T_FacingText = App.MainPageLoader.GetString("MainPage_Facing");
  159. T_FacingTip = App.MainPageLoader.GetString("MainPage_FacingTip");
  160. T_BookModeText = App.MainPageLoader.GetString("MainPage_BookMode");
  161. T_BookModeTip = App.MainPageLoader.GetString("MainPage_BookModeTip");
  162. T_ShowViewModularText = App.MainPageLoader.GetString("MainPage_ShowViewModular");
  163. //T_ShowViewModularTip = App.MainPageLoader.GetString("MainPage_ShowViewModularTip");
  164. }
  165. #endregion 文案
  166. public DelegateCommand Load { get; set; }
  167. public DelegateCommand FirstPageCommand { get; set; }
  168. public DelegateCommand LastPageCommand { get; set; }
  169. public DelegateCommand PrePageCommand { get; set; }
  170. public DelegateCommand NextPageCommand { get; set; }
  171. public DelegateCommand MyPropertyCommand { get; set; }
  172. public DelegateCommand FullScreenCommand { get; set; }
  173. public DelegateCommand ReadModeCommand { get; set; }
  174. public DelegateCommand OpenViewCommand { get; set; }
  175. public DelegateCommand ZoomInCommand { get; set; }
  176. public DelegateCommand ZoomOutCommand { get; set; }
  177. public DelegateCommand SetViewModeCommand { get; set; }
  178. public DelegateCommand OpenFullCommand { get; set; }
  179. public DelegateCommand<object> PageTextKeyDownCommand { get; set; }
  180. public DelegateCommand<object> PageTextPreviewKeyDownCommand { get; set; }
  181. public DelegateCommand<object> SelectionChangedCommand { get; set; }
  182. public DelegateCommand<object> ShowViewModularCommand { get; set; }
  183. /// <summary>
  184. /// 上一个视图
  185. /// </summary>
  186. public DelegateCommand PreViewCommand { get; set; }
  187. /// <summary>
  188. /// 下一个视图
  189. /// </summary>
  190. public DelegateCommand NextViewCommnad { get; set; }
  191. /// <summary>
  192. /// 拦截键盘输入的事件
  193. /// </summary>
  194. public DelegateCommand<object> PreviewKeyDown { get; set; }
  195. /// <summary>
  196. /// 进入单页
  197. /// </summary>
  198. public DelegateCommand SinglePageCommand { get; set; }
  199. /// <summary>
  200. /// 进入双页
  201. /// </summary>
  202. public DelegateCommand DoublePageCommand { get; set; }
  203. /// <summary>
  204. /// 回到实际大小
  205. /// </summary>
  206. public DelegateCommand RealSizeCommand { get; set; }
  207. private bool isShowViewModular = false;
  208. /// <summary>
  209. /// 是否已经显示 用于跳过控件显示前触发的事件
  210. /// </summary>
  211. public bool IsLoaded = false;
  212. /// <summary>
  213. /// 属性栏是否展开
  214. /// </summary>
  215. public bool IsShowViewModular
  216. {
  217. get { return isShowViewModular; }
  218. set
  219. {
  220. SetProperty(ref isShowViewModular, value);
  221. }
  222. }
  223. private bool isSingleView;
  224. public bool IsSingleView
  225. {
  226. get { return isSingleView; }
  227. set
  228. {
  229. SetProperty(ref isSingleView, value);
  230. if (value)
  231. {
  232. SetModeView();
  233. }
  234. }
  235. }
  236. private bool isContinue;
  237. public bool IsContinue
  238. {
  239. get { return isContinue; }
  240. set
  241. {
  242. SetProperty(ref isContinue, value);
  243. SetModeView();
  244. }
  245. }
  246. private bool isDoubleView;
  247. public bool IsDoubleView
  248. {
  249. get { return isDoubleView; }
  250. set
  251. {
  252. SetProperty(ref isDoubleView, value);
  253. if (value)
  254. {
  255. SetModeView();
  256. }
  257. }
  258. }
  259. private bool isBookMode;
  260. public bool IsBookMode
  261. {
  262. get { return isBookMode; }
  263. set
  264. {
  265. SetProperty(ref isBookMode, value);
  266. if (value)
  267. {
  268. SetModeView();
  269. }
  270. }
  271. }
  272. private int pageCount;
  273. public int PageCount
  274. {
  275. get { return pageCount; }
  276. set
  277. {
  278. SetProperty(ref pageCount, value);
  279. }
  280. }
  281. private int currentPage;
  282. public int CurrentPage
  283. {
  284. get { return currentPage; }
  285. set
  286. {
  287. SetProperty(ref currentPage, value);
  288. }
  289. }
  290. private double currenZoom;
  291. public double CurrentZoom
  292. {
  293. get { return currenZoom; }
  294. set { SetProperty(ref currenZoom, value); }
  295. }
  296. private int selectedIndex;
  297. public int SelectedIndex
  298. {
  299. get { return selectedIndex; }
  300. set
  301. {
  302. SetProperty(ref selectedIndex, value);
  303. }
  304. }
  305. private bool rightPanelEnable = true;
  306. public bool RightPanelEnable
  307. {
  308. get { return rightPanelEnable; }
  309. set
  310. {
  311. SetProperty(ref rightPanelEnable, value);
  312. }
  313. }
  314. private bool isPreEnable = false;
  315. /// <summary>
  316. /// 前进是否启用
  317. /// </summary>
  318. public bool IsPreEnable
  319. {
  320. get { return isPreEnable; }
  321. set
  322. {
  323. SetProperty(ref isPreEnable, value);
  324. }
  325. }
  326. private bool isNextEnable = false;
  327. public bool IsNextEnable
  328. {
  329. get { return isNextEnable; }
  330. set
  331. {
  332. SetProperty(ref isNextEnable, value);
  333. }
  334. }
  335. private Stack<int> UndoStack = new Stack<int>();
  336. private Stack<int> RedoStack = new Stack<int>();
  337. public CPDFViewer PDFViewer { get; set; }
  338. public ViewContentViewModel ViewContentViewModel { get; set; }
  339. public OpenFileInfo OpenFileInfo = null;
  340. /// <summary>
  341. /// 来自undo redo方法的跳页 不计入栈
  342. /// </summary>
  343. private bool isFromMethod = false;
  344. /// <summary>
  345. /// 记录上一个页面索引值
  346. /// </summary>
  347. private int oldPageIndex = 0;
  348. private IRegionManager region;
  349. private IDialogService dialogs;
  350. public BottomToolContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  351. {
  352. region = regionManager;
  353. dialogs = dialogService;
  354. PrePageCommand = new DelegateCommand(GoPrePage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  355. NextPageCommand = new DelegateCommand(GoNextPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  356. FirstPageCommand = new DelegateCommand(GoFirstPage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  357. LastPageCommand = new DelegateCommand(GoLastPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  358. ZoomInCommand = new DelegateCommand(ZoomIn, CanZoomIn).ObservesProperty(() => CurrentZoom);
  359. ZoomOutCommand = new DelegateCommand(ZoomOut, CanZoomOut).ObservesProperty(() => CurrentZoom);
  360. SetViewModeCommand = new DelegateCommand(SetModeView);
  361. PageTextKeyDownCommand = new DelegateCommand<object>(PageNumText_KeyDown);
  362. PageTextPreviewKeyDownCommand = new DelegateCommand<object>(PageNumText_PreviewKeyDown);
  363. SelectionChangedCommand = new DelegateCommand<object>(SelectionChangedEvent);
  364. ShowViewModularCommand = new DelegateCommand<object>(ShowViewModularEvent);
  365. PreviewKeyDown = new DelegateCommand<object>(previewkeydown);
  366. PreViewCommand = new DelegateCommand(undo);
  367. SinglePageCommand = new DelegateCommand(() => { IsSingleView = true; });
  368. DoublePageCommand = new DelegateCommand(() => { IsDoubleView = true; });
  369. RealSizeCommand = new DelegateCommand(ZoomToRealSize);
  370. NextViewCommnad = new DelegateCommand(redo);
  371. InitString();
  372. }
  373. /// <summary>
  374. /// 缩放至实际大小
  375. /// </summary>
  376. private void ZoomToRealSize()
  377. {
  378. PDFViewer?.ChangeFitMode(FitMode.FitSize);
  379. if (OpenFileInfo != null)
  380. {
  381. OpenFileInfo.LastFitMode = FitMode.FitSize;
  382. }
  383. }
  384. private void previewkeydown(object e)
  385. {
  386. var args = e as KeyEventArgs;
  387. if (args == null)
  388. {
  389. return;
  390. }
  391. //输入值的预处理通过input事件里写正则来判断
  392. if (args.Key == Key.Enter)
  393. {
  394. var text = (args.OriginalSource as TextBox);
  395. int zoom = int.Parse(text.Text.Replace("%", ""));
  396. PDFViewer?.Zoom(zoom / 100.0);
  397. }
  398. }
  399. private void undo()
  400. {
  401. if (!IsPreEnable)
  402. {
  403. return;
  404. }
  405. RedoStack.Push(PDFViewer.CurrentIndex);
  406. IsNextEnable = true;
  407. App.Current.Dispatcher.Invoke(() =>
  408. {
  409. int index = UndoStack.Pop();
  410. PDFViewer.GoToPage(index);
  411. if (UndoStack.Count <= 0)
  412. {
  413. IsPreEnable = false;
  414. }
  415. });
  416. isFromMethod = true;
  417. }
  418. private void redo()
  419. {
  420. if (!IsNextEnable)
  421. {
  422. return;
  423. }
  424. App.Current.Dispatcher.Invoke(() =>
  425. {
  426. UndoStack.Push(PDFViewer.CurrentIndex);
  427. IsPreEnable = true;
  428. int index = RedoStack.Pop();
  429. PDFViewer.GoToPage(index);
  430. if (RedoStack.Count <= 0)
  431. {
  432. IsNextEnable = false;
  433. }
  434. });
  435. isFromMethod = true;
  436. }
  437. private void ShowViewModularEvent(object obj)
  438. {
  439. if (region.Regions.ContainsRegionWithName(ViewContentViewModel.PropertyRegionName))
  440. {
  441. var check = (bool)obj;
  442. if (check)
  443. {
  444. ViewContentViewModel.IsPropertyOpen = true;
  445. NavigationParameters param = new NavigationParameters();
  446. param.Add(ParameterNames.PDFViewer, PDFViewer);
  447. param.Add(ParameterNames.BottomToolContentViewModel, this);
  448. region.RequestNavigate(RegionNames.PropertyRegionName, "ViewModularContent", param);
  449. }
  450. else
  451. {
  452. ViewContentViewModel.IsPropertyOpen = false;
  453. region.RequestNavigate(RegionNames.PropertyRegionName, "PropertyPanelContent");
  454. }
  455. }
  456. }
  457. private void SelectionChangedEvent(object e)
  458. {
  459. if (e == null)
  460. {
  461. return;
  462. }
  463. var args = e as SelectionChangedEventArgs;
  464. if (args != null)
  465. {
  466. if (SelectedIndex <= 2)
  467. {
  468. switch (SelectedIndex)
  469. {
  470. case 0:
  471. PDFViewer?.ChangeFitMode(FitMode.FitSize);
  472. if (OpenFileInfo != null)
  473. {
  474. OpenFileInfo.LastFitMode = FitMode.FitSize;
  475. }
  476. break;
  477. case 1:
  478. PDFViewer?.ChangeFitMode(FitMode.FitWidth);
  479. if (OpenFileInfo != null)
  480. {
  481. OpenFileInfo.LastFitMode = FitMode.FitWidth;
  482. }
  483. break;
  484. case 2:
  485. PDFViewer?.ChangeFitMode(FitMode.FitHeight);
  486. if (OpenFileInfo != null)
  487. {
  488. OpenFileInfo.LastFitMode = FitMode.FitHeight;
  489. }
  490. break;
  491. default:
  492. break;
  493. }
  494. }
  495. else
  496. {
  497. if (args.AddedItems[0] is ComboBoxItem)
  498. {
  499. if ((args.AddedItems[0] as ComboBoxItem).Tag != null)
  500. {
  501. PDFViewer?.Zoom(Convert.ToDouble((args.AddedItems[0] as ComboBoxItem).Tag.ToString()) / 100.0);
  502. }
  503. }
  504. }
  505. }
  506. }
  507. private void PageNumText_PreviewKeyDown(object e)
  508. {
  509. var args = e as KeyEventArgs;
  510. if (args != null)
  511. {
  512. //显示文本框输入内容
  513. 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,
  514. Key.NumPad3,Key.NumPad4,Key.NumPad5,Key.NumPad6,Key.NumPad7,Key.NumPad8,Key.NumPad9,Key.Delete,Key.Back,Key.Enter,Key.Right,Key.Left};
  515. if (!NumberKeys.Contains(args.Key))
  516. {
  517. args.Handled = true;
  518. }
  519. }
  520. }
  521. private void PageNumText_KeyDown(object e)
  522. {
  523. if (!(e is KeyEventArgs))
  524. return;
  525. //回车提交输入
  526. if ((e as KeyEventArgs).Key == System.Windows.Input.Key.Enter)
  527. {
  528. int pagenum = 0;
  529. string text = ((e as KeyEventArgs).OriginalSource as TextBox).Text.ToString();
  530. char[] chs = { ' ', '/', '\\' };//字符截取 拒止非法输入
  531. int i = text.LastIndexOfAny(chs);
  532. if (i > 0)
  533. text = text.Substring(0, i - 1);
  534. if (!int.TryParse(text, out pagenum))
  535. {
  536. CurrentPage = PDFViewer.Document.PageCount;
  537. ((e as KeyEventArgs).OriginalSource as TextBox).Text = PDFViewer.Document.PageCount.ToString();
  538. return;
  539. }
  540. if (pagenum < 1)
  541. pagenum = 1;
  542. else if (pagenum > PDFViewer.Document.PageCount)
  543. pagenum = PDFViewer.Document.PageCount;
  544. PDFViewer.GoToPage(pagenum - 1);
  545. CurrentPage = pagenum;
  546. ((e as KeyEventArgs).OriginalSource as TextBox).Text = pagenum.ToString();
  547. }
  548. }
  549. private void GoPrePage()
  550. {
  551. if (PDFViewer != null)
  552. {
  553. if (PDFViewer.RenderPageStart == PDFViewer.CurrentIndex)
  554. {
  555. PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
  556. }
  557. else
  558. {
  559. if ((PDFViewer.RenderPageStart - 1) > 0)
  560. {
  561. PDFViewer.GoToPage(PDFViewer.RenderPageStart - 1);
  562. }
  563. else
  564. {
  565. PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
  566. }
  567. }
  568. }
  569. }
  570. private bool CanPrePageExcute()
  571. {
  572. if (PDFViewer != null)
  573. {
  574. if (PDFViewer.CurrentIndex <= 0)
  575. return false;
  576. else
  577. return true;
  578. }
  579. return false;
  580. }
  581. private void GoNextPage()
  582. {
  583. if (PDFViewer != null)
  584. {
  585. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 1);
  586. //双页模式 一次性跳两页
  587. if (((int)PDFViewer.ModeView > 2))
  588. {
  589. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 2);
  590. }
  591. }
  592. }
  593. private bool CanNextPageExcute()
  594. {
  595. if (PDFViewer != null)
  596. {
  597. if (CurrentPage >= PDFViewer.Document.PageCount)
  598. return false;
  599. else
  600. return true;
  601. }
  602. return false;
  603. }
  604. private void GoFirstPage()
  605. {
  606. if (PDFViewer != null)
  607. {
  608. PDFViewer.GoToPage(0);
  609. }
  610. }
  611. private void GoLastPage()
  612. {
  613. if (PDFViewer != null)
  614. {
  615. PDFViewer.GoToPage(PDFViewer.Document.PageCount - 1);
  616. }
  617. }
  618. private void ZoomIn()
  619. {
  620. double zoom = SetPageZoomFactor(true);
  621. PDFViewer.Zoom(zoom / 100);
  622. }
  623. private void ZoomOut()
  624. {
  625. double zoom = SetPageZoomFactor(false);
  626. PDFViewer.Zoom(zoom / 100);
  627. }
  628. private double SetPageZoomFactor(bool flag)
  629. {
  630. double zoom = PDFViewer.ZoomFactor * 100;
  631. if (flag)
  632. {
  633. if (zoom >= 25 && zoom <= 300)
  634. {
  635. zoom = zoom + 25;
  636. }
  637. else if (zoom >= 301 && zoom <= 1000)
  638. {
  639. zoom = zoom + 40;
  640. }
  641. else if (zoom >= 1001 && zoom <= 10000)
  642. {
  643. zoom = zoom + 100;
  644. }
  645. }
  646. else
  647. {
  648. if (zoom >= 25 && zoom <= 300)
  649. {
  650. zoom = zoom - 25;
  651. }
  652. else if (zoom >= 301 && zoom <= 1000)
  653. {
  654. zoom = zoom - 40;
  655. }
  656. else if (zoom >= 1001 && zoom <= 10000)
  657. {
  658. zoom = zoom - 100;
  659. }
  660. }
  661. if (zoom < 25)
  662. {
  663. zoom = 25;
  664. }
  665. if (zoom > 10000)
  666. {
  667. zoom = 10000;
  668. }
  669. return zoom;
  670. }
  671. private bool CanZoomIn()
  672. {
  673. if (PDFViewer != null)
  674. {
  675. if (PDFViewer.ZoomFactor * 100 >= 10000)
  676. return false;
  677. else
  678. return true;
  679. }
  680. return false;
  681. }
  682. private bool CanZoomOut()
  683. {
  684. if (PDFViewer != null)
  685. {
  686. if (PDFViewer.ZoomFactor * 100 <= 25)
  687. return false;
  688. else
  689. return true;
  690. }
  691. return false;
  692. }
  693. public void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  694. {
  695. if (e.Key == "PageNum")
  696. {
  697. RenderData renderData = e.Value as RenderData;
  698. if (renderData != null)
  699. {
  700. CurrentPage = renderData.PageIndex;
  701. PageCount = PDFViewer.Document.PageCount;
  702. if (!renderData.IsMouseWheel && !isFromMethod)
  703. {
  704. UndoStack.Push(oldPageIndex);
  705. RedoStack.Clear();
  706. IsNextEnable = false;
  707. IsPreEnable = true;
  708. }
  709. isFromMethod = false;
  710. }
  711. oldPageIndex = renderData.PageIndex - 1;
  712. }
  713. if (e.Key == "ViewMode")
  714. {
  715. GetModeView((ViewMode)e.Value);
  716. }
  717. if (e.Key == "Zoom")
  718. {
  719. CurrentZoom = (double)e.Value * 100;
  720. switch (PDFViewer.ModeFit)
  721. {
  722. case FitMode.FitWidth:
  723. SelectedIndex = 1;
  724. break;
  725. case FitMode.FitHeight:
  726. SelectedIndex = 2;
  727. break;
  728. case FitMode.FitSize:
  729. SelectedIndex = 0;
  730. break;
  731. case FitMode.FitFree:
  732. SelectedIndex = -1;
  733. break;
  734. default:
  735. break;
  736. }
  737. }
  738. if (OpenFileInfo != null)
  739. {
  740. if (OpenFileInfo.LastFitMode != PDFViewer.ModeFit)
  741. {
  742. OpenFileInfo.LastFitMode = PDFViewer.ModeFit;
  743. }
  744. if (PDFViewer.ModeFit == FitMode.FitFree)
  745. {
  746. if (OpenFileInfo.LastZoom != PDFViewer.ZoomFactor)
  747. {
  748. OpenFileInfo.LastZoom = PDFViewer.ZoomFactor;
  749. }
  750. }
  751. if (OpenFileInfo.LastPageIndex != PDFViewer.CurrentIndex)
  752. {
  753. OpenFileInfo.LastPageIndex = PDFViewer.CurrentIndex;
  754. }
  755. SettingHelper.SetFileInfo(OpenFileInfo);
  756. }
  757. }
  758. private void ViewContentViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  759. {
  760. if (e.PropertyName == "OCRContentVisible")
  761. {
  762. if (ViewContentViewModel.OCRContentVisible == Visibility.Visible)
  763. {
  764. RightPanelEnable = false;
  765. }
  766. else
  767. {
  768. RightPanelEnable = true;
  769. }
  770. }
  771. if (e.PropertyName == "IsPropertyOpen")
  772. {
  773. //右上角按钮收起属性面板后,底部工具栏按钮状态同步变化
  774. if (ViewContentViewModel.IsPropertyOpen == false)
  775. {
  776. //监听的属性面板的变化
  777. IsShowViewModular = ViewContentViewModel.IsPropertyOpen;
  778. }
  779. }
  780. }
  781. /// <summary>
  782. /// 设置缩放控件的选中项
  783. /// </summary>
  784. private void SetZoomIndex()
  785. {
  786. switch (PDFViewer.ModeFit)
  787. {
  788. case FitMode.FitWidth:
  789. SelectedIndex = 1;
  790. break;
  791. case FitMode.FitHeight:
  792. SelectedIndex = 2;
  793. break;
  794. case FitMode.FitSize:
  795. SelectedIndex = 0;
  796. break;
  797. case FitMode.FitFree:
  798. SelectedIndex = -1;
  799. break;
  800. default:
  801. break;
  802. }
  803. }
  804. private void GetModeView(ViewMode mode)
  805. {
  806. if ((int)mode % 2 == 0)
  807. {
  808. if (!IsContinue)
  809. {
  810. IsContinue = true;
  811. }
  812. }
  813. else
  814. {
  815. if (IsContinue)
  816. {
  817. IsContinue = false;
  818. }
  819. }
  820. if ((int)mode <= 2)
  821. {
  822. if (!isSingleView)
  823. {
  824. IsSingleView = true;
  825. }
  826. }
  827. else if ((int)mode <= 4)
  828. {
  829. if (!isDoubleView)
  830. {
  831. IsDoubleView = true;
  832. }
  833. }
  834. else
  835. {
  836. if (!isBookMode)
  837. {
  838. IsBookMode = true;
  839. }
  840. }
  841. }
  842. public void SetModeView()
  843. {
  844. if (PDFViewer != null)
  845. {
  846. if (IsContinue)
  847. {
  848. if (IsSingleView)
  849. {
  850. PDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
  851. }
  852. else if (IsDoubleView)
  853. {
  854. PDFViewer.ChangeViewMode(ViewMode.DoubleContinuous);
  855. }
  856. else
  857. {
  858. PDFViewer.ChangeViewMode(ViewMode.BookContinuous);
  859. }
  860. }
  861. else
  862. {
  863. if (IsSingleView)
  864. {
  865. PDFViewer.ChangeViewMode(ViewMode.Single);
  866. }
  867. else if (IsDoubleView)
  868. {
  869. PDFViewer.ChangeViewMode(ViewMode.Double);
  870. }
  871. else
  872. {
  873. PDFViewer.ChangeViewMode(ViewMode.Book);
  874. }
  875. }
  876. }
  877. SaveMode(PDFViewer.ModeView);
  878. }
  879. private void SaveMode(object modeView)
  880. {
  881. OpenFileInfo fileInfo = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  882. if (fileInfo != null)
  883. {
  884. if (modeView is SplitMode)
  885. {
  886. fileInfo.LastSplitMode = PDFViewer.Mode;
  887. }
  888. else if (modeView is ViewMode)
  889. {
  890. fileInfo.LastViewMode = PDFViewer.ModeView;
  891. }
  892. else if (modeView is bool)
  893. {
  894. fileInfo.LastPageSpace = (bool)modeView;
  895. }
  896. SettingHelper.SetFileInfo(fileInfo);
  897. }
  898. }
  899. #region Navigation
  900. public void OnNavigatedTo(NavigationContext navigationContext)
  901. {
  902. var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  903. if (viewContentViewModel != null)
  904. {
  905. this.ViewContentViewModel = viewContentViewModel;
  906. ViewContentViewModel.bottom = this;
  907. IsShowViewModular = viewContentViewModel.IsPropertyOpen;
  908. viewContentViewModel.PropertyChanged -= ViewContentViewModel_PropertyChanged;
  909. viewContentViewModel.PropertyChanged += ViewContentViewModel_PropertyChanged;
  910. }
  911. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  912. if (pdfview != null)
  913. {
  914. if (ViewContentViewModel.OpenFileInfo != null)
  915. {
  916. OpenFileInfo = ViewContentViewModel.OpenFileInfo;
  917. }
  918. //获取页面设置等信息
  919. PDFViewer = pdfview;
  920. PageCount = PDFViewer.Document.PageCount;
  921. //避免加密文档,刚进入文档时抛异常
  922. if (PageCount > 0)
  923. {
  924. oldPageIndex = pdfview.CurrentIndex;
  925. }
  926. GetModeView(PDFViewer.ModeView);
  927. //设置下拉框选中项
  928. SetZoomIndex();
  929. if (PageCount > 0)
  930. {
  931. //文档解密后再获取当前页,以此来确保上下翻页按钮的状态的准确的
  932. CurrentPage = PDFViewer.CurrentIndex + 1;
  933. }
  934. CurrentZoom = PDFViewer.ZoomFactor * 100;
  935. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  936. }
  937. }
  938. public bool IsNavigationTarget(NavigationContext navigationContext)
  939. {
  940. return true;
  941. }
  942. public void OnNavigatedFrom(NavigationContext navigationContext)
  943. {
  944. IsShowViewModular = ViewContentViewModel.IsPropertyOpen;
  945. }
  946. #endregion Navigation
  947. }
  948. }