CompressDialogViewModel.cs 7.4 KB

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