CompressDialogViewModel.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Office.Model.Dialog.ToolsDialogs;
  3. using PDF_Office.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. namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.CompressDialogs
  14. {
  15. public class CompressDialogViewModel : BindableBase, IDialogAware
  16. {
  17. #region 参数和属性
  18. private CompressDialogModel compressDialogModel;
  19. private IntPtr compressingIntpr = IntPtr.Zero;
  20. private CPDFDocument document;
  21. private Visibility _compressLargeStyle = Visibility.Hidden;
  22. public IDialogService dialogs;
  23. public Visibility CompressLargeStyle
  24. {
  25. get { return _compressLargeStyle; }
  26. set
  27. {
  28. SetProperty(ref _compressLargeStyle, value);
  29. }
  30. }
  31. private Visibility _compressStandardStyle = Visibility.Hidden;
  32. public Visibility CompressStandardStyle
  33. {
  34. get { return _compressStandardStyle; }
  35. set
  36. {
  37. SetProperty(ref _compressStandardStyle, value);
  38. }
  39. }
  40. private Visibility _compressLittleStyle = Visibility.Hidden;
  41. public Visibility CompressLittleStyle
  42. {
  43. get { return _compressLittleStyle; }
  44. set
  45. {
  46. SetProperty(ref _compressLittleStyle, value);
  47. }
  48. }
  49. private Visibility _compressMicroStyle = Visibility.Hidden;
  50. public Visibility CompressMicroStyle
  51. {
  52. get { return _compressMicroStyle; }
  53. set
  54. {
  55. SetProperty(ref _compressMicroStyle, value);
  56. }
  57. }
  58. #endregion
  59. #region 委托声明
  60. public DelegateCommand LargeQualityCommand { get; set; }
  61. public DelegateCommand StandardQualityCommand { get; set; }
  62. public DelegateCommand LittleQualityCommand { get; set; }
  63. public DelegateCommand MicroQualityCommand { get; set; }
  64. public DelegateCommand CompressCommand { get; set; }
  65. public DelegateCommand ConfirmCompressCommand { get; set; }
  66. #endregion
  67. public CompressDialogViewModel(IDialogService dialogService)
  68. {
  69. LargeQualityCommand = new DelegateCommand(LargeQuality);
  70. StandardQualityCommand = new DelegateCommand(StandardQuality);
  71. LittleQualityCommand = new DelegateCommand(LittleQuality);
  72. MicroQualityCommand = new DelegateCommand(MicroQuality);
  73. CompressCommand = new DelegateCommand(Compress);
  74. ConfirmCompressCommand = new DelegateCommand(ConfirmCompress);
  75. dialogs = dialogService;
  76. }
  77. #region 逻辑函数
  78. private void LargeQuality()
  79. {
  80. compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusLarge;
  81. CompressLargeStyle = Visibility.Visible;
  82. CompressStandardStyle = Visibility.Hidden;
  83. CompressLittleStyle = Visibility.Hidden;
  84. CompressMicroStyle = Visibility.Hidden;
  85. }
  86. private void StandardQuality()
  87. {
  88. compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusStandard;
  89. CompressLargeStyle = Visibility.Hidden;
  90. CompressStandardStyle = Visibility.Visible;
  91. CompressLittleStyle = Visibility.Hidden;
  92. CompressMicroStyle = Visibility.Hidden;
  93. }
  94. private void LittleQuality()
  95. {
  96. compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusLittle;
  97. CompressLargeStyle = Visibility.Hidden;
  98. CompressStandardStyle = Visibility.Hidden;
  99. CompressLittleStyle = Visibility.Visible;
  100. CompressMicroStyle = Visibility.Hidden;
  101. }
  102. private void MicroQuality()
  103. {
  104. compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusMicro;
  105. CompressLargeStyle = Visibility.Hidden;
  106. CompressStandardStyle = Visibility.Hidden;
  107. CompressLittleStyle = Visibility.Hidden;
  108. CompressMicroStyle = Visibility.Visible;
  109. }
  110. private void ConfirmCompress()
  111. {
  112. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  113. }
  114. private int GetIndex(int pageindex)
  115. {
  116. Trace.WriteLine(pageindex);
  117. return 0;
  118. }
  119. private void Compress()
  120. {
  121. FolderBrowserDialog folderDialog = new FolderBrowserDialog();
  122. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  123. /*
  124. *设置这个对话框的起始保存路径
  125. */
  126. sfd.InitialDirectory = document.FilePath;
  127. /*
  128. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  129. */
  130. sfd.Filter = "PDF|*.pdf;";
  131. /*
  132. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  133. **/
  134. sfd.FileName = document.FileName + "_CompressFile.pdf";
  135. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  136. {
  137. /*
  138. * 做一些工作
  139. */
  140. Trace.WriteLine("compressDialogModel.CompressQuality: " + compressDialogModel.CompressQuality);
  141. DialogParameters value = new DialogParameters();
  142. value.Add(ParameterNames.PDFDocument, document);
  143. value.Add(ParameterNames.FilePath, sfd.FileName);
  144. value.Add("compressDialogModel.CompressQuality", (int)compressDialogModel.CompressQuality);
  145. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  146. dialogs.ShowDialog(DialogNames.CompressProgressBarDialog, value, e =>
  147. {
  148. });
  149. }
  150. else
  151. {
  152. MessageBox.Show("Cancel.");
  153. }
  154. }
  155. #endregion
  156. #region 框架行为
  157. public string Title => "";
  158. public event Action<IDialogResult> RequestClose;
  159. public bool CanCloseDialog()
  160. {
  161. return true;
  162. }
  163. public void OnDialogClosed()
  164. {
  165. }
  166. public void OnDialogOpened(IDialogParameters parameters)
  167. {
  168. CPDFDocument doc = null;
  169. parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
  170. if (doc != null)
  171. {
  172. CompressDialogModel compressdialogmodel = new CompressDialogModel();
  173. document = doc;
  174. compressDialogModel = compressdialogmodel;
  175. compressDialogModel.CompressQuality = CompressDialogModel.EnumQualityLevel.StatusDefault;
  176. }
  177. }
  178. #endregion
  179. }
  180. }