QuickToolsContentViewModel.cs 22 KB

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