ReadModeContentViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. using Button = System.Windows.Controls.Button;
  23. using System.Windows.Controls.Primitives;
  24. namespace PDF_Office.ViewModels.PropertyPanel.ViewModular
  25. {
  26. public class ReadModeContentViewModel : BindableBase, INavigationAware
  27. {
  28. private ViewContentViewModel viewContentViewModel;
  29. private CPDFViewer PDFViewer;
  30. private int currentPage;
  31. public int CurrentPage
  32. {
  33. get { return currentPage; }
  34. set
  35. {
  36. SetProperty(ref currentPage, value);
  37. }
  38. }
  39. private int pageCount;
  40. public int PageCount
  41. {
  42. get { return pageCount; }
  43. set
  44. {
  45. SetProperty(ref pageCount, value);
  46. }
  47. }
  48. private bool CanNextPageExcute()
  49. {
  50. if (PDFViewer != null)
  51. {
  52. if (PDFViewer.CurrentIndex >= PDFViewer.Document.PageCount - 1)
  53. return false;
  54. else
  55. return true;
  56. }
  57. return false;
  58. }
  59. private bool CanPrePageExcute()
  60. {
  61. if (PDFViewer != null)
  62. {
  63. if (PDFViewer.CurrentIndex <= 0)
  64. return false;
  65. else
  66. return true;
  67. }
  68. return false;
  69. }
  70. private Button btnPrePage = null;
  71. private Button btnNextPage = null;
  72. private RepeatButton btnZoomOut = null;
  73. private RepeatButton btnZoomIn = null;
  74. public DelegateCommand<object> KeyDownCommand { get; set; }
  75. public DelegateCommand<object> PreviewKeyDownCommand { get; set; }
  76. public DelegateCommand<object> ZoomOutCommand { get; set; }
  77. public DelegateCommand<object> ZoomInCommand { get; set; }
  78. public DelegateCommand PrePageCommand { get; set; }
  79. public DelegateCommand NextPageCommand { get; set; }
  80. public DelegateCommand<object> LoadedCommand { get; set; }
  81. public ReadModeContentViewModel()
  82. {
  83. KeyDownCommand = new DelegateCommand<object>(KeyDownEvent);
  84. PreviewKeyDownCommand = new DelegateCommand<object>(PreviewKeyDownEvent);
  85. ZoomOutCommand = new DelegateCommand<object>(PageZoomOutEvent);
  86. ZoomInCommand = new DelegateCommand<object>(PageZoomInEvent);
  87. PrePageCommand = new DelegateCommand(PrePageEvent);
  88. NextPageCommand = new DelegateCommand(NextPageEvent);
  89. LoadedCommand = new DelegateCommand<object>(Loaded);
  90. }
  91. /// <summary>
  92. /// 页面加载
  93. /// </summary>
  94. /// <param name="obj"></param>
  95. private void Loaded(object obj)
  96. {
  97. if (obj is CompositeCommandParameter composite)
  98. {
  99. if (composite.Parameter is object[] array)
  100. {
  101. btnPrePage = array[0] as Button;
  102. btnNextPage = array[1] as Button;
  103. btnZoomOut = array[2] as RepeatButton;
  104. btnZoomIn = array[3] as RepeatButton;
  105. ChangeBtnZoom();
  106. ChangePageBtnState();
  107. }
  108. }
  109. }
  110. /// <summary>
  111. /// 下一页
  112. /// </summary>
  113. /// <param name="obj"></param>
  114. private void NextPageEvent()
  115. {
  116. if (PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Double || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.DoubleContinuous || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Book || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.BookContinuous)
  117. {
  118. if (((int)PDFViewer.Mode % 2) == 0)
  119. {
  120. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 2);
  121. }
  122. }
  123. else
  124. {
  125. PDFViewer.GoToPage(PDFViewer.CurrentIndex + 1);
  126. }
  127. }
  128. /// <summary>
  129. /// 上一页
  130. /// </summary>
  131. /// <param name="obj"></param>
  132. private void PrePageEvent()
  133. {
  134. PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
  135. }
  136. /// <summary>
  137. /// 放大
  138. /// </summary>
  139. /// <param name="obj"></param>
  140. private void PageZoomInEvent(object obj)
  141. {
  142. if (PDFViewer != null)
  143. {
  144. double zoom = SetPageZoomFactor(true);
  145. PDFViewer.Zoom(zoom / 100);
  146. ChangeBtnZoom();
  147. }
  148. }
  149. /// <summary>
  150. /// 设置缩放因子
  151. /// </summary>
  152. /// <param name="flag"></param>
  153. /// <returns></returns>
  154. private double SetPageZoomFactor(bool flag)
  155. {
  156. double zoom = PDFViewer.ZoomFactor * 100;
  157. if (flag)
  158. {
  159. if (zoom >= 25 && zoom <= 300)
  160. {
  161. zoom = zoom + 25;
  162. }
  163. else if (zoom >= 301 && zoom <= 1000)
  164. {
  165. zoom = zoom + 40;
  166. }
  167. else if (zoom >= 1001 && zoom <= 10000)
  168. {
  169. zoom = zoom + 100;
  170. }
  171. }
  172. else
  173. {
  174. if (zoom >= 25 && zoom <= 300)
  175. {
  176. zoom = zoom - 25;
  177. }
  178. else if (zoom >= 301 && zoom <= 1000)
  179. {
  180. zoom = zoom - 40;
  181. }
  182. else if (zoom >= 1001 && zoom <= 10000)
  183. {
  184. zoom = zoom - 100;
  185. }
  186. }
  187. if (zoom < 25)
  188. {
  189. zoom = 25;
  190. }
  191. if (zoom > 10000)
  192. {
  193. zoom = 10000;
  194. }
  195. return zoom;
  196. }
  197. /// <summary>
  198. /// 缩小
  199. /// </summary>
  200. /// <param name="obj"></param>
  201. private void PageZoomOutEvent(object obj)
  202. {
  203. if (PDFViewer != null)
  204. {
  205. double zoom = SetPageZoomFactor(false);
  206. PDFViewer.Zoom(zoom / 100);
  207. ChangeBtnZoom();
  208. }
  209. }
  210. /// <summary>
  211. /// 输入限制
  212. /// </summary>
  213. /// <param name="obj"></param>
  214. private void PreviewKeyDownEvent(object obj)
  215. {
  216. var args = obj as KeyEventArgs;
  217. if (args != null)
  218. {
  219. //显示文本框输入内容
  220. 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 };
  221. if (!NumberKeys.Contains(args.Key))
  222. {
  223. args.Handled = true;
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// 回车键保存
  229. /// </summary>
  230. /// <param name="obj"></param>
  231. private void KeyDownEvent(object obj)
  232. {
  233. if (obj is CompositeCommandParameter objs)
  234. {
  235. if (objs.EventArgs is System.Windows.Input.KeyEventArgs keyEventArgs)
  236. {
  237. TextBlock textBlock = objs.Parameter as TextBlock;
  238. if (keyEventArgs.Key == Key.Enter)
  239. {
  240. if (keyEventArgs.OriginalSource is System.Windows.Controls.TextBox txtCurrentPageNum)
  241. {
  242. int pagenum = 0;
  243. string text = txtCurrentPageNum.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.Document.PageCount;
  253. txtCurrentPageNum.Text = PDFViewer.Document.PageCount.ToString();
  254. return;
  255. }
  256. if (pagenum < 1)
  257. {
  258. pagenum = 1;
  259. }
  260. else if (pagenum > PDFViewer.Document.PageCount)
  261. {
  262. pagenum = PDFViewer.Document.PageCount;
  263. }
  264. PDFViewer.GoToPage(pagenum - 1);
  265. CurrentPage = pagenum;
  266. textBlock.Visibility = System.Windows.Visibility.Visible;
  267. txtCurrentPageNum.Text = pagenum.ToString();
  268. txtCurrentPageNum.Background = new SolidColorBrush(Colors.Transparent);
  269. //移动焦点
  270. txtCurrentPageNum.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
  271. }
  272. }
  273. }
  274. }
  275. }
  276. public bool IsNavigationTarget(NavigationContext navigationContext)
  277. {
  278. return true;
  279. }
  280. public void OnNavigatedFrom(NavigationContext navigationContext)
  281. {
  282. }
  283. public void OnNavigatedTo(NavigationContext navigationContext)
  284. {
  285. if (navigationContext.Parameters[ParameterNames.ViewContentViewModel] is ViewContentViewModel viewContentViewModel)
  286. {
  287. this.viewContentViewModel = viewContentViewModel;
  288. }
  289. if (navigationContext.Parameters[ParameterNames.PDFViewer] is CPDFViewer pdfview)
  290. {
  291. //获取页面设置等信息
  292. this.PDFViewer = pdfview;
  293. if (PDFViewer != null)
  294. {
  295. PageCount = PDFViewer.Document.PageCount;
  296. CurrentPage = PDFViewer.CurrentIndex + 1;
  297. this.PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  298. }
  299. }
  300. }
  301. /// <summary>
  302. /// 滚动的时候,参数变化
  303. /// </summary>
  304. /// <param name="sender"></param>
  305. /// <param name="e"></param>
  306. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  307. {
  308. if (e.Key == "SplieMode" || e.Key == "ViewMode")
  309. {
  310. return;
  311. }
  312. if (e.Key == "PageNum")
  313. {
  314. CurrentPage = (int)e.Value;
  315. PageCount = PDFViewer.Document.PageCount;
  316. ChangeBtnZoom();
  317. ChangePageBtnState();
  318. }
  319. }
  320. /// <summary>
  321. /// 改变缩放按钮
  322. /// </summary>
  323. private void ChangeBtnZoom()
  324. {
  325. if (PDFViewer == null)
  326. {
  327. return;
  328. }
  329. if (btnZoomOut == null && btnZoomIn == null)
  330. {
  331. return;
  332. }
  333. if (PDFViewer.ZoomFactor * 100 <= 25)
  334. {
  335. btnZoomOut.IsEnabled = false;
  336. btnZoomOut.Opacity = 0.5;
  337. btnZoomIn.IsEnabled = true;
  338. btnZoomIn.Opacity = 1;
  339. }
  340. else if (PDFViewer.ZoomFactor * 100 >= 25 && PDFViewer.ZoomFactor * 100 <= 10000)
  341. {
  342. btnZoomOut.Opacity = 1;
  343. btnZoomOut.IsEnabled = true;
  344. btnZoomIn.IsEnabled = true;
  345. btnZoomIn.Opacity = 1;
  346. }
  347. else
  348. {
  349. btnZoomIn.IsEnabled = false;
  350. btnZoomIn.Opacity = 0.5;
  351. btnZoomOut.IsEnabled = true;
  352. btnZoomOut.Opacity = 1;
  353. }
  354. }
  355. /// <summary>
  356. /// 改变上一页、下一页按钮
  357. /// </summary>
  358. private void ChangePageBtnState()
  359. {
  360. if (PDFViewer == null)
  361. {
  362. return;
  363. }
  364. if (btnPrePage == null && btnNextPage == null)
  365. {
  366. return;
  367. }
  368. if (PDFViewer.CurrentIndex + 1 <= 1)
  369. {
  370. btnPrePage.IsEnabled = false;
  371. btnPrePage.Opacity = 0.5;
  372. }
  373. else
  374. {
  375. btnPrePage.IsEnabled = true;
  376. btnPrePage.Opacity = 1;
  377. }
  378. if (PDFViewer.CurrentIndex + 1 >= PDFViewer.Document.PageCount)
  379. {
  380. btnNextPage.IsEnabled = false;
  381. btnNextPage.Opacity = 0.5;
  382. }
  383. else
  384. {
  385. btnNextPage.IsEnabled = true;
  386. btnNextPage.Opacity = 1;
  387. }
  388. }
  389. }
  390. }