123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- using ComPDFKitViewer.PdfViewer;
- using Dropbox.Api.Sharing;
- using PDF_Office.Helper;
- using PDF_Office.Model;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- using System.Windows.Forms;
- using System.Windows.Input;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrayNotify;
- using System.Windows.Media;
- using KeyEventArgs = System.Windows.Input.KeyEventArgs;
- using TextBox = System.Windows.Controls.TextBox;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement;
- namespace PDF_Office.ViewModels.PropertyPanel.ViewModular
- {
- public class ReadModeContentViewModel : BindableBase, INavigationAware
- {
- private ViewContentViewModel viewContentViewModel;
- private CPDFViewer PDFViewer;
- private int currentPage;
- public int CurrentPage
- {
- get { return currentPage; }
- set
- {
- SetProperty(ref currentPage, value);
- }
- }
- private int pageCount;
- public int PageCount
- {
- get { return pageCount; }
- set
- {
- SetProperty(ref pageCount, value);
- }
- }
- public DelegateCommand<object> KeyDownCommand { get; set; }
- public DelegateCommand<object> PreviewKeyDownCommand { get; set; }
- public DelegateCommand<object> ZoomOutCommand { get; set; }
- public DelegateCommand<object> ZoomInCommand { get; set; }
- public DelegateCommand<object> PrePageCommand { get; set; }
- public DelegateCommand<object> NextPageCommand { get; set; }
- public ReadModeContentViewModel()
- {
- KeyDownCommand = new DelegateCommand<object>(KeyDownEvent);
- PreviewKeyDownCommand = new DelegateCommand<object>(PreviewKeyDownEvent);
- ZoomOutCommand = new DelegateCommand<object>(PageZoomOutEvent);
- ZoomInCommand = new DelegateCommand<object>(PageZoomInEvent);
- PrePageCommand = new DelegateCommand<object>(PrePageEvent);
- NextPageCommand = new DelegateCommand<object>(NextPageEvent);
- }
- private void NextPageEvent(object obj)
- {
- if (PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Double || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.DoubleContinuous || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Book || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.BookContinuous)
- {
- PDFViewer.GoToPage(PDFViewer.CurrentIndex + 2);
- }
- else
- PDFViewer.GoToPage(PDFViewer.CurrentIndex + 1);
- }
- private void PrePageEvent(object obj)
- {
- if (PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Double || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.DoubleContinuous || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Book || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.BookContinuous)
- {
- PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
- }
- else
- {
- PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
- }
- }
- private void PageZoomInEvent(object obj)
- {
- double zoom = PDFViewer.ZoomFactor * 100;
- zoom = zoom + 25;
- if (zoom != 0 && zoom <= 1000 && PDFViewer != null)
- {
- PDFViewer.Zoom(zoom / 100);
- }
- }
- private void PageZoomOutEvent(object obj)
- {
- double zoom = PDFViewer.ZoomFactor * 100;
- zoom = zoom - 25;
- if (zoom != 0 && PDFViewer != null)
- {
- PDFViewer.Zoom(zoom / 100);
- }
- }
- private void PreviewKeyDownEvent(object obj)
- {
- var args = obj as KeyEventArgs;
- if (args != null)
- {
- //显示文本框输入内容
- 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};
- if (!NumberKeys.Contains(args.Key))
- {
- args.Handled = true;
- }
- }
- }
- private void KeyDownEvent(object obj)
- {
- if (obj is CompositeCommandParameter objs)
- {
- if (objs.EventArgs is System.Windows.Input.KeyEventArgs keyEventArgs)
- {
- TextBlock textBlock = objs.Parameter as TextBlock;
- if (keyEventArgs.Key == Key.Enter)
- {
- if (keyEventArgs.OriginalSource is System.Windows.Controls.TextBox txtCurrentPageNum)
- {
- int pagenum = 0;
- string text = txtCurrentPageNum.Text.ToString();
- char[] chs = { ' ', '/', '\\' };//字符截取 拒止非法输入
- int i = text.LastIndexOfAny(chs);
- if (i > 0)
- {
- text = text.Substring(0, i - 1);
- }
- if (!int.TryParse(text, out pagenum))
- {
- CurrentPage = PDFViewer.Document.PageCount;
- txtCurrentPageNum.Text = PDFViewer.Document.PageCount.ToString();
- return;
- }
- if (pagenum < 1)
- {
- pagenum = 1;
- }
- else if (pagenum > PDFViewer.Document.PageCount)
- {
- pagenum = PDFViewer.Document.PageCount;
- }
- PDFViewer.GoToPage(pagenum - 1);
- CurrentPage = pagenum;
- txtCurrentPageNum.Text = pagenum.ToString();
- textBlock.Visibility = System.Windows.Visibility.Visible;
- txtCurrentPageNum.Background = new SolidColorBrush(Colors.Transparent);
- txtCurrentPageNum.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
- }
- }
- }
- }
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
- if (viewContentViewModel != null)
- {
- this.viewContentViewModel = viewContentViewModel;
- }
- var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
- if (pdfview != null)
- {
- //获取页面设置等信息
- this.PDFViewer = pdfview;
- PageCount = PDFViewer.Document.PageCount;
- CurrentPage = PDFViewer.CurrentIndex + 1;
- this.PDFViewer.InfoChanged += PDFViewer_InfoChanged;
- }
- }
- private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
- {
- if (e.Key == "SplieMode" || e.Key == "ViewMode")
- {
- return;
- }
- PageCount = PDFViewer.Document.PageCount;
- CurrentPage = PDFViewer.CurrentIndex + 1;
- }
- }
- }
|