CompressDialogModel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using ComPDFKit.PDFDocument;
  2. using Microsoft.Office.Interop.Word;
  3. using PDF_Master.CustomControl;
  4. using PDF_Master.Helper;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Task = System.Threading.Tasks.Task;
  13. namespace PDF_Master.Model.Dialog.ToolsDialogs
  14. {
  15. public class CompressDialogModel
  16. {
  17. CPDFDocument tempDocument = null;
  18. private IntPtr compressingIntpr = IntPtr.Zero;
  19. //委托函数为全局变量
  20. private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
  21. public enum EnumQualityLevel
  22. {
  23. StatusLarge = 100,
  24. StatusStandard = 80,
  25. StatusDefault = 40,
  26. StatusLittle = 40,
  27. StatusMicro = 10
  28. }
  29. public EnumQualityLevel CompressQuality { get; set; }
  30. public static string PageIndex { get; set; }
  31. public void CompressClear()
  32. {
  33. if (!compressingIntpr.Equals(IntPtr.Zero))
  34. {
  35. tempDocument.CompressFile_Cancel(compressingIntpr);
  36. }
  37. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  38. }
  39. private int GetIndex(int pageindex)
  40. {
  41. try
  42. {
  43. App.mainWindowViewModel.Value = (pageindex + 1);
  44. if (App.mainWindowViewModel.Value == App.mainWindowViewModel.MaxValue) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  45. return 0;
  46. }
  47. catch { Trace.WriteLine("g了嘛"); return -1; }
  48. }
  49. public async void OnOpened(Prism.Services.Dialogs.DialogResult e)
  50. {
  51. string filepath = "";
  52. string password = "";
  53. try
  54. {
  55. if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel || e.Result == Prism.Services.Dialogs.ButtonResult.None) { return; }
  56. CPDFDocument doc = null;
  57. compressingIntpr = IntPtr.Zero;
  58. int compressquality = 45;
  59. doc = e.Parameters.GetValue<CPDFDocument>(ParameterNames.PDFDocument);
  60. filepath = e.Parameters.GetValue<string>(ParameterNames.FilePath);
  61. try { if (File.Exists(filepath)) File.Delete(filepath); }
  62. catch
  63. {
  64. AlertsMessage alertsMessage = new AlertsMessage();
  65. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("FileNotExistWarning"), App.ServiceLoader.GetString("Text_ok"));
  66. return;
  67. }
  68. password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
  69. compressquality = e.Parameters.GetValue<int>("compressDialogModel.CompressQuality");
  70. if (doc != null)
  71. {
  72. App.mainWindowViewModel.Value = 0;
  73. App.mainWindowViewModel.ProgressTitle = "Compressing";
  74. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible;
  75. App.mainWindowViewModel.MaxValue = doc.PageCount;
  76. CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath);
  77. document.UnlockWithPassword(password);
  78. indexDelegate += GetIndex;
  79. compressingIntpr = document.CompressFile_Init((float)compressquality, indexDelegate);
  80. GC.KeepAlive(indexDelegate);//关键 防止委托过早回收
  81. tempDocument = document;
  82. Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
  83. App.mainWindowViewModel.ProcessCloseAction = CompressClear;
  84. Trace.WriteLine("怎么啦");
  85. if (await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); }))
  86. {
  87. CommonHelper.ShowFileBrowser(filepath);
  88. }
  89. Trace.WriteLine("零零零零怎么");
  90. document.Release();
  91. }
  92. }
  93. catch
  94. {
  95. AlertsMessage alertsMessage = new AlertsMessage();
  96. alertsMessage.ShowDialog("", "Compress file error", App.ServiceLoader.GetString("Text_ok"));
  97. tempDocument.CompressFile_Cancel(compressingIntpr);
  98. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  99. }
  100. }
  101. }
  102. }