SplitScreenContentViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Office.Interop.Excel;
  4. using PDF_Office.Helper;
  5. using PDF_Office.Model;
  6. using PDFSettings.Settings;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.IO;
  15. using System.Threading.Tasks;
  16. using ContextMenu = System.Windows.Controls.ContextMenu;
  17. using MenuItem = System.Windows.Controls.MenuItem;
  18. using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
  19. using Visibility = System.Windows.Visibility;
  20. namespace PDF_Office.ViewModels.PropertyPanel.ViewModular
  21. {
  22. internal class SplitScreenContentViewModel : BindableBase, INavigationAware
  23. {
  24. private ObservableCollection<string> selectedFiles;
  25. public ObservableCollection<string> SelectedFiles
  26. {
  27. get { return selectedFiles; }
  28. set
  29. {
  30. SetProperty(ref selectedFiles, value);
  31. }
  32. }
  33. private string splitViewRegionName;
  34. public string SplitViewRegionName
  35. {
  36. get { return splitViewRegionName; }
  37. set
  38. {
  39. SetProperty(ref splitViewRegionName, value);
  40. }
  41. }
  42. private string splitScreenPageRegionName;
  43. public string SplitScreenPageRegionName
  44. {
  45. get { return splitScreenPageRegionName; }
  46. set
  47. {
  48. SetProperty(ref splitScreenPageRegionName, value);
  49. }
  50. }
  51. private Visibility pDFViewerVisibility = Visibility.Collapsed;
  52. public Visibility PDFViewerVisibility
  53. {
  54. get { return pDFViewerVisibility; }
  55. set
  56. {
  57. SetProperty(ref pDFViewerVisibility, value);
  58. }
  59. }
  60. private Visibility stkpnlContentVisibility = Visibility.Visible;
  61. public Visibility StkpnlContentVisibility
  62. {
  63. get { return stkpnlContentVisibility; }
  64. set
  65. {
  66. SetProperty(ref stkpnlContentVisibility, value);
  67. }
  68. }
  69. /// <summary>
  70. /// 鼠标滚轮缩放的缩放值
  71. /// </summary>
  72. private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  73. public CPDFViewer SplitScreenPDFViewer { get; set; }
  74. public CPDFViewer PDFViewer { get; set; }
  75. private IRegionManager region;
  76. private IDialogService dialogs;
  77. private CPDFViewer historyPDFViewer = null;
  78. public DelegateCommand<object> AddFileCommand { get; set; }
  79. public DelegateCommand<object> SelectedFilesCommand { get; set; }
  80. public SplitScreenContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  81. {
  82. region = regionManager;
  83. dialogs = dialogService;
  84. //该区域名称 其他地方不需要调用,为减少一次打开多个文件的处理内容
  85. //改为随机字符串,不计入RegionNames字典里
  86. SplitViewRegionName = Guid.NewGuid().ToString();
  87. SplitScreenPageRegionName = Guid.NewGuid().ToString();
  88. SelectedFiles = new ObservableCollection<string>();
  89. //dicSelectedFiles = new Dictionary<string, string>();
  90. AddFileCommand = new DelegateCommand<object>(AddFileEvent);
  91. SelectedFilesCommand = new DelegateCommand<object>(SelectedFilesEvent);
  92. PDFViewerVisibility = Visibility.Visible;
  93. PDFViewerVisibility = Visibility.Collapsed;
  94. if (App.OpenedFileList != null)
  95. {
  96. if (App.OpenedFileList.Count > 0)
  97. {
  98. foreach (var item in App.OpenedFileList)
  99. {
  100. string filename = Path.GetFileName(item);
  101. SelectedFiles.Add(item);
  102. }
  103. }
  104. }
  105. }
  106. private void SelectedFilesEvent(object obj)
  107. {
  108. if (obj is ContextMenu menu)
  109. {
  110. foreach (var item in SelectedFiles)
  111. {
  112. MenuItem deleteMenu = new MenuItem();
  113. deleteMenu.Header = Path.GetFileName(item);
  114. deleteMenu.Tag = item;
  115. deleteMenu.Click += SelectedFile_Click;
  116. menu.Items.Add(deleteMenu);
  117. }
  118. }
  119. }
  120. private async void SelectedFile_Click(object sender, System.Windows.RoutedEventArgs e)
  121. {
  122. if (sender is MenuItem menu)
  123. {
  124. await Task.Delay(3);
  125. OpenPDFViewer(menu.Tag.ToString());
  126. }
  127. }
  128. private async void AddFileEvent(object obj)
  129. {
  130. OpenFileDialog openFileDialog = new OpenFileDialog();
  131. openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
  132. openFileDialog.Multiselect = true;
  133. if ((bool)openFileDialog.ShowDialog())
  134. {
  135. await Task.Delay(3);
  136. bool flag = obj is bool;
  137. OpenPDFViewer(openFileDialog.FileNames[0]);
  138. }
  139. }
  140. private double zoomFactor = 1;
  141. private void OpenPDFViewer(string fileName)
  142. {
  143. bool isHavehistory = false;
  144. if (historyPDFViewer != null)
  145. {
  146. isHavehistory = true;
  147. zoomFactor = historyPDFViewer.ZoomFactor;
  148. CheckViewer(SplitViewRegionName, historyPDFViewer);
  149. }
  150. if (LoadFileFormPath(fileName))
  151. {
  152. //CheckViewer(SplitViewRegionName, PDFViewer);
  153. region.AddToRegion(SplitViewRegionName, SplitScreenPDFViewer);
  154. //App.SplitScreenPDFViewer = SplitScreenPDFViewer;
  155. SplitScreenPDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  156. PDFViewerVisibility = Visibility.Visible;
  157. StkpnlContentVisibility = Visibility.Collapsed;
  158. OpenPageContent();
  159. if (isHavehistory)
  160. {
  161. SplitScreenPDFViewer.Zoom(zoomFactor);
  162. }
  163. SetPDFViewer();
  164. }
  165. //ToolMethod.SetFileThumbImg(openFileDialog.FileNames[0]);
  166. }
  167. /// <summary>
  168. /// 设置视图
  169. /// </summary>
  170. private void SetPDFViewer()
  171. {
  172. if (SplitScreenPDFViewer == null)
  173. {
  174. return;
  175. }
  176. SplitScreenPDFViewer.ChangeViewMode(PDFViewer.ModeView);
  177. if (PDFViewer.GetDrawMode() != DrawModes.Draw_Mode_Custom)
  178. {
  179. SplitScreenPDFViewer.SetDrawMode(PDFViewer.GetDrawMode());
  180. }
  181. else
  182. {
  183. OpenFileInfo info = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  184. var color = info.LastFillBrushColor;
  185. var color1 = color.ToString().Substring(1, 8);
  186. UInt32 c = Convert.ToUInt32(color1, 16);
  187. SplitScreenPDFViewer.SetDrawMode(PDFViewer.GetDrawMode(), c);
  188. }
  189. }
  190. private void OpenPageContent()
  191. {
  192. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new System.Action(() =>
  193. {
  194. //CheckViewer(SplitScreenPageRegionName, "SplitScreenPageContent");
  195. NavigationParameters parameters = new NavigationParameters();
  196. parameters.Add(ParameterNames.PDFViewer, SplitScreenPDFViewer);
  197. region.RequestNavigate(SplitScreenPageRegionName, "PageContent", parameters);
  198. }));
  199. }
  200. private void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  201. {
  202. double newZoom = CheckZoomLevel(SplitScreenPDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  203. SplitScreenPDFViewer.Zoom(newZoom);
  204. }
  205. private double CheckZoomLevel(double zoom, bool IsGrowth)
  206. {
  207. double standardZoom = 100;
  208. if (zoom <= 0.01)
  209. {
  210. return 0.01;
  211. }
  212. if (zoom >= 10)
  213. {
  214. return 10;
  215. }
  216. zoom *= 100;
  217. for (int i = 0; i < zoomLevel.Length - 1; i++)
  218. {
  219. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  220. {
  221. standardZoom = zoomLevel[i + 1];
  222. break;
  223. }
  224. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  225. {
  226. standardZoom = zoomLevel[i];
  227. break;
  228. }
  229. }
  230. return standardZoom / 100;
  231. }
  232. /// <summary>
  233. /// 检查视图,删掉之前的
  234. /// </summary>
  235. /// <param name="regionName"></param>
  236. /// <param name="viewer"></param>
  237. private void CheckViewer(string regionName, object viewer)
  238. {
  239. if (region.Regions.ContainsRegionWithName(regionName))
  240. {
  241. if (region.Regions[regionName].Views.Contains(viewer))
  242. {
  243. var contentRegion = region.Regions[regionName];
  244. contentRegion.Remove(viewer);
  245. }
  246. }
  247. }
  248. /// <summary>
  249. /// 从文件路径创建PDFViewer对象,已包含文档解密逻辑
  250. /// </summary>
  251. /// <param name="path"></param>
  252. /// <returns></returns>
  253. private bool LoadFileFormPath(string path)
  254. {
  255. SplitScreenPDFViewer = new CPDFViewer();
  256. SplitScreenPDFViewer.InitDocument(path);
  257. if (SplitScreenPDFViewer.Document == null)
  258. {
  259. //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_OpenFileFailedWarning"));
  260. return false;
  261. }
  262. else
  263. {
  264. if (SplitScreenPDFViewer.Document.IsLocked)
  265. {
  266. DialogParameters value = new DialogParameters();
  267. value.Add(ParameterNames.PDFDocument, SplitScreenPDFViewer.Document);
  268. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
  269. {
  270. if (e.Result == ButtonResult.OK)
  271. {
  272. if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
  273. {
  274. SplitScreenPDFViewer.Tag = e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString();
  275. }
  276. }
  277. });
  278. if (SplitScreenPDFViewer.Document.IsLocked)
  279. {
  280. //未成功解密文档时,释放Document对象,返回
  281. SplitScreenPDFViewer.Document.Release();
  282. return false;
  283. }
  284. }
  285. }
  286. SplitScreenPDFViewer.Load();
  287. historyPDFViewer = SplitScreenPDFViewer;
  288. return true;
  289. }
  290. public bool IsNavigationTarget(NavigationContext navigationContext)
  291. {
  292. return true;
  293. }
  294. public void OnNavigatedFrom(NavigationContext navigationContext)
  295. {
  296. }
  297. public void OnNavigatedTo(NavigationContext navigationContext)
  298. {
  299. if (navigationContext.Parameters[ParameterNames.PDFViewer] is CPDFViewer cPDFViewer)
  300. {
  301. PDFViewer = cPDFViewer; PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  302. }
  303. }
  304. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  305. {
  306. if (e.Key == "ViewMode")
  307. {
  308. ViewMode ViewMode = (ViewMode)e.Value;
  309. SetPDFViewer();
  310. }
  311. }
  312. }
  313. }