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 KeyDownCommand { get; set; } public DelegateCommand PreviewKeyDownCommand { get; set; } public DelegateCommand ZoomOutCommand { get; set; } public DelegateCommand ZoomInCommand { get; set; } public DelegateCommand PrePageCommand { get; set; } public DelegateCommand NextPageCommand { get; set; } public ReadModeContentViewModel() { KeyDownCommand = new DelegateCommand(KeyDownEvent); PreviewKeyDownCommand = new DelegateCommand(PreviewKeyDownEvent); ZoomOutCommand = new DelegateCommand(PageZoomOutEvent); ZoomInCommand = new DelegateCommand(PageZoomInEvent); PrePageCommand = new DelegateCommand(PrePageEvent); NextPageCommand = new DelegateCommand(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 NumberKeys = new List() { 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 e) { if (e.Key == "SplieMode" || e.Key == "ViewMode") { return; } PageCount = PDFViewer.Document.PageCount; CurrentPage = PDFViewer.CurrentIndex + 1; } } }