QuickToolsContentViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Office.Helper;
  3. using PDF_Office.Model;
  4. using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  5. using PDF_Office.Model.PDFTool;
  6. using PDF_Office.Views;
  7. using PDFSettings;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Controls;
  18. namespace PDF_Office.ViewModels.HomePanel.PDFTools
  19. {
  20. public class QuickToolsContentViewModel : BindableBase
  21. {
  22. #region 属性
  23. /// <summary>
  24. /// 扩展/收缩
  25. /// </summary>
  26. private bool _isExpendTools = false;
  27. public bool IsExpendTools
  28. {
  29. get { return _isExpendTools; }
  30. set { SetProperty(ref _isExpendTools, value); }
  31. }
  32. private string regionName;
  33. public string ToolRegionName
  34. {
  35. get { return regionName; }
  36. set { SetProperty(ref regionName, value); }
  37. }
  38. #endregion
  39. #region Command and Event
  40. public IDialogService dialogs;
  41. public DelegateCommand<ToolItem> QuickToolsCommand { get; set; }
  42. public DelegateCommand<object> OpenMenuCommand { get; set; }
  43. public DelegateCommand ExpendCommand { get; set; }
  44. public DelegateCommand ShowToolCommand { get; set; }
  45. public IRegionManager toolregion;
  46. public event EventHandler<bool> ExpendToolsHanlder;
  47. #endregion
  48. public QuickToolsContentViewModel(IDialogService dialogService, IRegionManager regionManager)
  49. {
  50. toolregion = regionManager;
  51. ToolRegionName = RegionNames.ToolRegionName;
  52. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  53. {
  54. toolregion.RequestNavigate(ToolRegionName, "Guid");
  55. }));
  56. dialogs = dialogService;
  57. InitVariable();
  58. InitCommand();
  59. }
  60. #region 初始化和绑定
  61. private void InitVariable()
  62. {
  63. }
  64. private void InitCommand()
  65. {
  66. QuickToolsCommand = new DelegateCommand<ToolItem>(QuickTools_Click);
  67. OpenMenuCommand = new DelegateCommand<object>(OpenMenu);
  68. ExpendCommand = new DelegateCommand(Expend);
  69. ShowToolCommand = new DelegateCommand(ShowTool);
  70. }
  71. #endregion
  72. private void ShowTool()
  73. {
  74. toolregion.RequestNavigate(ToolRegionName, "Tools");
  75. }
  76. public void QuickTools_Click(ToolItem toolItem)
  77. {
  78. /*
  79. *设置这个对话框的起始保存路径
  80. */
  81. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  82. dlg.Multiselect = false;
  83. dlg.Filter = "PDF|*.pdf;*.PDF;";
  84. if (toolItem.FnType == PDFFnType.Compress || toolItem.FnType == PDFFnType.Security||toolItem.FnType ==PDFFnType.BatchRemove||toolItem.FnType==PDFFnType.ConvertPDF||toolItem.FnType==PDFFnType.Background|| toolItem.FnType == PDFFnType.HeaderFooter|| toolItem.FnType == PDFFnType.BatesNumbers|| toolItem.FnType == PDFFnType.WaterMark|| toolItem.FnType == PDFFnType.PDFToExcel|| toolItem.FnType == PDFFnType.PDFToImage|| toolItem.FnType == PDFFnType.PDFToPPT|| toolItem.FnType == PDFFnType.Batch)
  85. {
  86. dlg.Multiselect = true;
  87. }
  88. if (toolItem.FnType == PDFFnType.ImageToPDF)
  89. {
  90. dlg.Multiselect = true;
  91. dlg.Filter = "Picture|*.png;*.PNG;*.jpg;*.JPG;*bmp;*jpeg;*gif;*tiff;";
  92. }
  93. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  94. {
  95. CPDFViewer viewer = new CPDFViewer();
  96. switch ((PDFFnType)toolItem.FnType)
  97. {
  98. case PDFFnType.Split:
  99. viewer.InitDocument(dlg.FileName);
  100. if (!CheckPassword(viewer)) {return;}
  101. DialogParameters splitvalue = new DialogParameters();
  102. splitvalue.Add(ParameterNames.PDFViewer, viewer);
  103. splitvalue.Add(ParameterNames.FilePath, dlg.FileName);
  104. dialogs.ShowDialog(DialogNames.HomePageSplitDialog, splitvalue, e => { });
  105. break;
  106. case PDFFnType.Extract:
  107. viewer.InitDocument(dlg.FileName);
  108. if (!CheckPassword(viewer)) {return;}
  109. DialogParameters extractvalue = new DialogParameters();
  110. extractvalue.Add(ParameterNames.PDFViewer, viewer);
  111. extractvalue.Add(ParameterNames.FilePath, dlg.FileName);
  112. dialogs.ShowDialog(DialogNames.HomePageExtractDialog, extractvalue, e => { });
  113. break;
  114. case PDFFnType.Insert:
  115. viewer.InitDocument(dlg.FileName);
  116. if (!CheckPassword(viewer)) {return;}
  117. DialogParameters insertvalue = new DialogParameters();
  118. insertvalue.Add(ParameterNames.PDFViewer, viewer);
  119. insertvalue.Add(ParameterNames.FilePath, dlg.FileName);
  120. dialogs.ShowDialog(DialogNames.HomePageInsertDialog, insertvalue, e => { });
  121. break;
  122. case PDFFnType.Compress:
  123. DialogParameters compresspdf = new DialogParameters();
  124. compresspdf.Add(ParameterNames.BatchProcessing_Name, "1");
  125. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  126. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1;
  127. compresspdf.Add(ParameterNames.FilePath, dlg.FileNames);
  128. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, compresspdf, e => { });
  129. break;
  130. case PDFFnType.ImageToPDF:
  131. DialogParameters picturetopdf = new DialogParameters();
  132. picturetopdf.Add(ParameterNames.FilePath, dlg.FileNames);
  133. dialogs.ShowDialog(DialogNames.HomePagePictureToPDFDialog, picturetopdf, e => { });
  134. break;
  135. case PDFFnType.Print:
  136. viewer.InitDocument(dlg.FileName);
  137. if (!CheckPassword(viewer)) {return;}
  138. DialogParameters printvalue = new DialogParameters();
  139. printvalue.Add(ParameterNames.PDFViewer, viewer);
  140. printvalue.Add(ParameterNames.FilePath, dlg.FileName);
  141. dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printvalue, e => { });
  142. try
  143. {
  144. dialogs.ShowDialog("MainWindow");
  145. }
  146. catch
  147. {
  148. }
  149. break;
  150. case PDFFnType.Security:
  151. DialogParameters securitypdf = new DialogParameters();
  152. securitypdf.Add(ParameterNames.BatchProcessing_Name, "2");
  153. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  154. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 2;
  155. securitypdf.Add(ParameterNames.FilePath, dlg.FileNames);
  156. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, securitypdf, e => { });
  157. break;
  158. case PDFFnType.ConvertPDF:
  159. DialogParameters convertpdf = new DialogParameters();
  160. convertpdf.Add(ParameterNames.BatchProcessing_Name, "0");
  161. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  162. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  163. convertpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  164. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdf, e => { });
  165. break;
  166. case PDFFnType.BatchRemove:
  167. DialogParameters batchremovepdf = new DialogParameters();
  168. batchremovepdf.Add(ParameterNames.BatchProcessing_Name, "7");
  169. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  170. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 7;
  171. batchremovepdf.Add(ParameterNames.FilePath, dlg.FileNames);
  172. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batchremovepdf, e => { });
  173. break;
  174. case PDFFnType.HeaderFooter:
  175. DialogParameters headerFooterpdf = new DialogParameters();
  176. headerFooterpdf.Add(ParameterNames.BatchProcessing_Name, "5");
  177. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  178. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 5;
  179. headerFooterpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  180. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, headerFooterpdf, e => { });
  181. break;
  182. case PDFFnType.BatesNumbers:
  183. DialogParameters batesNumberspdf = new DialogParameters();
  184. batesNumberspdf.Add(ParameterNames.BatchProcessing_Name, "6");
  185. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  186. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 6;
  187. batesNumberspdf.Add(ParameterNames.FilePath, dlg.FileNames);
  188. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batesNumberspdf, e => { });
  189. break;
  190. case PDFFnType.Background:
  191. DialogParameters backgroundpdf = new DialogParameters();
  192. backgroundpdf.Add(ParameterNames.BatchProcessing_Name, "4");
  193. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  194. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 4;
  195. backgroundpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  196. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, backgroundpdf, e => { });
  197. break;
  198. case PDFFnType.WaterMark:
  199. DialogParameters watermarkpdf = new DialogParameters();
  200. watermarkpdf.Add(ParameterNames.BatchProcessing_Name, "3");
  201. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  202. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 3;
  203. watermarkpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  204. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, watermarkpdf, e => { });
  205. break;
  206. case PDFFnType.PDFToImage:
  207. DialogParameters convertpdftoimage = new DialogParameters();
  208. convertpdftoimage.Add(ParameterNames.BatchProcessing_Name, "0");
  209. convertpdftoimage.Add("ConverterTypeIndex", 7);
  210. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  211. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  212. convertpdftoimage.Add(ParameterNames.FilePath, dlg.FileNames);
  213. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoimage, e => { });
  214. break;
  215. case PDFFnType.PDFToExcel:
  216. DialogParameters convertpdftoexcel = new DialogParameters();
  217. convertpdftoexcel.Add(ParameterNames.BatchProcessing_Name, "0");
  218. convertpdftoexcel.Add("ConverterTypeIndex", 1);
  219. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  220. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  221. convertpdftoexcel.Add(ParameterNames.FilePath, dlg.FileNames);
  222. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoexcel, e => { });
  223. break;
  224. case PDFFnType.PDFToPPT:
  225. DialogParameters convertpdftoppt = new DialogParameters();
  226. convertpdftoppt.Add(ParameterNames.BatchProcessing_Name, "0");
  227. convertpdftoppt.Add("ConverterTypeIndex", 2);
  228. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  229. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  230. convertpdftoppt.Add(ParameterNames.FilePath, dlg.FileNames);
  231. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoppt, e => { });
  232. break;
  233. case PDFFnType.PDFToWord:
  234. DialogParameters convertpdftoword = new DialogParameters();
  235. convertpdftoword.Add(ParameterNames.BatchProcessing_Name, "0");
  236. convertpdftoword.Add("ConverterTypeIndex", 0);
  237. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  238. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  239. convertpdftoword.Add(ParameterNames.FilePath, dlg.FileNames);
  240. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoword, e => { });
  241. break;
  242. case PDFFnType.Batch:
  243. DialogParameters batcpdf = new DialogParameters();
  244. batcpdf.Add(ParameterNames.BatchProcessing_Name, "0");
  245. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  246. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  247. batcpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  248. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batcpdf, e => { });
  249. break;
  250. }
  251. }
  252. }
  253. //
  254. //
  255. private bool CheckPassword(CPDFViewer viewer) {
  256. if (viewer.Document.IsLocked)
  257. {
  258. DialogParameters value = new DialogParameters();
  259. value.Add(ParameterNames.PDFDocument, viewer.Document);
  260. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
  261. {
  262. if (e.Result == ButtonResult.OK)
  263. {
  264. if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
  265. {
  266. viewer.Tag = e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString();
  267. }
  268. }
  269. });
  270. if (viewer.Document.IsLocked)
  271. {
  272. //未成功解密文档时,释放Document对象,返回
  273. viewer.Document.Release();
  274. return false;
  275. }
  276. }
  277. return true;
  278. }
  279. private void Expend()
  280. {
  281. IsExpendTools = !IsExpendTools;
  282. ExpendToolsHanlder?.Invoke(null, IsExpendTools);
  283. }
  284. private void OpenMenu(object obj)
  285. {
  286. var menu = App.Current.FindResource("ExpendToolsContextMenu") as ContextMenu;
  287. var btn = obj as Button;
  288. if (menu != null && btn != null)
  289. {
  290. if(menu.Items.Count == 1)
  291. {
  292. var item = menu.Items[0] as MenuItem;
  293. if(_isExpendTools)
  294. {
  295. item.Header = "收缩";
  296. }
  297. else
  298. {
  299. item.Header = "展开";
  300. }
  301. item.Command = ExpendCommand;
  302. //btn.ContextMenu = menu;
  303. menu.PlacementTarget = btn;
  304. menu.IsOpen = true;
  305. }
  306. }
  307. }
  308. }
  309. }