using ComPDFKitViewer.PdfViewer; using Dropbox.Api.Sharing; using PDF_Master.Helper; using PDF_Master.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; using Button = System.Windows.Controls.Button; using System.Windows.Controls.Primitives; using Microsoft.Office.Interop.Word; using System.Diagnostics; namespace PDF_Master.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); } } private bool CanNextPageExcute() { if (PDFViewer != null) { if (PDFViewer.CurrentIndex >= PDFViewer.Document.PageCount - 1) return false; else return true; } return false; } private bool CanPrePageExcute() { if (PDFViewer != null) { if (PDFViewer.CurrentIndex <= 0) return false; else return true; } return false; } private Button btnPrePage = null; private Button btnNextPage = null; private RepeatButton btnZoomOut = null; private RepeatButton btnZoomIn = null; 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 DelegateCommand LoadedCommand { 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); LoadedCommand = new DelegateCommand(Loaded); } /// /// 页面加载 /// /// private void Loaded(object obj) { if (obj is CompositeCommandParameter composite) { if (composite.Parameter is object[] array) { btnPrePage = array[0] as Button; btnNextPage = array[1] as Button; btnZoomOut = array[2] as RepeatButton; btnZoomIn = array[3] as RepeatButton; ChangeBtnZoom(); ChangePageBtnState(); } } } /// /// 下一页 /// /// private void NextPageEvent() { if (PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Double || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.DoubleContinuous || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Book || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.BookContinuous) { if (((int)PDFViewer.Mode % 2) == 0) { PDFViewer.GoToPage(PDFViewer.CurrentIndex + 2); } } else { PDFViewer.GoToPage(PDFViewer.CurrentIndex + 1); } } /// /// 上一页 /// /// private void PrePageEvent() { if (PDFViewer.RenderPageStart == PDFViewer.CurrentIndex) { PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1); } else { if ((PDFViewer.RenderPageStart - 1) > 0) { PDFViewer.GoToPage(PDFViewer.RenderPageStart - 1); } else { PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1); } } Trace.WriteLine("当前" + PDFViewer.CurrentIndex); Trace.WriteLine("结束" + PDFViewer.RenderPageEnd); Trace.WriteLine("开始" + PDFViewer.RenderPageStart); } /// /// 放大 /// /// private void PageZoomInEvent(object obj) { if (PDFViewer != null) { double zoom = SetPageZoomFactor(true); PDFViewer.Zoom(zoom / 100); ChangeBtnZoom(); } } /// /// 设置缩放因子 /// /// /// private double SetPageZoomFactor(bool flag) { double zoom = PDFViewer.ZoomFactor * 100; if (flag) { if (zoom >= 25 && zoom <= 300) { zoom = zoom + 25; } else if (zoom >= 301 && zoom <= 1000) { zoom = zoom + 40; } else if (zoom >= 1001 && zoom <= 10000) { zoom = zoom + 100; } } else { if (zoom >= 25 && zoom <= 300) { zoom = zoom - 25; } else if (zoom >= 301 && zoom <= 1000) { zoom = zoom - 40; } else if (zoom >= 1001 && zoom <= 10000) { zoom = zoom - 100; } } if (zoom < 25) { zoom = 25; } if (zoom > 10000) { zoom = 10000; } return zoom; } /// /// 缩小 /// /// private void PageZoomOutEvent(object obj) { if (PDFViewer != null) { double zoom = SetPageZoomFactor(false); PDFViewer.Zoom(zoom / 100); ChangeBtnZoom(); } } /// /// 输入限制 /// /// 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; pagenum = PDFViewer.CurrentIndex+1; } PDFViewer.GoToPage(pagenum - 1); CurrentPage = pagenum; textBlock.Visibility = System.Windows.Visibility.Visible; txtCurrentPageNum.Text = pagenum.ToString(); 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) { if (navigationContext.Parameters[ParameterNames.ViewContentViewModel] is ViewContentViewModel viewContentViewModel) { this.viewContentViewModel = viewContentViewModel; } if (navigationContext.Parameters[ParameterNames.PDFViewer] is CPDFViewer pdfview) { //获取页面设置等信息 this.PDFViewer = pdfview; if (PDFViewer != null) { 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; } if (e.Key == "PageNum") { if (e.Value is ComPDFKitViewer.RenderData data) { CurrentPage = data.PageIndex; } PageCount = PDFViewer.Document.PageCount; ChangeBtnZoom(); ChangePageBtnState(); } } /// /// 改变缩放按钮 /// private void ChangeBtnZoom() { if (PDFViewer == null) { return; } if (btnZoomOut == null && btnZoomIn == null) { return; } if (PDFViewer.ZoomFactor * 100 <= 25) { btnZoomOut.IsEnabled = false; btnZoomOut.Opacity = 0.5; btnZoomIn.IsEnabled = true; btnZoomIn.Opacity = 1; } else if (PDFViewer.ZoomFactor * 100 >= 25 && PDFViewer.ZoomFactor * 100 <= 10000) { btnZoomOut.Opacity = 1; btnZoomOut.IsEnabled = true; btnZoomIn.IsEnabled = true; btnZoomIn.Opacity = 1; } else { btnZoomIn.IsEnabled = false; btnZoomIn.Opacity = 0.5; btnZoomOut.IsEnabled = true; btnZoomOut.Opacity = 1; } } /// /// 改变上一页、下一页按钮 /// private void ChangePageBtnState() { if (PDFViewer == null) { return; } if (btnPrePage == null && btnNextPage == null) { return; } if (PDFViewer.CurrentIndex + 1 <= 1) { btnPrePage.IsEnabled = false; btnPrePage.Opacity = 0.5; } else { btnPrePage.IsEnabled = true; btnPrePage.Opacity = 1; } if (PDFViewer.CurrentIndex + 1 >= PDFViewer.Document.PageCount) { btnNextPage.IsEnabled = false; btnNextPage.Opacity = 0.5; } else { btnNextPage.IsEnabled = true; btnNextPage.Opacity = 1; } } } }