HomeContentViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. using Microsoft.Win32;
  2. using PDF_Master.CustomControl;
  3. using PDF_Master.EventAggregators;
  4. using PDF_Master.Helper;
  5. using PDF_Master.Model;
  6. using Prism.Commands;
  7. using Prism.Events;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using WIA;
  19. namespace PDF_Master.ViewModels
  20. {
  21. public class HomeContentViewModel : BindableBase, INavigationAware
  22. {
  23. #region 文案
  24. private string T_openFiles;
  25. public string T_OpenFiles
  26. {
  27. get { return T_openFiles; }
  28. set
  29. {
  30. SetProperty(ref T_openFiles, value);
  31. }
  32. }
  33. private string T_createPDF;
  34. public string T_CreatePDF
  35. {
  36. get { return T_createPDF; }
  37. set
  38. {
  39. SetProperty(ref T_createPDF, value);
  40. }
  41. }
  42. private string T_createPDFToNew;
  43. public string T_CreatePDFToNew
  44. {
  45. get { return T_createPDFToNew; }
  46. set
  47. {
  48. SetProperty(ref T_createPDFToNew, value);
  49. }
  50. }
  51. private void InitString()
  52. {
  53. T_OpenFiles = App.HomePageLoader.GetString("Home_OpenFiles");
  54. T_CreatePDF = App.HomePageLoader.GetString("Home_CreatePDF");
  55. T_CreatePDFToNew = App.HomePageLoader.GetString("Home_CreateToNew");
  56. }
  57. #endregion
  58. private string fileName = "Home";
  59. public string FileName
  60. {
  61. get { return fileName; }
  62. set { SetProperty(ref fileName, value); }
  63. }
  64. private string regionName;
  65. public string ToolRegionName
  66. {
  67. get { return regionName; }
  68. set { SetProperty(ref regionName, value); }
  69. }
  70. private Visibility isLoading = Visibility.Collapsed;
  71. public Visibility IsLoading
  72. {
  73. get { return isLoading; }
  74. set
  75. {
  76. SetProperty(ref isLoading, value);
  77. }
  78. }
  79. private MainContentViewModel mainContentViewModel;
  80. public DelegateCommand OpenFileCommand { get; set; }
  81. public DelegateCommand<string> ShowToolCommand { get; set; }
  82. public DelegateCommand CreateBlackPDFCommand { get; set; }
  83. public DelegateCommand CreateFromOtherFile { get; set; }
  84. public DelegateCommand CreateFromHtmlCommnd { get; set; }
  85. public DelegateCommand CreateFromScanner { get; set; }
  86. public IRegionManager toolregion;
  87. public IEventAggregator eventer;
  88. public IDialogService dialog;
  89. public HomeContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogaware)
  90. {
  91. toolregion = regionManager;
  92. eventer = eventAggregator;
  93. dialog = dialogaware;
  94. ToolRegionName = RegionNames.ToolRegionName;
  95. OpenFileCommand = new DelegateCommand(OpenFile);
  96. ShowToolCommand = new DelegateCommand<string>(ShowToolContent);
  97. CreateBlackPDFCommand = new DelegateCommand(CreatBlankPDF);
  98. CreateFromOtherFile = new DelegateCommand(createFromOtherFile);
  99. CreateFromHtmlCommnd = new DelegateCommand(createFormHtml);
  100. CreateFromScanner = new DelegateCommand(createFromScanner);
  101. InitString();
  102. }
  103. /// <summary>
  104. /// 显示右侧不同的工具栏页面
  105. /// </summary>
  106. /// <param name="view"></param>
  107. public void ShowToolContent(string view)
  108. {
  109. toolregion.RequestNavigate(ToolRegionName, view);
  110. }
  111. /// <summary>
  112. /// 从外部拖拽打开文件
  113. /// </summary>
  114. /// <param name="files"></param>
  115. public async void AddFileFromDrag(List<string> files)
  116. {
  117. string txt = Properties.Resources.txtex;
  118. string word = Properties.Resources.wordex;
  119. string ppt = Properties.Resources.pptex;
  120. string excel = Properties.Resources.excelex;
  121. string html = Properties.Resources.htmlex;
  122. string image = Properties.Resources.imageex;
  123. string allfiles = txt + word + excel + ppt + html;
  124. if (files.Count() == 1 && App.OpenedFileList.Contains(files[0]))
  125. {
  126. //单文件打开重复文件时 需要选中已打开的重复文件
  127. App.mainWindowViewModel.SelectItem(files[0]);
  128. return;
  129. }
  130. IsLoading = Visibility.Visible;
  131. //排除文件夹或其他类型文件
  132. if (!System.IO.File.Exists(files[0]))
  133. {
  134. AlertsMessage alertsMessage = new AlertsMessage();
  135. alertsMessage.ShowDialog("", "Unsupported file format", "OK");
  136. IsLoading = Visibility.Collapsed;
  137. return;
  138. }
  139. //先在当前页签打开第一个文档
  140. var type = System.IO.Path.GetExtension(files[0]).ToLower();
  141. if (image.Contains(type))
  142. {
  143. //从图片创建
  144. mainContentViewModel.CreateFile(files[0]);
  145. }
  146. else if (type == ".pdf")
  147. {
  148. mainContentViewModel.OpenFile(files[0]);
  149. }
  150. else if(allfiles.Contains(type))
  151. {
  152. await mainContentViewModel.CreateFileFromOffice(files[0]);
  153. }
  154. else
  155. {
  156. AlertsMessage alertsMessage = new AlertsMessage();
  157. alertsMessage.ShowDialog("", "Unsupported file format", "OK");
  158. IsLoading = Visibility.Collapsed;
  159. return;
  160. }
  161. ToolMethod.SetFileThumbImg(files[0]);
  162. //循环用新页签打开新文档
  163. for (int i = 1; i < files.Count(); i++)
  164. {
  165. if (!App.OpenedFileList.Contains(files[i]))
  166. {
  167. App.mainWindowViewModel.AddTabItem(files[i]);
  168. await Task.Delay(50);
  169. }
  170. ToolMethod.SetFileThumbImg(files[i]);
  171. }
  172. IsLoading = Visibility.Collapsed;
  173. }
  174. /// <summary>
  175. /// 从其他格式文件创建PDF
  176. /// </summary>
  177. private async void createFromOtherFile()
  178. {
  179. string txt = Properties.Resources.txtex;
  180. string word = Properties.Resources.wordex;
  181. string ppt = Properties.Resources.pptex;
  182. string excel = Properties.Resources.excelex;
  183. string html = Properties.Resources.htmlex;
  184. string image = Properties.Resources.imageex;
  185. string allfiles = txt + word + excel + ppt + image + html;
  186. OpenFileDialog dialog = new OpenFileDialog();
  187. dialog.Multiselect = true;
  188. dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
  189. $"Microsoft Office Word({word})|{word}|" +
  190. $"Microsoft Office Excel({excel})|{excel}|" +
  191. $"Microsoft Office PowerPoint({ppt})|{ppt}|" +
  192. $"Txt({txt})|{txt}|" +
  193. $"Picture({image})|{image}|" +
  194. $"Html({html})|{html}");
  195. if ((bool)dialog.ShowDialog())
  196. {
  197. var fileList = dialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  198. if (fileList.Count <= 0)
  199. {
  200. IsLoading = Visibility.Collapsed;
  201. return;
  202. }
  203. IsLoading = Visibility.Visible;
  204. await Task.Delay(10);
  205. //在当前页签打开第一个文件
  206. var type = System.IO.Path.GetExtension(fileList[0]).ToLower();
  207. if (image.Contains(type))
  208. {
  209. //图片文件
  210. mainContentViewModel.CreateFile(fileList[0]);
  211. }
  212. else
  213. {
  214. await mainContentViewModel.CreateFileFromOffice(fileList[0]);
  215. }
  216. ToolMethod.SetFileThumbImg(fileList[0]);
  217. for (int i = 1; i < fileList.Count(); i++)
  218. {
  219. if (!App.OpenedFileList.Contains(fileList[i]))
  220. {
  221. App.mainWindowViewModel.AddTabItem(fileList[i]);
  222. await Task.Delay(50);
  223. }
  224. ToolMethod.SetFileThumbImg(fileList[i]);
  225. }
  226. IsLoading = Visibility.Collapsed;
  227. }
  228. }
  229. /// <summary>
  230. /// 从网页创建PDF文件
  231. /// </summary>
  232. private void createFormHtml()
  233. {
  234. dialog.ShowDialog(DialogNames.CreateFromHtmlDialog, async e =>
  235. {
  236. if (e.Result == ButtonResult.OK)
  237. {
  238. IsLoading = Visibility.Visible;
  239. var model = e.Parameters.GetValue<Model.Dialog.HomePageToolsDialogs.HtmlModel>(ParameterNames.DataModel);
  240. await mainContentViewModel.CreateFileFromOffice(model.FilePath, model.PageSize, model.Margin);
  241. IsLoading = Visibility.Collapsed;
  242. }
  243. });
  244. }
  245. /// <summary>
  246. /// 从扫描仪创建
  247. /// </summary>
  248. private void createFromScanner()
  249. {
  250. dialog.ShowDialog(DialogNames.CreateFromScannerDialogs, async e =>
  251. {
  252. if (e.Result == ButtonResult.OK)
  253. {
  254. IsLoading = Visibility.Visible;
  255. string path;
  256. e.Parameters.TryGetValue("Imagepath", out path);
  257. mainContentViewModel.CreateFile(path);
  258. IsLoading = Visibility.Collapsed;
  259. }
  260. });
  261. }
  262. /// <summary>
  263. /// 打开文件
  264. /// </summary>
  265. public async void OpenFile()
  266. {
  267. OpenFileDialog openFileDialog = new OpenFileDialog();
  268. openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
  269. openFileDialog.Multiselect = true;
  270. if ((bool)openFileDialog.ShowDialog())
  271. {
  272. IsLoading = Visibility.Visible;
  273. await Task.Delay(3);
  274. if (openFileDialog.FileNames.Count() == 1)
  275. {
  276. //单文件打开重复文件时 需要选中已打开的重复文件
  277. if (App.OpenedFileList.Contains(openFileDialog.FileName))
  278. {
  279. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  280. }
  281. else
  282. {
  283. mainContentViewModel.OpenFile(openFileDialog.FileName);
  284. }
  285. ToolMethod.SetFileThumbImg(openFileDialog.FileName);
  286. }
  287. else
  288. {
  289. var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  290. if (fileList.Count <= 0)
  291. {
  292. IsLoading = Visibility.Collapsed;
  293. return;
  294. }
  295. mainContentViewModel.OpenFile(fileList[0]);
  296. for (int i = 1; i < fileList.Count(); i++)
  297. {
  298. if (!App.OpenedFileList.Contains(fileList[i]))
  299. {
  300. App.mainWindowViewModel.AddTabItem(fileList[i]);
  301. }
  302. ToolMethod.SetFileThumbImg(fileList[i]);
  303. }
  304. }
  305. IsLoading = Visibility.Collapsed;
  306. }
  307. }
  308. /// <summary>
  309. /// 创建空白文档
  310. /// </summary>
  311. public void CreatBlankPDF()
  312. {
  313. mainContentViewModel.CreateFile();
  314. }
  315. #region Navigate
  316. public void OnNavigatedTo(NavigationContext navigationContext)
  317. {
  318. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  319. if (mainVM != null)
  320. {
  321. mainContentViewModel = mainVM;
  322. mainContentViewModel.homeContentViewModel = this;
  323. }
  324. toolregion.RequestNavigate(ToolRegionName, "Guid");
  325. }
  326. public bool IsNavigationTarget(NavigationContext navigationContext)
  327. {
  328. return true;
  329. }
  330. public void OnNavigatedFrom(NavigationContext navigationContext)
  331. {
  332. }
  333. #endregion
  334. }
  335. }