QuickToolsContentViewModel.cs 20 KB

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