ReadModeContentViewModel.cs 14 KB

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