CompressDialogViewModel.cs 6.8 KB

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