123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- using ComPDFKitViewer;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Office.Interop.Word;
- using PDF_Master.Model;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- namespace PDF_Master.ViewModels.PropertyPanel.ViewModular
- {
- internal class PageContentViewModel : BindableBase, INavigationAware
- {
- private IRegionManager region;
- private ViewContentViewModel viewContentViewModel { get; set; }
- private IDialogService dialogs;
- public CPDFViewer PDFViewer { get; set; }
- 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 void GoPrePage()
- {
- if (PDFViewer != null)
- {
- 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);
- }
- }
- }
- }
- 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.ModeView > 2))
- {
- 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 int selectedIndex;
- public int SelectedIndex
- {
- get { return selectedIndex; }
- set
- {
- SetProperty(ref selectedIndex, value);
- }
- }
- private double currenZoom;
- public double CurrentZoom
- {
- get { return currenZoom; }
- set { SetProperty(ref currenZoom, value); }
- }
- public DelegateCommand<object> PageTextKeyDownCommand { get; set; }
- public DelegateCommand<object> PageTextPreviewKeyDownCommand { get; set; }
- public DelegateCommand<object> PageTextFocusableChangedCommand { get; set; }
- public DelegateCommand<object> SelectionChangedCommand { get; set; }
- public DelegateCommand FirstPageCommand { get; set; }
- public DelegateCommand LastPageCommand { get; set; }
- public DelegateCommand PrePageCommand { get; set; }
- public DelegateCommand NextPageCommand { get; set; }
- public PageContentViewModel(IRegionManager regionManager, IDialogService dialogService)
- {
- region = regionManager;
- dialogs = dialogService;
- 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);
- PageTextKeyDownCommand = new DelegateCommand<object>(PageNumText_KeyDown);
- PageTextPreviewKeyDownCommand = new DelegateCommand<object>(PageNumText_PreviewKeyDown);
- PageTextFocusableChangedCommand = new DelegateCommand<object>(FocusableChanged);
- SelectionChangedCommand = new DelegateCommand<object>(SelectionChangedEvent);
- }
- 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<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 PageNumText_KeyDown(object e)
- {
- if (!(e is KeyEventArgs))
- {
- return;
- }
- //回车提交输入
- if ((e as KeyEventArgs).Key == System.Windows.Input.Key.Enter)
- {
- int pagenum = 0;
- var textBox = ((e as RoutedEventArgs).OriginalSource as TextBox);
- string text = 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;
- textBox.Text = PDFViewer.Document.PageCount.ToString();
- return;
- }
- if (pagenum < 1)
- {
- pagenum = 1;
- }
- else if (pagenum > PDFViewer.Document.PageCount)
- {
- //pagenum = (PDFViewer.CurrentIndex + 1);
- CurrentPage = PDFViewer.Document.PageCount;
- textBox.Text = "";
- return;
- }
- PDFViewer.GoToPage(pagenum - 1);
- CurrentPage = pagenum;
- textBox.Text = pagenum.ToString();
- }
- }
- private void FocusableChanged(object e)
- {
- int pagenum = 0;
- if (!(e != null && e is RoutedEventArgs)) { return; }
- var textBox = ((e as RoutedEventArgs).OriginalSource as TextBox);
- string text = 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.CurrentIndex + 1);
- textBox.Text = (PDFViewer.CurrentIndex + 1).ToString();
- return;
- }
- if (pagenum < 1)
- {
- pagenum = (PDFViewer.CurrentIndex + 1);
- }
- else if (pagenum > PDFViewer.Document.PageCount)
- {
- pagenum = (PDFViewer.CurrentIndex + 1);
- }
- PDFViewer.GoToPage(pagenum - 1);
- CurrentPage = pagenum;
- textBox.Text = pagenum.ToString();
- }
- 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 viewer)
- {
- //PDFViewer = viewer;
- //PageCount = PDFViewer.Document.PageCount;
- //CurrentPage = PDFViewer.CurrentIndex + 1;
- //CurrentZoom = PDFViewer.ZoomFactor * 100;
- //this.PDFViewer.InfoChanged += PDFViewer_InfoChanged;
- //获取页面设置等信息
- this.PDFViewer = viewer;
- if (PDFViewer != null)
- {
- PageCount = PDFViewer.Document.PageCount;
- //避免加密文档,刚进入文档时抛异常
- if (PageCount > 0)
- {
- CurrentPage = PDFViewer.CurrentIndex + 1;
- }
- CurrentZoom = PDFViewer.ZoomFactor * 100;
- this.PDFViewer.InfoChanged += PDFViewer_InfoChanged;
- PDFViewer.SetMouseMode(MouseModes.PanTool);
- var annotArgs = new SelectToolArgs();
- PDFViewer.SetToolParam(annotArgs);
- }
- }
- }
- private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
- {
- if (e.Key == "PageNum")
- {
- RenderData renderData = e.Value as RenderData;
- if (renderData != null)
- {
- CurrentPage = renderData.PageIndex;
- PageCount = PDFViewer.Document.PageCount;
- }
- }
- if (e.Key == "SplieMode" || e.Key == "ViewMode")
- {
- return;
- }
- if (e.Key == "Zoom")
- {
- CurrentZoom = (double)e.Value * 100;
- SelectedIndex = -1;
- }
- }
- }
- }
|