using ComPDFKitViewer.PdfViewer; using PDF_Office.Model; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace PDF_Office.ViewModels.Dialog { public class FullScreenWindowViewModel : BindableBase, IDialogAware { public string Title => ""; private Visibility showTip = Visibility.Collapsed; public Visibility ShowTip { get { return showTip; } set { SetProperty(ref showTip, value); } } private CPDFViewer PDFViewer; public DelegateCommand ExitCommand { get; set; } public DelegateCommand LoadCommand { get; set; } public event Action RequestClose; public string RegionName { get; set; } /// /// 鼠标滚轮缩放的缩放值 /// private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 }; public FullScreenWindowViewModel() { ExitCommand = new DelegateCommand(Exit); LoadCommand = new DelegateCommand(Load); RegionName = Guid.NewGuid().ToString(); } private void Exit() { RequestClose.Invoke(new DialogResult(ButtonResult.OK)); } private void Load(object grid) { var Grid = grid as Grid; if(Grid!=null&&PDFViewer!=null) { Grid.Children.Insert(0, PDFViewer); ShowTip = Visibility.Visible; } } public bool CanCloseDialog() { return true; } public void OnDialogClosed() { if(PDFViewer!=null&&PDFViewer.Document!=null) { PDFViewer.Document.Release(); } } public void OnDialogOpened(IDialogParameters parameters) { string filepath; string password; parameters.TryGetValue(ParameterNames.FilePath,out filepath); parameters.TryGetValue(ParameterNames.PassWord,out password); if (!string.IsNullOrEmpty(filepath)) { PDFViewer = new CPDFViewer(); PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler; PDFViewer.InitDocument(filepath); if (PDFViewer.Document == null) { RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); } if (PDFViewer.Document.IsLocked && !string.IsNullOrEmpty(password)) { PDFViewer.Document.UnlockWithPassword(password); if (PDFViewer.Document.IsLocked) { var dialogservice = Prism.Ioc.ContainerLocator.Container.Resolve(typeof(IDialogService)) as IDialogService; DialogParameters pairs = new DialogParameters(); pairs.Add(ParameterNames.PDFDocument,PDFViewer.Document); dialogservice.ShowDialog(DialogNames.VerifyPassWordDialog,pairs,e=> { if(e.Result!=ButtonResult.OK) { RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); } }); } } PDFViewer.Load(); PDFViewer.SetMouseMode(MouseModes.Default); } } #region PDFViewer缩放事件 public void PdfViewer_MouseWheelZoomHandler(object sender, bool e) { double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e); PDFViewer.Zoom(newZoom); } private double CheckZoomLevel(double zoom, bool IsGrowth) { double standardZoom = 100; if (zoom <= 0.01) { return 0.01; } if (zoom >= 10) { return 10; } zoom *= 100; for (int i = 0; i < zoomLevel.Length - 1; i++) { if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth) { standardZoom = zoomLevel[i + 1]; break; } if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth) { standardZoom = zoomLevel[i]; break; } } return standardZoom / 100; } #endregion } }