QuickToolsContentViewModel.cs 24 KB

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