123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- 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<string> selectedFiles;
- public ObservableCollection<string> 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);
- }
- }
- /// <summary>
- /// 鼠标滚轮缩放的缩放值
- /// </summary>
- 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<object> AddFileCommand { get; set; }
- public DelegateCommand<object> 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<string>();
- //dicSelectedFiles = new Dictionary<string, string>();
- AddFileCommand = new DelegateCommand<object>(AddFileEvent);
- SelectedFilesCommand = new DelegateCommand<object>(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]);
- }
- /// <summary>
- /// 设置视图
- /// </summary>
- 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;
- }
- /// <summary>
- /// 检查视图,删掉之前的
- /// </summary>
- /// <param name="regionName"></param>
- /// <param name="viewer"></param>
- 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);
- }
- }
- }
- /// <summary>
- /// 从文件路径创建PDFViewer对象,已包含文档解密逻辑
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- 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<string>(ParameterNames.PassWord) != null)
- {
- SplitScreenPDFViewer.Tag = e.Parameters.GetValue<string>(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<string, object> e)
- {
- if (e.Key == "ViewMode")
- {
- ViewMode ViewMode = (ViewMode)e.Value;
- SetPDFViewer();
- }
- }
- }
- }
|