ReadModeContentViewModel.cs 14 KB

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