CompressProgressBarDialogViewModel.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using PDF_Office.Model.Dialog.ToolsDialogs;
  2. using ComPDFKit.PDFDocument;
  3. using PDF_Office.Model;
  4. using Prism.Mvvm;
  5. using Prism.Services.Dialogs;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using PDF_Office.Helper;
  13. namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.CompressDialogs
  14. {
  15. public class CompresProgressBarDialogViewModel : BindableBase, IDialogAware
  16. {
  17. #region 参数和属性
  18. private IntPtr compressingIntpr = IntPtr.Zero;
  19. private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
  20. private CPDFDocument tempDocument;
  21. private string _pageIndex = "0";
  22. /// <value>
  23. /// 当前页
  24. /// </value>
  25. public string PageIndex
  26. {
  27. get { return _pageIndex; }
  28. set
  29. {
  30. SetProperty(ref _pageIndex, value);
  31. }
  32. }
  33. private string _pageNumber = "";
  34. /// <value>
  35. /// 总页数
  36. /// </value>
  37. public string PageNumber
  38. {
  39. get { return _pageNumber; }
  40. set
  41. {
  42. SetProperty(ref _pageNumber, value);
  43. }
  44. }
  45. #endregion
  46. #region 逻辑函数
  47. private int GetIndex(int pageindex)
  48. {
  49. PageIndex = pageindex.ToString();
  50. return 0;
  51. }
  52. #endregion
  53. #region 框架行为
  54. public string Title => "";
  55. public event Action<IDialogResult> RequestClose;
  56. public bool CanCloseDialog()
  57. {
  58. return true;
  59. }
  60. public void OnDialogClosed()
  61. {
  62. tempDocument.CompressFile_Cancel(compressingIntpr);
  63. }
  64. public async void OnDialogOpened(IDialogParameters parameters)
  65. {
  66. CPDFDocument doc = null;
  67. string filepath = "";
  68. string password = "";
  69. int compressquality = 45;
  70. parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
  71. parameters.TryGetValue<string>(ParameterNames.FilePath, out filepath);
  72. parameters.TryGetValue<string>(ParameterNames.PassWord, out password);
  73. parameters.TryGetValue<int>("compressDialogModel.CompressQuality", out compressquality);
  74. if (doc != null)
  75. {
  76. PageNumber = doc.PageCount.ToString();
  77. CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath);
  78. document.UnlockWithPassword(password);
  79. indexDelegate += GetIndex;
  80. compressingIntpr = document.CompressFile_Init(compressquality, indexDelegate);
  81. //GC.KeepAlive(indexDelegate);
  82. tempDocument = document;
  83. Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
  84. await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); });
  85. CommonHelper.ShowFileBrowser(filepath);
  86. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  87. document.Release();
  88. }
  89. }
  90. #endregion
  91. }
  92. }