123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- 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<object> KeyDownCommand { get; set; }
- public DelegateCommand<object> PreviewKeyDownCommand { get; set; }
- public DelegateCommand<object> ZoomOutCommand { get; set; }
- public DelegateCommand<object> ZoomInCommand { get; set; }
- public DelegateCommand PrePageCommand { get; set; }
- public DelegateCommand NextPageCommand { get; set; }
- public DelegateCommand<object> LoadedCommand { 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(PrePageEvent);
- NextPageCommand = new DelegateCommand(NextPageEvent);
- LoadedCommand = new DelegateCommand<object>(Loaded);
- }
- /// <summary>
- /// 页面加载
- /// </summary>
- /// <param name="obj"></param>
- 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();
- }
- }
- }
- /// <summary>
- /// 下一页
- /// </summary>
- /// <param name="obj"></param>
- 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);
- }
- }
- /// <summary>
- /// 上一页
- /// </summary>
- /// <param name="obj"></param>
- 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);
- }
- /// <summary>
- /// 放大
- /// </summary>
- /// <param name="obj"></param>
- private void PageZoomInEvent(object obj)
- {
- if (PDFViewer != null)
- {
- double zoom = SetPageZoomFactor(true);
- PDFViewer.Zoom(zoom / 100);
- ChangeBtnZoom();
- }
- }
- /// <summary>
- /// 设置缩放因子
- /// </summary>
- /// <param name="flag"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 缩小
- /// </summary>
- /// <param name="obj"></param>
- private void PageZoomOutEvent(object obj)
- {
- if (PDFViewer != null)
- {
- double zoom = SetPageZoomFactor(false);
- PDFViewer.Zoom(zoom / 100);
- ChangeBtnZoom();
- }
- }
- /// <summary>
- /// 输入限制
- /// </summary>
- /// <param name="obj"></param>
- 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;
- }
- }
- }
- /// <summary>
- /// 回车键保存
- /// </summary>
- /// <param name="obj"></param>
- 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;
- }
- }
- }
- /// <summary>
- /// 滚动的时候,参数变化
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> 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();
- }
- }
- /// <summary>
- /// 改变缩放按钮
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 改变上一页、下一页按钮
- /// </summary>
- 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;
- }
- }
- }
- }
|