QuickToolsContentViewModel.cs 16 KB

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