using Prism.Mvvm; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ComPDFKitViewer.PdfViewer; using Prism.Commands; using Prism.Regions; using System.Windows; using PDF_Office.Views; using ComPDFKitViewer; using System.Windows.Input; using System.Windows.Controls; using Prism.Services.Dialogs; using PDF_Office.CustomControl; using PDF_Office.Model; namespace PDF_Office.ViewModels { public class BottomToolContentViewModel : BindableBase { public DelegateCommand Load { get; set; } public DelegateCommand FirstPageCommand { get; set; } public DelegateCommand LastPageCommand { get; set; } public DelegateCommand PrePageCommand { get; set; } public DelegateCommand NextPageCommand { get; set; } public DelegateCommand MyPropertyCommand { get; set; } public DelegateCommand FullScreenCommand { get; set; } public DelegateCommand ReadModeCommand { get; set; } public DelegateCommand OpenViewCommand { get; set; } public DelegateCommand ZoomInCommand { get; set; } public DelegateCommand ZoomOutCommand { get; set; } public DelegateCommand SetViewModeCommand { get; set; } public DelegateCommand OpenFullCommand { get; set; } public DelegateCommand PageTextKeyDownCommand { get; set; } public DelegateCommand PageTextPreviewKeyDownCommand { get; set; } public DelegateCommand SelectionChangedCommand { get; set; } private bool isSingleView; public bool IsSingleView { get { return isSingleView; } set { SetProperty(ref isSingleView, value); if (value) { SetModeView(); } } } private bool isContinue; public bool IsContinue { get { return isContinue; } set { SetProperty(ref isContinue, value); SetModeView(); } } private bool isDoubleView; public bool IsDoubleView { get { return isDoubleView; } set { SetProperty(ref isDoubleView, value); if (value) { SetModeView(); } } } private bool isBookMode; public bool IsBookMode { get { return isBookMode; } set { SetProperty(ref isBookMode, value); if (value) { SetModeView(); } } } private int pageCount; public int PageCount { get { return pageCount; } set { SetProperty(ref pageCount, value); } } private int currentPage; public int CurrentPage { get { return currentPage; } set { SetProperty(ref currentPage, value); } } private double currenZoom; public double CurrentZoom { get { return currenZoom; } set { SetProperty(ref currenZoom, value); } } private int selectedIndex; public int SelectedIndex { get { return selectedIndex; } set { SetProperty(ref selectedIndex, value); } } public CPDFViewer PDFViewer { get; set; } private IRegionManager region; private IDialogService dialogs; public BottomToolContentViewModel(IRegionManager regionManager,IDialogService dialogService) { region = regionManager; dialogs = dialogService; Load = new DelegateCommand(LoadControl); PrePageCommand = new DelegateCommand(GoPrePage, CanPrePageExcute).ObservesProperty(() => CurrentPage); NextPageCommand = new DelegateCommand(GoNextPage, CanNextPageExcute).ObservesProperty(() => CurrentPage); FirstPageCommand = new DelegateCommand(GoFirstPage, CanPrePageExcute).ObservesProperty(() => CurrentPage); LastPageCommand = new DelegateCommand(GoLastPage, CanNextPageExcute).ObservesProperty(() => CurrentPage); ZoomInCommand = new DelegateCommand(ZoomIn, CanZoomIn).ObservesProperty(()=>CurrentZoom); ZoomOutCommand = new DelegateCommand(ZoomOut, CanZoomOut).ObservesProperty(()=>CurrentZoom); SetViewModeCommand = new DelegateCommand(SetModeView); PageTextKeyDownCommand = new DelegateCommand(PageNumText_KeyDown); PageTextPreviewKeyDownCommand = new DelegateCommand(PageNumText_PreviewKeyDown); SelectionChangedCommand = new DelegateCommand(SelectionChangedEvent); OpenFullCommand = new DelegateCommand(OpenFullWindow); } /// /// 进入全屏模式 /// private void OpenFullWindow() { DialogParameters parameters = new DialogParameters(); //因为全屏模式可能需要设置特定的页面模式,所以只传文件路径,新建一个PDFview对象 parameters.Add(ParameterNames.FilePath,PDFViewer.Document.FilePath); parameters.Add(ParameterNames.PassWord,PDFViewer.Tag==null?"":PDFViewer.Tag.ToString()); dialogs.ShowDialog(DialogNames.FullScreenDialog,parameters,e=> { if(e.Result==ButtonResult.Cancel) { //TODO:弹窗提示打开全屏模式失败 MessageBox.Show("Error"); } }); } private void SelectionChangedEvent(object e) { var args = e as SelectionChangedEventArgs; if(args!=null) { if(SelectedIndex<=2) { switch (SelectedIndex) { case 0: PDFViewer.ChangeFitMode(FitMode.FitSize); break; case 1: PDFViewer.ChangeFitMode(FitMode.FitWidth); break; case 2: PDFViewer.ChangeFitMode(FitMode.FitHeight); break; default: break; } } else { if (args.AddedItems[0] is ComboBoxItem) { if ((args.AddedItems[0] as ComboBoxItem).Tag != null) { PDFViewer.Zoom(Convert.ToDouble((args.AddedItems[0] as ComboBoxItem).Tag.ToString()) / 100.0); } } } } } private void PageNumText_PreviewKeyDown(object e) { var args = e 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 PageNumText_KeyDown(object e) { if (!(e is KeyEventArgs)) return; //回车提交输入 if ((e as KeyEventArgs).Key == System.Windows.Input.Key.Enter) { int pagenum = 0; string text = ((e as KeyEventArgs).OriginalSource as TextBox).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; ((e as KeyEventArgs).OriginalSource as TextBox).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; ((e as KeyEventArgs).OriginalSource as TextBox).Text = pagenum.ToString(); } } private void GoPrePage() { if (PDFViewer != null) { PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1); } } private bool CanPrePageExcute() { if (PDFViewer != null) { if (PDFViewer.CurrentIndex <= 0) return false; else return true; } return false; } private void GoNextPage() { if (PDFViewer != null) { PDFViewer.GoToPage(PDFViewer.CurrentIndex + 1); if(((int)PDFViewer.Mode%2)==0) { PDFViewer.GoToPage(PDFViewer.CurrentIndex + 2); } } } private bool CanNextPageExcute() { if (PDFViewer != null) { if (PDFViewer.CurrentIndex >= PDFViewer.Document.PageCount - 1) return false; else return true; } return false; } private void GoFirstPage() { if (PDFViewer != null) { PDFViewer.GoToPage(0); } } private void GoLastPage() { if (PDFViewer != null) { PDFViewer.GoToPage(PDFViewer.Document.PageCount - 1); } } private void ZoomIn() { var zoom = PDFViewer.ZoomFactor*100; if (zoom >= 1 && zoom <= 100) { PDFViewer.Zoom((zoom + 5) / 100.0); } else if (zoom <= 500) { PDFViewer.Zoom((zoom + 50) / 100.0); } else { PDFViewer.Zoom((zoom + 100) / 100.0); } } private void ZoomOut() { var zoom = PDFViewer.ZoomFactor*100; if (zoom >= 1 && zoom <= 100) { PDFViewer.Zoom((zoom - 5)/100.0); } else if (zoom <= 500) { PDFViewer.Zoom((zoom - 50) / 100.0); } else { PDFViewer.Zoom((zoom - 100) / 100.0); } } private bool CanZoomIn() { if (PDFViewer != null) { if (PDFViewer.ZoomFactor*100 >=1000) return false; else return true; } return false; } private bool CanZoomOut() { if (PDFViewer != null) { if (PDFViewer.ZoomFactor*100 <=1) return false; else return true; } return false; } private void LoadControl() { if (App.mainWindowViewModel.CurrentPDFViewer != null) { PDFViewer = App.mainWindowViewModel.CurrentPDFViewer; if (PDFViewer != null) { //获取页面设置等信息 PageCount = PDFViewer.Document.PageCount; GetModeView(PDFViewer.ModeView); CurrentPage = PDFViewer.CurrentIndex+1; CurrentZoom = PDFViewer.ZoomFactor*100; PDFViewer.InfoChanged += PDFViewer_InfoChanged; } } } private void PDFViewer_InfoChanged(object sender, KeyValuePair e) { if (e.Key == "PageNum") { CurrentPage = (int)e.Value; } if (e.Key == "ViewMode") { GetModeView((ViewMode)e.Value); } if(e.Key=="Zoom") { CurrentZoom = (double)e.Value*100; SelectedIndex = -1; } } private void GetModeView(ViewMode mode) { if ((int)mode % 2 == 0) { if(!IsContinue) { IsContinue = true; } } else { if (IsContinue) { IsContinue = false; } } if ((int)mode <= 2) { if(!isSingleView) { IsSingleView = true; } } else if ((int)mode <= 4) { if(!isDoubleView) { IsDoubleView = true; } } else { if(!isBookMode) { IsBookMode = true; } } } private void SetModeView() { if (PDFViewer != null) { if (IsContinue) { if (IsSingleView) { PDFViewer.ChangeViewMode(ViewMode.SingleContinuous); } else if (IsDoubleView) { PDFViewer.ChangeViewMode(ViewMode.DoubleContinuous); } else { PDFViewer.ChangeViewMode(ViewMode.BookContinuous); } } else { if (IsSingleView) { PDFViewer.ChangeViewMode(ViewMode.Single); } else if (IsDoubleView) { PDFViewer.ChangeViewMode(ViewMode.Double); } else { PDFViewer.ChangeViewMode(ViewMode.Book); } } } } } }