QuickToolsContentViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. break;
  172. case PDFFnType.ConvertPDF:
  173. //DialogParameters convertpdf = new DialogParameters();
  174. //convertpdf.Add(ParameterNames.BatchProcessing_Name, "0");
  175. //HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  176. //HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  177. //convertpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  178. //dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdf, e => { });
  179. break;
  180. case PDFFnType.BatchRemove:
  181. DialogParameters batchremovepdf = new DialogParameters();
  182. batchremovepdf.Add(ParameterNames.BatchProcessing_Name, "7");
  183. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  184. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 7;
  185. batchremovepdf.Add(ParameterNames.FilePath, dlg.FileNames);
  186. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batchremovepdf, e => { });
  187. break;
  188. case PDFFnType.HeaderFooter:
  189. DialogParameters headerFooterpdf = new DialogParameters();
  190. headerFooterpdf.Add(ParameterNames.BatchProcessing_Name, "5");
  191. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  192. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 5;
  193. headerFooterpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  194. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, headerFooterpdf, e => { });
  195. break;
  196. case PDFFnType.BatesNumbers:
  197. DialogParameters batesNumberspdf = new DialogParameters();
  198. batesNumberspdf.Add(ParameterNames.BatchProcessing_Name, "6");
  199. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  200. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 6;
  201. batesNumberspdf.Add(ParameterNames.FilePath, dlg.FileNames);
  202. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batesNumberspdf, e => { });
  203. break;
  204. case PDFFnType.Background:
  205. DialogParameters backgroundpdf = new DialogParameters();
  206. backgroundpdf.Add(ParameterNames.BatchProcessing_Name, "4");
  207. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  208. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 4;
  209. backgroundpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  210. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, backgroundpdf, e => { });
  211. break;
  212. case PDFFnType.WaterMark:
  213. DialogParameters watermarkpdf = new DialogParameters();
  214. watermarkpdf.Add(ParameterNames.BatchProcessing_Name, "3");
  215. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  216. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 3;
  217. watermarkpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  218. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, watermarkpdf, e => { });
  219. break;
  220. case PDFFnType.PDFToImage:
  221. viewer.InitDocument(dlg.FileName);
  222. if (!CheckPassword(viewer)) { return; }
  223. DialogParameters convertpdftoimage = new DialogParameters();
  224. convertpdftoimage.Add(ParameterNames.PDFViewer, viewer);
  225. dialogs.ShowDialog(DialogNames.ConverterImgDialog, convertpdftoimage, e =>
  226. {
  227. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  228. converterDialogsModel.OnOpened((DialogResult)e);
  229. });
  230. //DialogParameters convertpdftoimage = new DialogParameters();
  231. //convertpdftoimage.Add(ParameterNames.BatchProcessing_Name, "0");
  232. //convertpdftoimage.Add("ConverterTypeIndex", 7);
  233. //HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  234. //HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  235. //convertpdftoimage.Add(ParameterNames.FilePath, dlg.FileNames);
  236. //dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoimage, e => { });
  237. break;
  238. case PDFFnType.PDFToExcel:
  239. viewer.InitDocument(dlg.FileName);
  240. if (!CheckPassword(viewer)) { return; }
  241. DialogParameters convertpdftoexcel = new DialogParameters();
  242. convertpdftoexcel.Add(ParameterNames.PDFViewer, viewer);
  243. dialogs.ShowDialog(DialogNames.ConverterExcelDialog, convertpdftoexcel, e =>
  244. {
  245. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  246. converterDialogsModel.OnOpened((DialogResult)e);
  247. });
  248. //DialogParameters convertpdftoexcel = new DialogParameters();
  249. //convertpdftoexcel.Add(ParameterNames.BatchProcessing_Name, "0");
  250. //convertpdftoexcel.Add("ConverterTypeIndex", 1);
  251. //HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  252. //HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  253. //convertpdftoexcel.Add(ParameterNames.FilePath, dlg.FileNames);
  254. //dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoexcel, e => { });
  255. break;
  256. case PDFFnType.PDFToPPT:
  257. viewer.InitDocument(dlg.FileName);
  258. if (!CheckPassword(viewer)) { return; }
  259. DialogParameters convertpdf = new DialogParameters();
  260. convertpdf.Add(ParameterNames.PDFViewer, viewer);
  261. dialogs.ShowDialog(DialogNames.ConverterPPTDialog, convertpdf, e =>
  262. {
  263. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  264. converterDialogsModel.OnOpened((DialogResult)e);
  265. });
  266. //批量处理
  267. //DialogParameters convertpdftoppt = new DialogParameters();
  268. //convertpdftoppt.Add(ParameterNames.BatchProcessing_Name, "0");
  269. //convertpdftoppt.Add("ConverterTypeIndex", 2);
  270. //HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  271. //HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  272. //convertpdftoppt.Add(ParameterNames.FilePath, dlg.FileNames);
  273. //dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoppt, e => { });
  274. break;
  275. case PDFFnType.PDFToWord:
  276. viewer.InitDocument(dlg.FileName);
  277. if (!CheckPassword(viewer)) { return; }
  278. DialogParameters convertpdftoword = new DialogParameters();
  279. convertpdftoword.Add(ParameterNames.PDFViewer, viewer);
  280. dialogs.ShowDialog(DialogNames.ConverterWordDialog, convertpdftoword, e =>
  281. {
  282. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  283. converterDialogsModel.OnOpened((DialogResult)e);
  284. });
  285. //批量处理
  286. //DialogParameters convertpdftoword = new DialogParameters();
  287. //convertpdftoword.Add(ParameterNames.BatchProcessing_Name, "0");
  288. //convertpdftoword.Add("ConverterTypeIndex", 0);
  289. //HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  290. //HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  291. //convertpdftoword.Add(ParameterNames.FilePath, dlg.FileNames);
  292. //dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoword, e => { });
  293. break;
  294. case PDFFnType.Batch:
  295. DialogParameters batcpdf = new DialogParameters();
  296. batcpdf.Add(ParameterNames.BatchProcessing_Name, "0");
  297. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  298. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  299. batcpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  300. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batcpdf, e => { });
  301. break;
  302. case PDFFnType.Merge:
  303. viewer.InitDocument(dlg.FileName);
  304. if (!CheckPassword(viewer)) { return; }
  305. DialogParameters merge = new DialogParameters();
  306. merge.Add(ParameterNames.PDFViewer, viewer);
  307. dialogs.ShowDialog(DialogNames.MergeDialog, merge, e =>
  308. {
  309. });
  310. break;
  311. }
  312. }
  313. }
  314. //
  315. //
  316. private bool CheckPassword(CPDFViewer viewer) {
  317. if (viewer.Document.IsLocked)
  318. {
  319. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(viewer.Document, EnumPasswordKind.StatusOpenPassword, dialogs);
  320. if (result.IsDiscryptied)
  321. {
  322. if (result.Password != null)
  323. {
  324. string filePath = viewer.Document.FilePath;
  325. viewer.Document.UnlockWithPassword(result.Password);
  326. viewer.Tag = result.Password;
  327. }
  328. ///TODO:
  329. ///此处填入需要执行的代码
  330. }
  331. if (result.IsDiscryptied == false)
  332. {
  333. //未成功解密文档时,释放Document对象,返回
  334. viewer.Document.Release();
  335. return false;
  336. }
  337. }
  338. return true;
  339. }
  340. private void Expend()
  341. {
  342. IsExpendTools = !IsExpendTools;
  343. ExpendToolsHanlder?.Invoke(null, IsExpendTools);
  344. }
  345. private void OpenMenu(object obj)
  346. {
  347. var menu = App.Current.FindResource("ExpendToolsContextMenu") as ContextMenu;
  348. var btn = obj as Button;
  349. if (menu != null && btn != null)
  350. {
  351. if(menu.Items.Count == 1)
  352. {
  353. var item = menu.Items[0] as MenuItem;
  354. if(_isExpendTools)
  355. {
  356. item.Header = "收缩";
  357. }
  358. else
  359. {
  360. item.Header = "展开";
  361. }
  362. item.Command = ExpendCommand;
  363. //btn.ContextMenu = menu;
  364. menu.PlacementTarget = btn;
  365. menu.IsOpen = true;
  366. }
  367. }
  368. }
  369. }
  370. }