123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837 |
- 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;
- using PDF_Office.ViewModels.BOTA;
- using PDF_Office.Views.BOTA;
- using PDF_Office.Views.PropertyPanel.AnnotPanel;
- using PDF_Office.Views.Tools;
- using PDFSettings.Settings;
- using PDF_Office.Helper;
- using System.IO;
- using ImTools;
- using PDF_Office.Views.PropertyPanel.ViewModular;
- namespace PDF_Office.ViewModels
- {
- public class BottomToolContentViewModel : BindableBase, INavigationAware
- {
- #region 文案
- private string T_actual;
- public string T_Actual
- {
- get { return T_actual; }
- set
- {
- SetProperty(ref T_actual, value);
- }
- }
- private string T_fitWidth;
- public string T_FitWidth
- {
- get { return T_fitWidth; }
- set
- {
- SetProperty(ref T_fitWidth, value);
- }
- }
- private string T_fitPage;
- public string T_FitPage
- {
- get { return T_fitPage; }
- set
- {
- SetProperty(ref T_fitPage, value);
- }
- }
- private void InitString()
- {
- T_Actual = App.MainPageLoader.GetString("Zoom_Actual");
- T_FitWidth = App.MainPageLoader.GetString("Zoom_FitWidth");
- T_FitPage = App.MainPageLoader.GetString("Zoom_FitPage");
- }
- #endregion 文案
- 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<object> PageTextKeyDownCommand { get; set; }
- public DelegateCommand<object> PageTextPreviewKeyDownCommand { get; set; }
- public DelegateCommand<object> SelectionChangedCommand { get; set; }
- public DelegateCommand<object> ShowViewModularCommand { get; set; }
- /// <summary>
- /// 上一个视图
- /// </summary>
- public DelegateCommand PreViewCommand { get; set; }
- /// <summary>
- /// 下一个视图
- /// </summary>
- public DelegateCommand NextViewCommnad { get; set; }
- private bool isShowViewModular = false;
- /// <summary>
- /// 属性栏是否展开
- /// </summary>
- public bool IsShowViewModular
- {
- get { return isShowViewModular; }
- set
- {
- SetProperty(ref isShowViewModular, value);
- }
- }
- 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);
- }
- }
- private bool rightPanelEnable = true;
- public bool RightPanelEnable
- {
- get { return rightPanelEnable; }
- set
- {
- SetProperty(ref rightPanelEnable, value);
- }
- }
- private bool isPreEnable = false;
- /// <summary>
- /// 前进是否启用
- /// </summary>
- public bool IsPreEnable
- {
- get { return isPreEnable; }
- set
- {
- SetProperty(ref isPreEnable, value);
- }
- }
- private bool isNextEnable = false;
- public bool IsNextEnable
- {
- get { return isNextEnable; }
- set
- {
- SetProperty(ref isNextEnable, value);
- }
- }
- private Stack<int> UndoStack = new Stack<int>();
- private Stack<int> RedoStack = new Stack<int>();
- public CPDFViewer PDFViewer { get; set; }
- public ViewContentViewModel ViewContentViewModel { get; set; }
- public OpenFileInfo OpenFileInfo = null;
- /// <summary>
- /// 来自undo redo方法的跳页 不计入栈
- /// </summary>
- private bool isFromMethod = false;
- /// <summary>
- /// 记录上一个页面索引值
- /// </summary>
- private int oldPageIndex = 0;
- private IRegionManager region;
- private IDialogService dialogs;
- public BottomToolContentViewModel(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);
- ZoomInCommand = new DelegateCommand(ZoomIn, CanZoomIn).ObservesProperty(() => CurrentZoom);
- ZoomOutCommand = new DelegateCommand(ZoomOut, CanZoomOut).ObservesProperty(() => CurrentZoom);
- SetViewModeCommand = new DelegateCommand(SetModeView);
- PageTextKeyDownCommand = new DelegateCommand<object>(PageNumText_KeyDown);
- PageTextPreviewKeyDownCommand = new DelegateCommand<object>(PageNumText_PreviewKeyDown);
- SelectionChangedCommand = new DelegateCommand<object>(SelectionChangedEvent);
- ShowViewModularCommand = new DelegateCommand<object>(ShowViewModularEvent);
- PreViewCommand = new DelegateCommand(undo);
- NextViewCommnad = new DelegateCommand(redo);
- InitString();
- }
- private void undo()
- {
- RedoStack.Push(PDFViewer.CurrentIndex);
- IsNextEnable = true;
- int index = UndoStack.Pop();
- PDFViewer.GoToPage(index);
- if (UndoStack.Count <= 0)
- {
- IsPreEnable = false;
- }
- isFromMethod = true;
- }
- private void redo()
- {
- App.Current.Dispatcher.Invoke(() =>
- {
- UndoStack.Push(PDFViewer.CurrentIndex);
- IsPreEnable = true;
- int index = RedoStack.Pop();
- PDFViewer.GoToPage(index);
- if (RedoStack.Count <= 0)
- {
- IsNextEnable = false;
- }
- });
- isFromMethod = true;
- }
- private void ShowViewModularEvent(object obj)
- {
- ViewContentViewModel.IsPropertyOpen = true;
- if (region.Regions.ContainsRegionWithName(ViewContentViewModel.PropertyRegionName))
- {
- var views = region.Regions[ViewContentViewModel.PropertyRegionName].Views;
- var item = views.FindFirst(p => p is ViewModularContent);
- if (item == null)
- {
- NavigationParameters param = new NavigationParameters();
- param.Add(ParameterNames.PDFViewer, PDFViewer);
- param.Add(ParameterNames.BottomToolContentViewModel, this);
- region.RequestNavigate(RegionNames.PropertyRegionName, "ViewModularContent", param);
- }
- else
- {
- if (item is ViewModularContent bOTAContent1)
- {
- var contentRegion = region.Regions[ViewContentViewModel.PropertyRegionName];
- contentRegion.Activate(bOTAContent1);
- }
- }
- }
- //if (ViewContentViewModel.IsPropertyOpen == false)
- //{
- // ViewContentViewModel.IsPropertyOpen = true;
- // NavigationParameters param = new NavigationParameters();
- // param.Add(ParameterNames.PDFViewer, PDFViewer);
- // param.Add(ParameterNames.BottomToolContentViewModel, this);
- // region.RequestNavigate(RegionNames.PropertyRegionName, "ViewModularContent", param);
- // //if (region.Regions.ContainsRegionWithName(ViewContentViewModel.PropertyRegionName))
- // //{
- // // var views = region.Regions[ViewContentViewModel.PropertyRegionName].Views;
- // // foreach (var item in views)
- // // {
- // // if (item is LinkAnnotProperty linkAnnotProperty)
- // // {
- // // var contentRegion = region.Regions[ViewContentViewModel.PropertyRegionName];
- // // contentRegion.Remove(item);
- // // break;
- // // }
- // // }
- // //}
- //}
- //else
- //{
- //}
- IsShowViewModular = ViewContentViewModel.IsPropertyOpen;
- }
- private void SelectionChangedEvent(object e)
- {
- var args = e as SelectionChangedEventArgs;
- if (args != null)
- {
- if (SelectedIndex <= 2)
- {
- switch (SelectedIndex)
- {
- case 0:
- PDFViewer.ChangeFitMode(FitMode.FitSize);
- OpenFileInfo.LastFitMode = FitMode.FitSize;
- break;
- case 1:
- PDFViewer.ChangeFitMode(FitMode.FitWidth);
- OpenFileInfo.LastFitMode = FitMode.FitWidth;
- break;
- case 2:
- PDFViewer.ChangeFitMode(FitMode.FitHeight);
- OpenFileInfo.LastFitMode = 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;
- 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.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 void ZoomIn()
- {
- double zoom = SetPageZoomFactor(true);
- PDFViewer.Zoom(zoom / 100);
- }
- private void ZoomOut()
- {
- double zoom = SetPageZoomFactor(false);
- PDFViewer.Zoom(zoom / 100);
- }
- 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 bool CanZoomIn()
- {
- if (PDFViewer != null)
- {
- if (PDFViewer.ZoomFactor * 100 >= 10000)
- return false;
- else
- return true;
- }
- return false;
- }
- private bool CanZoomOut()
- {
- if (PDFViewer != null)
- {
- if (PDFViewer.ZoomFactor * 100 <= 25)
- return false;
- else
- return true;
- }
- return false;
- }
- public 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 (!renderData.IsMouseWheel && !isFromMethod)
- {
- UndoStack.Push(oldPageIndex);
- RedoStack.Clear();
- IsNextEnable = false;
- IsPreEnable = true;
- }
- isFromMethod = false;
- }
- oldPageIndex = renderData.PageIndex - 1;
- }
- if (e.Key == "ViewMode")
- {
- GetModeView((ViewMode)e.Value);
- }
- if (e.Key == "Zoom")
- {
- CurrentZoom = (double)e.Value * 100;
- SelectedIndex = -1;
- }
- if (OpenFileInfo != null)
- {
- if (OpenFileInfo.LastFitMode != PDFViewer.ModeFit)
- {
- OpenFileInfo.LastFitMode = PDFViewer.ModeFit;
- }
- if (PDFViewer.ModeFit == FitMode.FitFree)
- {
- if (OpenFileInfo.LastZoom != PDFViewer.ZoomFactor)
- {
- OpenFileInfo.LastZoom = PDFViewer.ZoomFactor;
- }
- }
- if (OpenFileInfo.LastPageIndex != PDFViewer.CurrentIndex)
- {
- OpenFileInfo.LastPageIndex = PDFViewer.CurrentIndex;
- }
- SettingHelper.SetFileInfo(OpenFileInfo);
- }
- }
- private void ViewContentViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
- {
- if (e.PropertyName == "OCRContentVisible")
- {
- if (ViewContentViewModel.OCRContentVisible == Visibility.Visible)
- {
- RightPanelEnable = false;
- }
- else
- {
- RightPanelEnable = true;
- }
- }
- }
- 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;
- }
- }
- }
- public 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);
- }
- }
- }
- SaveMode(PDFViewer.ModeView);
- }
- private void SaveMode(object modeView)
- {
- OpenFileInfo fileInfo = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
- if (fileInfo != null)
- {
- if (modeView is SplitMode)
- {
- fileInfo.LastSplitMode = PDFViewer.Mode;
- }
- else if (modeView is ViewMode)
- {
- fileInfo.LastViewMode = PDFViewer.ModeView;
- }
- else if (modeView is bool)
- {
- fileInfo.LastPageSpace = (bool)modeView;
- }
- SettingHelper.SetFileInfo(fileInfo);
- }
- }
- #region Navigation
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
- if (viewContentViewModel != null)
- {
- this.ViewContentViewModel = viewContentViewModel;
- IsShowViewModular = viewContentViewModel.IsPropertyOpen;
- viewContentViewModel.PropertyChanged -= ViewContentViewModel_PropertyChanged;
- viewContentViewModel.PropertyChanged += ViewContentViewModel_PropertyChanged;
- }
- var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
- if (pdfview != null)
- {
- if (ViewContentViewModel.OpenFileInfo != null)
- {
- OpenFileInfo = ViewContentViewModel.OpenFileInfo;
- }
- //获取页面设置等信息
- PDFViewer = pdfview;
- oldPageIndex = pdfview.CurrentIndex;
- PageCount = PDFViewer.Document.PageCount;
- GetModeView(PDFViewer.ModeView);
- CurrentPage = PDFViewer.CurrentIndex + 1;
- CurrentZoom = PDFViewer.ZoomFactor * 100;
- PDFViewer.InfoChanged += PDFViewer_InfoChanged;
- }
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- }
- #endregion Navigation
- }
- }
|