QuickToolsContentViewModel.cs 23 KB

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