using ComPDFKitViewer; using ComPDFKitViewer.PdfViewer; using Microsoft.Office.Interop.Excel; using PDF_Office.Helper; using PDF_Office.Model; using PDFSettings.Settings; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Threading.Tasks; using ContextMenu = System.Windows.Controls.ContextMenu; using MenuItem = System.Windows.Controls.MenuItem; using OpenFileDialog = Microsoft.Win32.OpenFileDialog; using Visibility = System.Windows.Visibility; namespace PDF_Office.ViewModels.PropertyPanel.ViewModular { internal class SplitScreenContentViewModel : BindableBase, INavigationAware { private ObservableCollection selectedFiles; public ObservableCollection SelectedFiles { get { return selectedFiles; } set { SetProperty(ref selectedFiles, value); } } private string splitViewRegionName; public string SplitViewRegionName { get { return splitViewRegionName; } set { SetProperty(ref splitViewRegionName, value); } } private string splitScreenPageRegionName; public string SplitScreenPageRegionName { get { return splitScreenPageRegionName; } set { SetProperty(ref splitScreenPageRegionName, value); } } private Visibility pDFViewerVisibility = Visibility.Collapsed; public Visibility PDFViewerVisibility { get { return pDFViewerVisibility; } set { SetProperty(ref pDFViewerVisibility, value); } } private Visibility stkpnlContentVisibility = Visibility.Visible; public Visibility StkpnlContentVisibility { get { return stkpnlContentVisibility; } set { SetProperty(ref stkpnlContentVisibility, value); } } /// /// 鼠标滚轮缩放的缩放值 /// private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 }; public CPDFViewer SplitScreenPDFViewer { get; set; } public CPDFViewer PDFViewer { get; set; } private IRegionManager region; private IDialogService dialogs; private CPDFViewer historyPDFViewer = null; public DelegateCommand AddFileCommand { get; set; } public DelegateCommand SelectedFilesCommand { get; set; } public SplitScreenContentViewModel(IRegionManager regionManager, IDialogService dialogService) { region = regionManager; dialogs = dialogService; //该区域名称 其他地方不需要调用,为减少一次打开多个文件的处理内容 //改为随机字符串,不计入RegionNames字典里 SplitViewRegionName = Guid.NewGuid().ToString(); SplitScreenPageRegionName = Guid.NewGuid().ToString(); SelectedFiles = new ObservableCollection(); //dicSelectedFiles = new Dictionary(); AddFileCommand = new DelegateCommand(AddFileEvent); SelectedFilesCommand = new DelegateCommand(SelectedFilesEvent); PDFViewerVisibility = Visibility.Visible; PDFViewerVisibility = Visibility.Collapsed; if (App.OpenedFileList != null) { if (App.OpenedFileList.Count > 0) { foreach (var item in App.OpenedFileList) { string filename = Path.GetFileName(item); SelectedFiles.Add(item); } } } } private void SelectedFilesEvent(object obj) { if (obj is ContextMenu menu) { foreach (var item in SelectedFiles) { MenuItem deleteMenu = new MenuItem(); deleteMenu.Header = Path.GetFileName(item); deleteMenu.Tag = item; deleteMenu.Click += SelectedFile_Click; menu.Items.Add(deleteMenu); } } } private async void SelectedFile_Click(object sender, System.Windows.RoutedEventArgs e) { if (sender is MenuItem menu) { await Task.Delay(3); OpenPDFViewer(menu.Tag.ToString()); } } private async void AddFileEvent(object obj) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = Properties.Resources.OpenDialogFilter; openFileDialog.Multiselect = true; if ((bool)openFileDialog.ShowDialog()) { await Task.Delay(3); bool flag = obj is bool; OpenPDFViewer(openFileDialog.FileNames[0]); } } private double zoomFactor = 1; private void OpenPDFViewer(string fileName) { bool isHavehistory = false; if (historyPDFViewer != null) { isHavehistory = true; zoomFactor = historyPDFViewer.ZoomFactor; CheckViewer(SplitViewRegionName, historyPDFViewer); } if (LoadFileFormPath(fileName)) { //CheckViewer(SplitViewRegionName, PDFViewer); region.AddToRegion(SplitViewRegionName, SplitScreenPDFViewer); //App.SplitScreenPDFViewer = SplitScreenPDFViewer; SplitScreenPDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler; PDFViewerVisibility = Visibility.Visible; StkpnlContentVisibility = Visibility.Collapsed; OpenPageContent(); if (isHavehistory) { SplitScreenPDFViewer.Zoom(zoomFactor); } SetPDFViewer(); } //ToolMethod.SetFileThumbImg(openFileDialog.FileNames[0]); } /// /// 设置视图 /// private void SetPDFViewer() { if (SplitScreenPDFViewer == null) { return; } SplitScreenPDFViewer.ChangeViewMode(PDFViewer.ModeView); if (PDFViewer.GetDrawMode() != DrawModes.Draw_Mode_Custom) { SplitScreenPDFViewer.SetDrawMode(PDFViewer.GetDrawMode()); } else { OpenFileInfo info = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath); var color = info.LastFillBrushColor; var color1 = color.ToString().Substring(1, 8); UInt32 c = Convert.ToUInt32(color1, 16); SplitScreenPDFViewer.SetDrawMode(PDFViewer.GetDrawMode(), c); } } private void OpenPageContent() { System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new System.Action(() => { //CheckViewer(SplitScreenPageRegionName, "SplitScreenPageContent"); NavigationParameters parameters = new NavigationParameters(); parameters.Add(ParameterNames.PDFViewer, SplitScreenPDFViewer); region.RequestNavigate(SplitScreenPageRegionName, "PageContent", parameters); })); } private void PdfViewer_MouseWheelZoomHandler(object sender, bool e) { double newZoom = CheckZoomLevel(SplitScreenPDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e); SplitScreenPDFViewer.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; } /// /// 检查视图,删掉之前的 /// /// /// private void CheckViewer(string regionName, object viewer) { if (region.Regions.ContainsRegionWithName(regionName)) { if (region.Regions[regionName].Views.Contains(viewer)) { var contentRegion = region.Regions[regionName]; contentRegion.Remove(viewer); } } } /// /// 从文件路径创建PDFViewer对象,已包含文档解密逻辑 /// /// /// private bool LoadFileFormPath(string path) { SplitScreenPDFViewer = new CPDFViewer(); SplitScreenPDFViewer.InitDocument(path); if (SplitScreenPDFViewer.Document == null) { //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_OpenFileFailedWarning")); return false; } else { if (SplitScreenPDFViewer.Document.IsLocked) { DialogParameters value = new DialogParameters(); value.Add(ParameterNames.PDFDocument, SplitScreenPDFViewer.Document); dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e => { if (e.Result == ButtonResult.OK) { if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue(ParameterNames.PassWord) != null) { SplitScreenPDFViewer.Tag = e.Parameters.GetValue(ParameterNames.PassWord).ToString(); } } }); if (SplitScreenPDFViewer.Document.IsLocked) { //未成功解密文档时,释放Document对象,返回 SplitScreenPDFViewer.Document.Release(); return false; } } } SplitScreenPDFViewer.Load(); historyPDFViewer = SplitScreenPDFViewer; return true; } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } public void OnNavigatedTo(NavigationContext navigationContext) { if (navigationContext.Parameters[ParameterNames.PDFViewer] is CPDFViewer cPDFViewer) { PDFViewer = cPDFViewer; PDFViewer.InfoChanged += PDFViewer_InfoChanged; } } private void PDFViewer_InfoChanged(object sender, KeyValuePair e) { if (e.Key == "ViewMode") { ViewMode ViewMode = (ViewMode)e.Value; SetPDFViewer(); } } } }