PageContentViewModel.cs 12 KB

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