PageContentViewModel.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Office.Interop.Word;
  4. using PDF_Master.Model;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Controls;
  15. using System.Windows.Input;
  16. namespace PDF_Master.ViewModels.PropertyPanel.ViewModular
  17. {
  18. internal class PageContentViewModel : BindableBase, INavigationAware
  19. {
  20. private IRegionManager region;
  21. private IDialogService dialogs;
  22. public CPDFViewer PDFViewer { get; set; }
  23. private int pageCount;
  24. public int PageCount
  25. {
  26. get { return pageCount; }
  27. set
  28. {
  29. SetProperty(ref pageCount, value);
  30. }
  31. }
  32. private int currentPage;
  33. public int CurrentPage
  34. {
  35. get { return currentPage; }
  36. set
  37. {
  38. SetProperty(ref currentPage, value);
  39. }
  40. }
  41. private void GoPrePage()
  42. {
  43. if (PDFViewer != null)
  44. {
  45. PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
  46. }
  47. }
  48. private bool CanPrePageExcute()
  49. {
  50. if (PDFViewer != null)
  51. {
  52. if (PDFViewer.CurrentIndex <= 0)
  53. return false;
  54. else
  55. return true;
  56. }
  57. return false;
  58. }
  59. private void GoNextPage()
  60. {
  61. if (PDFViewer != null)
  62. {
  63. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 1);
  64. //双页模式 一次性跳两页
  65. if (((int)PDFViewer.ModeView > 2))
  66. {
  67. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 2);
  68. }
  69. }
  70. }
  71. private bool CanNextPageExcute()
  72. {
  73. if (PDFViewer != null)
  74. {
  75. if (PDFViewer.CurrentIndex >= PDFViewer.Document.PageCount - 1)
  76. return false;
  77. else
  78. return true;
  79. }
  80. return false;
  81. }
  82. private void GoFirstPage()
  83. {
  84. if (PDFViewer != null)
  85. {
  86. PDFViewer.GoToPage(0);
  87. }
  88. }
  89. private void GoLastPage()
  90. {
  91. if (PDFViewer != null)
  92. {
  93. PDFViewer.GoToPage(PDFViewer.Document.PageCount - 1);
  94. }
  95. }
  96. private int selectedIndex;
  97. public int SelectedIndex
  98. {
  99. get { return selectedIndex; }
  100. set
  101. {
  102. SetProperty(ref selectedIndex, value);
  103. }
  104. }
  105. private double currenZoom;
  106. public double CurrentZoom
  107. {
  108. get { return currenZoom; }
  109. set { SetProperty(ref currenZoom, value); }
  110. }
  111. public DelegateCommand<object> PageTextKeyDownCommand { get; set; }
  112. public DelegateCommand<object> PageTextPreviewKeyDownCommand { get; set; }
  113. public DelegateCommand<object> SelectionChangedCommand { get; set; }
  114. public DelegateCommand FirstPageCommand { get; set; }
  115. public DelegateCommand LastPageCommand { get; set; }
  116. public DelegateCommand PrePageCommand { get; set; }
  117. public DelegateCommand NextPageCommand { get; set; }
  118. public PageContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  119. {
  120. region = regionManager;
  121. dialogs = dialogService;
  122. PrePageCommand = new DelegateCommand(GoPrePage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  123. NextPageCommand = new DelegateCommand(GoNextPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  124. FirstPageCommand = new DelegateCommand(GoFirstPage, CanPrePageExcute).ObservesProperty(() => CurrentPage);
  125. LastPageCommand = new DelegateCommand(GoLastPage, CanNextPageExcute).ObservesProperty(() => CurrentPage);
  126. PageTextKeyDownCommand = new DelegateCommand<object>(PageNumText_KeyDown);
  127. PageTextPreviewKeyDownCommand = new DelegateCommand<object>(PageNumText_PreviewKeyDown);
  128. SelectionChangedCommand = new DelegateCommand<object>(SelectionChangedEvent);
  129. }
  130. private void SelectionChangedEvent(object e)
  131. {
  132. var args = e as SelectionChangedEventArgs;
  133. if (args != null)
  134. {
  135. if (SelectedIndex <= 2)
  136. {
  137. switch (SelectedIndex)
  138. {
  139. case 0:
  140. PDFViewer.ChangeFitMode(FitMode.FitSize);
  141. break;
  142. case 1:
  143. PDFViewer.ChangeFitMode(FitMode.FitWidth);
  144. break;
  145. case 2:
  146. PDFViewer.ChangeFitMode(FitMode.FitHeight);
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. else
  153. {
  154. if (args.AddedItems[0] is ComboBoxItem)
  155. {
  156. if ((args.AddedItems[0] as ComboBoxItem).Tag != null)
  157. {
  158. PDFViewer.Zoom(Convert.ToDouble((args.AddedItems[0] as ComboBoxItem).Tag.ToString()) / 100.0);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. private void PageNumText_PreviewKeyDown(object e)
  165. {
  166. var args = e as KeyEventArgs;
  167. if (args != null)
  168. {
  169. //显示文本框输入内容
  170. 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,
  171. Key.NumPad3,Key.NumPad4,Key.NumPad5,Key.NumPad6,Key.NumPad7,Key.NumPad8,Key.NumPad9,Key.Delete,Key.Back,Key.Enter,Key.Right,Key.Left};
  172. if (!NumberKeys.Contains(args.Key))
  173. {
  174. args.Handled = true;
  175. }
  176. }
  177. }
  178. private void PageNumText_KeyDown(object e)
  179. {
  180. if (!(e is KeyEventArgs))
  181. {
  182. return;
  183. }
  184. //回车提交输入
  185. if ((e as KeyEventArgs).Key == System.Windows.Input.Key.Enter)
  186. {
  187. int pagenum = 0;
  188. string text = ((e as KeyEventArgs).OriginalSource as TextBox).Text.ToString();
  189. char[] chs = { ' ', '/', '\\' };//字符截取 拒止非法输入
  190. int i = text.LastIndexOfAny(chs);
  191. if (i > 0)
  192. {
  193. text = text.Substring(0, i - 1);
  194. }
  195. if (!int.TryParse(text, out pagenum))
  196. {
  197. CurrentPage = PDFViewer.Document.PageCount;
  198. ((e as KeyEventArgs).OriginalSource as TextBox).Text = PDFViewer.Document.PageCount.ToString();
  199. return;
  200. }
  201. if (pagenum < 1)
  202. {
  203. pagenum = 1;
  204. }
  205. else if (pagenum > PDFViewer.Document.PageCount)
  206. {
  207. pagenum = PDFViewer.Document.PageCount;
  208. }
  209. PDFViewer.GoToPage(pagenum - 1);
  210. CurrentPage = pagenum;
  211. ((e as KeyEventArgs).OriginalSource as TextBox).Text = pagenum.ToString();
  212. }
  213. }
  214. public bool IsNavigationTarget(NavigationContext navigationContext)
  215. {
  216. return true;
  217. }
  218. public void OnNavigatedFrom(NavigationContext navigationContext)
  219. {
  220. }
  221. public void OnNavigatedTo(NavigationContext navigationContext)
  222. {
  223. if (navigationContext.Parameters[ParameterNames.PDFViewer] is CPDFViewer viewer)
  224. {
  225. PDFViewer = viewer;
  226. PageCount = PDFViewer.Document.PageCount;
  227. CurrentPage = PDFViewer.CurrentIndex + 1;
  228. CurrentZoom = PDFViewer.ZoomFactor * 100;
  229. this.PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  230. }
  231. }
  232. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  233. {
  234. if (e.Key == "PageNum")
  235. {
  236. RenderData renderData = e.Value as RenderData;
  237. if (renderData != null)
  238. {
  239. CurrentPage = renderData.PageIndex;
  240. PageCount = PDFViewer.Document.PageCount;
  241. }
  242. }
  243. if (e.Key == "SplieMode" || e.Key == "ViewMode")
  244. {
  245. return;
  246. }
  247. if (e.Key == "Zoom")
  248. {
  249. CurrentZoom = (double)e.Value * 100;
  250. SelectedIndex = -1;
  251. }
  252. }
  253. }
  254. }