CompressDialogViewModel.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Master.Model.Dialog.ToolsDialogs;
  3. using PDF_Master.Model;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using Prism.Services.Dialogs;
  7. using System;
  8. using System.Diagnostics;
  9. using System.Windows.Forms;
  10. using System.Windows;
  11. using DialogResult = Prism.Services.Dialogs.DialogResult;
  12. using MessageBox = System.Windows.Forms.MessageBox;
  13. using PDF_Master.Helper;
  14. using ComPDFKitViewer.PdfViewer;
  15. using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  16. using System.Linq;
  17. using System.Collections.Generic;
  18. using PDF_Master.Properties;
  19. namespace PDF_Master.ViewModels.Dialog.ToolsDialogs.CompressDialogs
  20. {
  21. public class CompressDialogViewModel : BindableBase, IDialogAware
  22. {
  23. #region 文案
  24. private string T_title;
  25. public string T_Title
  26. {
  27. get { return T_title; }
  28. set
  29. {
  30. SetProperty(ref T_title, value);
  31. }
  32. }
  33. private string T_high;
  34. public string T_High
  35. {
  36. get { return T_high; }
  37. set
  38. {
  39. SetProperty(ref T_high, value);
  40. }
  41. }
  42. private string T_standard;
  43. public string T_Standard
  44. {
  45. get { return T_standard; }
  46. set
  47. {
  48. SetProperty(ref T_standard, value);
  49. }
  50. }
  51. private string T_low;
  52. public string T_Low
  53. {
  54. get { return T_low; }
  55. set
  56. {
  57. SetProperty(ref T_low, value);
  58. }
  59. }
  60. private string T_minimum;
  61. public string T_Minimum
  62. {
  63. get { return T_minimum; }
  64. set
  65. {
  66. SetProperty(ref T_minimum, value);
  67. }
  68. }
  69. private string T_compress;
  70. public string T_Compress
  71. {
  72. get { return T_compress; }
  73. set
  74. {
  75. SetProperty(ref T_compress, value);
  76. }
  77. }
  78. private string T_cancel;
  79. public string T_Cancel
  80. {
  81. get { return T_cancel; }
  82. set
  83. {
  84. SetProperty(ref T_cancel, value);
  85. }
  86. }
  87. private void InitSting()
  88. {
  89. T_Title = App.MainPageLoader.GetString("Compress_Title");
  90. T_High = App.MainPageLoader.GetString("Compress_High");
  91. T_Standard = App.MainPageLoader.GetString("Compress_Standard");
  92. T_Low = App.MainPageLoader.GetString("Compress_Low");
  93. T_Minimum = App.MainPageLoader.GetString("Compress_Minimum");
  94. T_Compress = App.MainPageLoader.GetString("Compress_Compress");
  95. T_Cancel = App.MainPageLoader.GetString("Compress_Cancel");
  96. }
  97. #endregion
  98. #region 参数和属性
  99. private CompressDialogModel compressDialogModel;
  100. private IntPtr compressingIntpr = IntPtr.Zero;
  101. private CPDFDocument document;
  102. private CPDFViewer pdfviewer;
  103. public IDialogService dialogs;
  104. #endregion
  105. #region 委托声明
  106. public DelegateCommand LargeQualityCommand { get; set; }
  107. public DelegateCommand StandardQualityCommand { get; set; }
  108. public DelegateCommand LittleQualityCommand { get; set; }
  109. public DelegateCommand MicroQualityCommand { get; set; }
  110. public DelegateCommand CompressCommand { get; set; }
  111. public DelegateCommand ConfirmCompressCommand { get; set; }
  112. public DelegateCommand BatchCompressCommand { get; set; }
  113. #endregion
  114. public CompressDialogViewModel(IDialogService dialogService)
  115. {
  116. InitSting();
  117. LargeQualityCommand = new DelegateCommand(LargeQuality);
  118. StandardQualityCommand = new DelegateCommand(StandardQuality);
  119. LittleQualityCommand = new DelegateCommand(LittleQuality);
  120. MicroQualityCommand = new DelegateCommand(MicroQuality);
  121. CompressCommand = new DelegateCommand(Compress);
  122. ConfirmCompressCommand = new DelegateCommand(ConfirmCompress);
  123. BatchCompressCommand = new DelegateCommand(BatchCompress);
  124. dialogs = dialogService;
  125. }
  126. #region 逻辑函数
  127. private void LargeQuality()
  128. {
  129. compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusLarge;
  130. }
  131. private void StandardQuality()
  132. {
  133. compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusStandard;
  134. }
  135. private void LittleQuality()
  136. {
  137. compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusLittle;
  138. }
  139. private void MicroQuality()
  140. {
  141. compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusMicro;
  142. }
  143. private void ConfirmCompress()
  144. {
  145. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  146. }
  147. private void BatchCompress() {
  148. DialogParameters compresspdf = new DialogParameters();
  149. compresspdf.Add(ParameterNames.BatchProcessing_Name, "1");
  150. HomePageBatchProcessingDialogModel.FilePaths =new List<string> { pdfviewer.Document.FilePath.ToString() } ;
  151. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1;
  152. compresspdf.Add(ParameterNames.FilePath, new string[] { pdfviewer.Document.FilePath.ToString() });
  153. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, compresspdf, e => { });
  154. }
  155. private int GetIndex(int pageindex)
  156. {
  157. Trace.WriteLine(pageindex);
  158. return 0;
  159. }
  160. private void Compress()
  161. {
  162. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  163. /*
  164. *设置这个对话框的起始保存路径
  165. */
  166. sfd.InitialDirectory = pdfviewer.Document.FilePath;
  167. /*
  168. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  169. */
  170. sfd.Filter = "PDF|*.pdf;";
  171. /*
  172. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  173. */
  174. sfd.FileName = pdfviewer.Document.FileName + "_Compress.pdf";
  175. /*
  176. * 做一些工作
  177. */
  178. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  179. {
  180. string saveSelectedPath = sfd.FileName;
  181. /*
  182. * 做一些工作
  183. */
  184. Trace.WriteLine("compressDialogModel.CompressQuality: " + compressDialogModel.CompressQuality);
  185. var dialogresult = new DialogResult(ButtonResult.OK);
  186. dialogresult.Parameters.Add(ParameterNames.PDFDocument, document);
  187. dialogresult.Parameters.Add(ParameterNames.PassWord, pdfviewer.Tag);
  188. dialogresult.Parameters.Add(ParameterNames.FilePath, saveSelectedPath);
  189. dialogresult.Parameters.Add("compressDialogModel.CompressQuality", (int)compressDialogModel.CompressQuality);
  190. RequestClose.Invoke(dialogresult);
  191. //DialogParameters value = new DialogParameters();
  192. //value.Add(ParameterNames.PDFDocument, document);
  193. //value.Add(ParameterNames.PassWord, pdfviewer.Tag);
  194. //value.Add(ParameterNames.FilePath, saveSelectedPath);
  195. //value.Add("compressDialogModel.CompressQuality", (int)compressDialogModel.CompressQuality);
  196. //RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  197. //dialogs.ShowDialog(DialogNames.CompressProgressBarDialog, value, e =>
  198. //{
  199. //});
  200. }
  201. else { return; }
  202. }
  203. #endregion
  204. #region 框架行为
  205. public string Title => "";
  206. public event Action<IDialogResult> RequestClose;
  207. public bool CanCloseDialog()
  208. {
  209. return true;
  210. }
  211. public void OnDialogClosed()
  212. {
  213. }
  214. public void OnDialogOpened(IDialogParameters parameters)
  215. {
  216. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfviewer);
  217. if (pdfviewer != null)
  218. {
  219. CompressDialogModel compressdialogmodel = new CompressDialogModel();
  220. document = pdfviewer.Document;
  221. compressDialogModel = compressdialogmodel;
  222. compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusDefault;
  223. }
  224. }
  225. #endregion
  226. }
  227. }