PageContentViewModel.cs 9.5 KB

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