BottomToolContentViewModel.cs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. /// <summary>
  349. /// 上下滑动的固定值
  350. /// </summary>
  351. private double scrolloffset = 50;
  352. private IRegionManager region;
  353. private IDialogService dialogs;
  354. public BottomToolContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  355. {
  356. region = regionManager;
  357. dialogs = dialogService;
  358. PrePageCommand = new DelegateCommand(GoPrePage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  359. NextPageCommand = new DelegateCommand(GoNextPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  360. FirstPageCommand = new DelegateCommand(GoFirstPage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  361. LastPageCommand = new DelegateCommand(GoLastPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  362. ZoomInCommand = new DelegateCommand(ZoomIn, CanZoomIn).ObservesProperty(() => CurrentZoom);
  363. ZoomOutCommand = new DelegateCommand(ZoomOut, CanZoomOut).ObservesProperty(() => CurrentZoom);
  364. SetViewModeCommand = new DelegateCommand(SetModeView);
  365. PageTextKeyDownCommand = new DelegateCommand<object>(PageNumText_KeyDown);
  366. PageTextPreviewKeyDownCommand = new DelegateCommand<object>(PageNumText_PreviewKeyDown);
  367. SelectionChangedCommand = new DelegateCommand<object>(SelectionChangedEvent);
  368. ShowViewModularCommand = new DelegateCommand<object>(ShowViewModularEvent);
  369. PreviewKeyDown = new DelegateCommand<object>(previewkeydown);
  370. PreViewCommand = new DelegateCommand(undo);
  371. SinglePageCommand = new DelegateCommand(() => { IsSingleView = true; });
  372. DoublePageCommand = new DelegateCommand(() => { IsDoubleView = true; });
  373. RealSizeCommand = new DelegateCommand(ZoomToRealSize);
  374. NextViewCommnad = new DelegateCommand(redo);
  375. InitString();
  376. }
  377. /// <summary>
  378. /// 缩放至实际大小
  379. /// </summary>
  380. private void ZoomToRealSize()
  381. {
  382. PDFViewer?.ChangeFitMode(FitMode.FitSize);
  383. if (OpenFileInfo != null)
  384. {
  385. OpenFileInfo.LastFitMode = FitMode.FitSize;
  386. }
  387. }
  388. private void previewkeydown(object e)
  389. {
  390. var args = e as KeyEventArgs;
  391. if (args == null)
  392. {
  393. return;
  394. }
  395. ////枚举 过滤所有不合理的输入
  396. //if (args.Key != Key.Enter && args.Key != Key.Oem5 && args.Key != Key.Back && args.Key != Key.OemComma && args.Key != Key.OemMinus && args.Key != Key.Subtract && !((args.Key >= Key.D0 && args.Key <= Key.D9) || (args.Key >= Key.NumPad0 && args.Key <= Key.NumPad9)))
  397. //{
  398. // args.Handled = true;
  399. //}
  400. if (args.Key == Key.Enter)
  401. {
  402. var text = (args.OriginalSource as TextBox);
  403. int zoom = int.Parse(text.Text.Replace("%", ""));
  404. PDFViewer?.Zoom(zoom / 100.0);
  405. }
  406. }
  407. private void undo()
  408. {
  409. if (!IsPreEnable)
  410. {
  411. return;
  412. }
  413. RedoStack.Push(PDFViewer.CurrentIndex);
  414. IsNextEnable = true;
  415. App.Current.Dispatcher.Invoke(() =>
  416. {
  417. int index = UndoStack.Pop();
  418. PDFViewer.GoToPage(index);
  419. if (UndoStack.Count <= 0)
  420. {
  421. IsPreEnable = false;
  422. }
  423. });
  424. isFromMethod = true;
  425. }
  426. private void redo()
  427. {
  428. if (!IsNextEnable)
  429. {
  430. return;
  431. }
  432. App.Current.Dispatcher.Invoke(() =>
  433. {
  434. UndoStack.Push(PDFViewer.CurrentIndex);
  435. IsPreEnable = true;
  436. int index = RedoStack.Pop();
  437. PDFViewer.GoToPage(index);
  438. if (RedoStack.Count <= 0)
  439. {
  440. IsNextEnable = false;
  441. }
  442. });
  443. isFromMethod = true;
  444. }
  445. private void ShowViewModularEvent(object obj)
  446. {
  447. if (region.Regions.ContainsRegionWithName(ViewContentViewModel.PropertyRegionName))
  448. {
  449. var check = (bool)obj;
  450. if (check)
  451. {
  452. ViewContentViewModel.IsPropertyOpen = true;
  453. NavigationParameters param = new NavigationParameters();
  454. param.Add(ParameterNames.PDFViewer, PDFViewer);
  455. param.Add(ParameterNames.BottomToolContentViewModel, this);
  456. region.RequestNavigate(RegionNames.PropertyRegionName, "ViewModularContent", param);
  457. }
  458. else
  459. {
  460. ViewContentViewModel.IsPropertyOpen = false;
  461. region.RequestNavigate(RegionNames.PropertyRegionName, "PropertyPanelContent");
  462. }
  463. }
  464. }
  465. private void SelectionChangedEvent(object e)
  466. {
  467. if (e == null)
  468. {
  469. return;
  470. }
  471. var args = e as SelectionChangedEventArgs;
  472. if (args != null)
  473. {
  474. if (SelectedIndex <= 2)
  475. {
  476. switch (SelectedIndex)
  477. {
  478. case 0:
  479. PDFViewer?.ChangeFitMode(FitMode.FitSize);
  480. if (OpenFileInfo != null)
  481. {
  482. OpenFileInfo.LastFitMode = FitMode.FitSize;
  483. }
  484. break;
  485. case 1:
  486. PDFViewer?.ChangeFitMode(FitMode.FitWidth);
  487. if (OpenFileInfo != null)
  488. {
  489. OpenFileInfo.LastFitMode = FitMode.FitWidth;
  490. }
  491. break;
  492. case 2:
  493. PDFViewer?.ChangeFitMode(FitMode.FitHeight);
  494. if (OpenFileInfo != null)
  495. {
  496. OpenFileInfo.LastFitMode = FitMode.FitHeight;
  497. }
  498. break;
  499. default:
  500. break;
  501. }
  502. }
  503. else
  504. {
  505. if (args.AddedItems[0] is ComboBoxItem)
  506. {
  507. if ((args.AddedItems[0] as ComboBoxItem).Tag != null)
  508. {
  509. PDFViewer?.Zoom(Convert.ToDouble((args.AddedItems[0] as ComboBoxItem).Tag.ToString()) / 100.0);
  510. }
  511. }
  512. }
  513. }
  514. }
  515. private void PageNumText_PreviewKeyDown(object e)
  516. {
  517. var args = e as KeyEventArgs;
  518. if (args != null)
  519. {
  520. //显示文本框输入内容
  521. 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,
  522. Key.NumPad3,Key.NumPad4,Key.NumPad5,Key.NumPad6,Key.NumPad7,Key.NumPad8,Key.NumPad9,Key.Delete,Key.Back,Key.Enter,Key.Right,Key.Left};
  523. if (!NumberKeys.Contains(args.Key))
  524. {
  525. args.Handled = true;
  526. }
  527. }
  528. }
  529. private void PageNumText_KeyDown(object e)
  530. {
  531. if (!(e is KeyEventArgs))
  532. return;
  533. //回车提交输入
  534. if ((e as KeyEventArgs).Key == System.Windows.Input.Key.Enter)
  535. {
  536. int pagenum = 0;
  537. string text = ((e as KeyEventArgs).OriginalSource as TextBox).Text.ToString();
  538. char[] chs = { ' ', '/', '\\' };//字符截取 拒止非法输入
  539. int i = text.LastIndexOfAny(chs);
  540. if (i > 0)
  541. text = text.Substring(0, i - 1);
  542. if (!int.TryParse(text, out pagenum))
  543. {
  544. CurrentPage = PDFViewer.Document.PageCount;
  545. ((e as KeyEventArgs).OriginalSource as TextBox).Text = PDFViewer.Document.PageCount.ToString();
  546. return;
  547. }
  548. if (pagenum < 1)
  549. pagenum = 1;
  550. else if (pagenum > PDFViewer.Document.PageCount)
  551. pagenum = PDFViewer.Document.PageCount;
  552. PDFViewer.GoToPage(pagenum - 1);
  553. CurrentPage = pagenum;
  554. ((e as KeyEventArgs).OriginalSource as TextBox).Text = pagenum.ToString();
  555. }
  556. }
  557. private void GoPrePage()
  558. {
  559. if (PDFViewer != null)
  560. {
  561. if (PDFViewer.RenderPageStart == PDFViewer.CurrentIndex)
  562. {
  563. PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
  564. }
  565. else
  566. {
  567. if ((PDFViewer.RenderPageStart - 1) > 0)
  568. {
  569. PDFViewer.GoToPage(PDFViewer.RenderPageStart - 1);
  570. }
  571. else
  572. {
  573. PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
  574. }
  575. }
  576. }
  577. }
  578. private bool CanPrePageExcute()
  579. {
  580. if (PDFViewer != null)
  581. {
  582. if (PDFViewer.CurrentIndex <= 0)
  583. return false;
  584. else
  585. return true;
  586. }
  587. return false;
  588. }
  589. private void GoNextPage()
  590. {
  591. if (PDFViewer != null)
  592. {
  593. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 1);
  594. //双页模式 一次性跳两页
  595. if (((int)PDFViewer.ModeView > 2))
  596. {
  597. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 2);
  598. }
  599. }
  600. }
  601. private bool CanNextPageExcute()
  602. {
  603. if (PDFViewer != null)
  604. {
  605. if (CurrentPage >= PDFViewer.Document.PageCount)
  606. return false;
  607. else
  608. return true;
  609. }
  610. return false;
  611. }
  612. private void GoFirstPage()
  613. {
  614. if (PDFViewer != null)
  615. {
  616. PDFViewer.GoToPage(0);
  617. }
  618. }
  619. private void GoLastPage()
  620. {
  621. if (PDFViewer != null)
  622. {
  623. PDFViewer.GoToPage(PDFViewer.Document.PageCount - 1);
  624. }
  625. }
  626. private void ZoomIn()
  627. {
  628. double zoom = SetPageZoomFactor(true);
  629. PDFViewer.Zoom(zoom / 100);
  630. }
  631. private void ZoomOut()
  632. {
  633. double zoom = SetPageZoomFactor(false);
  634. PDFViewer.Zoom(zoom / 100);
  635. }
  636. private double SetPageZoomFactor(bool flag)
  637. {
  638. double zoom = PDFViewer.ZoomFactor * 100;
  639. if (flag)
  640. {
  641. if (zoom >= 25 && zoom <= 300)
  642. {
  643. zoom = zoom + 25;
  644. }
  645. else if (zoom >= 301 && zoom <= 1000)
  646. {
  647. zoom = zoom + 40;
  648. }
  649. else if (zoom >= 1001 && zoom <= 10000)
  650. {
  651. zoom = zoom + 100;
  652. }
  653. }
  654. else
  655. {
  656. if (zoom >= 25 && zoom <= 300)
  657. {
  658. zoom = zoom - 25;
  659. }
  660. else if (zoom >= 301 && zoom <= 1000)
  661. {
  662. zoom = zoom - 40;
  663. }
  664. else if (zoom >= 1001 && zoom <= 10000)
  665. {
  666. zoom = zoom - 100;
  667. }
  668. }
  669. if (zoom < 25)
  670. {
  671. zoom = 25;
  672. }
  673. if (zoom > 10000)
  674. {
  675. zoom = 10000;
  676. }
  677. return zoom;
  678. }
  679. private bool CanZoomIn()
  680. {
  681. if (PDFViewer != null)
  682. {
  683. if (PDFViewer.ZoomFactor * 100 >= 10000)
  684. return false;
  685. else
  686. return true;
  687. }
  688. return false;
  689. }
  690. private bool CanZoomOut()
  691. {
  692. if (PDFViewer != null)
  693. {
  694. if (PDFViewer.ZoomFactor * 100 <= 25)
  695. return false;
  696. else
  697. return true;
  698. }
  699. return false;
  700. }
  701. public void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  702. {
  703. if (e.Key == "PageNum")
  704. {
  705. RenderData renderData = e.Value as RenderData;
  706. if (renderData != null)
  707. {
  708. CurrentPage = renderData.PageIndex;
  709. PageCount = PDFViewer.Document.PageCount;
  710. if (!renderData.IsMouseWheel && !isFromMethod)
  711. {
  712. UndoStack.Push(oldPageIndex);
  713. RedoStack.Clear();
  714. IsNextEnable = false;
  715. IsPreEnable = true;
  716. }
  717. isFromMethod = false;
  718. }
  719. oldPageIndex = renderData.PageIndex - 1;
  720. }
  721. if (e.Key == "ViewMode")
  722. {
  723. GetModeView((ViewMode)e.Value);
  724. }
  725. if (e.Key == "Zoom")
  726. {
  727. CurrentZoom = (double)e.Value * 100;
  728. switch (PDFViewer.ModeFit)
  729. {
  730. case FitMode.FitWidth:
  731. SelectedIndex = 1;
  732. break;
  733. case FitMode.FitHeight:
  734. SelectedIndex = 2;
  735. break;
  736. case FitMode.FitSize:
  737. SelectedIndex = 0;
  738. break;
  739. case FitMode.FitFree:
  740. SelectedIndex = -1;
  741. break;
  742. default:
  743. break;
  744. }
  745. }
  746. if (OpenFileInfo != null)
  747. {
  748. if (OpenFileInfo.LastFitMode != PDFViewer.ModeFit)
  749. {
  750. OpenFileInfo.LastFitMode = PDFViewer.ModeFit;
  751. }
  752. if (PDFViewer.ModeFit == FitMode.FitFree)
  753. {
  754. if (OpenFileInfo.LastZoom != PDFViewer.ZoomFactor)
  755. {
  756. OpenFileInfo.LastZoom = PDFViewer.ZoomFactor;
  757. }
  758. }
  759. if (OpenFileInfo.LastPageIndex != PDFViewer.CurrentIndex)
  760. {
  761. OpenFileInfo.LastPageIndex = PDFViewer.CurrentIndex;
  762. }
  763. SettingHelper.SetFileInfo(OpenFileInfo);
  764. }
  765. }
  766. private void ViewContentViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  767. {
  768. if (e.PropertyName == "OCRContentVisible")
  769. {
  770. if (ViewContentViewModel.OCRContentVisible == Visibility.Visible)
  771. {
  772. RightPanelEnable = false;
  773. }
  774. else
  775. {
  776. RightPanelEnable = true;
  777. }
  778. }
  779. if (e.PropertyName == "IsPropertyOpen")
  780. {
  781. //右上角按钮收起属性面板后,底部工具栏按钮状态同步变化
  782. if (ViewContentViewModel.IsPropertyOpen == false)
  783. {
  784. //监听的属性面板的变化
  785. IsShowViewModular = ViewContentViewModel.IsPropertyOpen;
  786. }
  787. }
  788. }
  789. /// <summary>
  790. /// 设置缩放控件的选中项
  791. /// </summary>
  792. private void SetZoomIndex()
  793. {
  794. switch (PDFViewer.ModeFit)
  795. {
  796. case FitMode.FitWidth:
  797. SelectedIndex = 1;
  798. break;
  799. case FitMode.FitHeight:
  800. SelectedIndex = 2;
  801. break;
  802. case FitMode.FitSize:
  803. SelectedIndex = 0;
  804. break;
  805. case FitMode.FitFree:
  806. SelectedIndex = -1;
  807. break;
  808. default:
  809. break;
  810. }
  811. }
  812. private void GetModeView(ViewMode mode)
  813. {
  814. if ((int)mode % 2 == 0)
  815. {
  816. if (!IsContinue)
  817. {
  818. IsContinue = true;
  819. }
  820. }
  821. else
  822. {
  823. if (IsContinue)
  824. {
  825. IsContinue = false;
  826. }
  827. }
  828. if ((int)mode <= 2)
  829. {
  830. if (!isSingleView)
  831. {
  832. IsSingleView = true;
  833. }
  834. }
  835. else if ((int)mode <= 4)
  836. {
  837. if (!isDoubleView)
  838. {
  839. IsDoubleView = true;
  840. }
  841. }
  842. else
  843. {
  844. if (!isBookMode)
  845. {
  846. IsBookMode = true;
  847. }
  848. }
  849. }
  850. public void SetModeView()
  851. {
  852. if (PDFViewer != null)
  853. {
  854. if (IsContinue)
  855. {
  856. if (IsSingleView)
  857. {
  858. PDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
  859. }
  860. else if (IsDoubleView)
  861. {
  862. PDFViewer.ChangeViewMode(ViewMode.DoubleContinuous);
  863. }
  864. else
  865. {
  866. PDFViewer.ChangeViewMode(ViewMode.BookContinuous);
  867. }
  868. }
  869. else
  870. {
  871. if (IsSingleView)
  872. {
  873. PDFViewer.ChangeViewMode(ViewMode.Single);
  874. }
  875. else if (IsDoubleView)
  876. {
  877. PDFViewer.ChangeViewMode(ViewMode.Double);
  878. }
  879. else
  880. {
  881. PDFViewer.ChangeViewMode(ViewMode.Book);
  882. }
  883. }
  884. }
  885. SaveMode(PDFViewer.ModeView);
  886. }
  887. private void SaveMode(object modeView)
  888. {
  889. OpenFileInfo fileInfo = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  890. if (fileInfo != null)
  891. {
  892. if (modeView is SplitMode)
  893. {
  894. fileInfo.LastSplitMode = PDFViewer.Mode;
  895. }
  896. else if (modeView is ViewMode)
  897. {
  898. fileInfo.LastViewMode = PDFViewer.ModeView;
  899. }
  900. else if (modeView is bool)
  901. {
  902. fileInfo.LastPageSpace = (bool)modeView;
  903. }
  904. SettingHelper.SetFileInfo(fileInfo);
  905. }
  906. }
  907. #region Navigation
  908. public void OnNavigatedTo(NavigationContext navigationContext)
  909. {
  910. var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  911. if (viewContentViewModel != null)
  912. {
  913. this.ViewContentViewModel = viewContentViewModel;
  914. ViewContentViewModel.bottom = this;
  915. IsShowViewModular = viewContentViewModel.IsPropertyOpen;
  916. viewContentViewModel.PropertyChanged -= ViewContentViewModel_PropertyChanged;
  917. viewContentViewModel.PropertyChanged += ViewContentViewModel_PropertyChanged;
  918. }
  919. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  920. if (pdfview != null)
  921. {
  922. if (ViewContentViewModel.OpenFileInfo != null)
  923. {
  924. OpenFileInfo = ViewContentViewModel.OpenFileInfo;
  925. }
  926. //获取页面设置等信息
  927. PDFViewer = pdfview;
  928. oldPageIndex = pdfview.CurrentIndex;
  929. PageCount = PDFViewer.Document.PageCount;
  930. GetModeView(PDFViewer.ModeView);
  931. //设置下拉框选中项
  932. SetZoomIndex();
  933. if (PageCount > 0)
  934. {
  935. //文档解密后再获取当前页,以此来确保上下翻页按钮的状态的准确的
  936. CurrentPage = PDFViewer.CurrentIndex + 1;
  937. }
  938. CurrentZoom = PDFViewer.ZoomFactor * 100;
  939. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  940. }
  941. }
  942. public bool IsNavigationTarget(NavigationContext navigationContext)
  943. {
  944. return true;
  945. }
  946. public void OnNavigatedFrom(NavigationContext navigationContext)
  947. {
  948. IsShowViewModular = ViewContentViewModel.IsPropertyOpen;
  949. }
  950. #endregion Navigation
  951. }
  952. }