ReadModeContentViewModel.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using ComPDFKitViewer.PdfViewer;
  2. using Dropbox.Api.Sharing;
  3. using PDF_Office.Helper;
  4. using PDF_Office.Model;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Threading.Tasks;
  14. using System.Windows.Controls;
  15. using System.Windows.Forms;
  16. using System.Windows.Input;
  17. using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrayNotify;
  18. using System.Windows.Media;
  19. using KeyEventArgs = System.Windows.Input.KeyEventArgs;
  20. using TextBox = System.Windows.Controls.TextBox;
  21. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  22. namespace PDF_Office.ViewModels.PropertyPanel.ViewModular
  23. {
  24. public class ReadModeContentViewModel : BindableBase, INavigationAware
  25. {
  26. private ViewContentViewModel viewContentViewModel;
  27. private CPDFViewer PDFViewer;
  28. private int currentPage;
  29. public int CurrentPage
  30. {
  31. get { return currentPage; }
  32. set
  33. {
  34. SetProperty(ref currentPage, value);
  35. }
  36. }
  37. private int pageCount;
  38. public int PageCount
  39. {
  40. get { return pageCount; }
  41. set
  42. {
  43. SetProperty(ref pageCount, value);
  44. }
  45. }
  46. public DelegateCommand<object> KeyDownCommand { get; set; }
  47. public DelegateCommand<object> PreviewKeyDownCommand { get; set; }
  48. public DelegateCommand<object> ZoomOutCommand { get; set; }
  49. public DelegateCommand<object> ZoomInCommand { get; set; }
  50. public DelegateCommand<object> PrePageCommand { get; set; }
  51. public DelegateCommand<object> NextPageCommand { get; set; }
  52. public ReadModeContentViewModel()
  53. {
  54. KeyDownCommand = new DelegateCommand<object>(KeyDownEvent);
  55. PreviewKeyDownCommand = new DelegateCommand<object>(PreviewKeyDownEvent);
  56. ZoomOutCommand = new DelegateCommand<object>(PageZoomOutEvent);
  57. ZoomInCommand = new DelegateCommand<object>(PageZoomInEvent);
  58. PrePageCommand = new DelegateCommand<object>(PrePageEvent);
  59. NextPageCommand = new DelegateCommand<object>(NextPageEvent);
  60. }
  61. private void NextPageEvent(object obj)
  62. {
  63. if (PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Double || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.DoubleContinuous || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Book || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.BookContinuous)
  64. {
  65. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 2);
  66. }
  67. else
  68. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 1);
  69. }
  70. private void PrePageEvent(object obj)
  71. {
  72. if (PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Double || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.DoubleContinuous || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Book || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.BookContinuous)
  73. {
  74. PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
  75. }
  76. else
  77. {
  78. PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
  79. }
  80. }
  81. private void PageZoomInEvent(object obj)
  82. {
  83. double zoom = PDFViewer.ZoomFactor * 100;
  84. zoom = zoom + 25;
  85. if (zoom != 0 && zoom <= 1000 && PDFViewer != null)
  86. {
  87. PDFViewer.Zoom(zoom / 100);
  88. }
  89. }
  90. private void PageZoomOutEvent(object obj)
  91. {
  92. double zoom = PDFViewer.ZoomFactor * 100;
  93. zoom = zoom - 25;
  94. if (zoom != 0 && PDFViewer != null)
  95. {
  96. PDFViewer.Zoom(zoom / 100);
  97. }
  98. }
  99. private void PreviewKeyDownEvent(object obj)
  100. {
  101. var args = obj as KeyEventArgs;
  102. if (args != null)
  103. {
  104. //显示文本框输入内容
  105. 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,Key.NumPad3,Key.NumPad4,Key.NumPad5,Key.NumPad6,Key.NumPad7,Key.NumPad8,Key.NumPad9,Key.Delete,Key.Back,Key.Enter,Key.Right,Key.Left};
  106. if (!NumberKeys.Contains(args.Key))
  107. {
  108. args.Handled = true;
  109. }
  110. }
  111. }
  112. private void KeyDownEvent(object obj)
  113. {
  114. if (obj is CompositeCommandParameter objs)
  115. {
  116. if (objs.EventArgs is System.Windows.Input.KeyEventArgs keyEventArgs)
  117. {
  118. TextBlock textBlock = objs.Parameter as TextBlock;
  119. if (keyEventArgs.Key == Key.Enter)
  120. {
  121. if (keyEventArgs.OriginalSource is System.Windows.Controls.TextBox txtCurrentPageNum)
  122. {
  123. int pagenum = 0;
  124. string text = txtCurrentPageNum.Text.ToString();
  125. char[] chs = { ' ', '/', '\\' };//字符截取 拒止非法输入
  126. int i = text.LastIndexOfAny(chs);
  127. if (i > 0)
  128. {
  129. text = text.Substring(0, i - 1);
  130. }
  131. if (!int.TryParse(text, out pagenum))
  132. {
  133. CurrentPage = PDFViewer.Document.PageCount;
  134. txtCurrentPageNum.Text = PDFViewer.Document.PageCount.ToString();
  135. return;
  136. }
  137. if (pagenum < 1)
  138. {
  139. pagenum = 1;
  140. }
  141. else if (pagenum > PDFViewer.Document.PageCount)
  142. {
  143. pagenum = PDFViewer.Document.PageCount;
  144. }
  145. PDFViewer.GoToPage(pagenum - 1);
  146. CurrentPage = pagenum;
  147. txtCurrentPageNum.Text = pagenum.ToString();
  148. textBlock.Visibility = System.Windows.Visibility.Visible;
  149. txtCurrentPageNum.Background = new SolidColorBrush(Colors.Transparent);
  150. txtCurrentPageNum.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
  151. }
  152. }
  153. }
  154. }
  155. }
  156. public bool IsNavigationTarget(NavigationContext navigationContext)
  157. {
  158. return true;
  159. }
  160. public void OnNavigatedFrom(NavigationContext navigationContext)
  161. {
  162. }
  163. public void OnNavigatedTo(NavigationContext navigationContext)
  164. {
  165. var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  166. if (viewContentViewModel != null)
  167. {
  168. this.viewContentViewModel = viewContentViewModel;
  169. }
  170. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  171. if (pdfview != null)
  172. {
  173. //获取页面设置等信息
  174. this.PDFViewer = pdfview;
  175. PageCount = PDFViewer.Document.PageCount;
  176. CurrentPage = PDFViewer.CurrentIndex + 1;
  177. this.PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  178. }
  179. }
  180. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  181. {
  182. if (e.Key == "SplieMode" || e.Key == "ViewMode")
  183. {
  184. return;
  185. }
  186. PageCount = PDFViewer.Document.PageCount;
  187. CurrentPage = PDFViewer.CurrentIndex + 1;
  188. }
  189. }
  190. }