CompressDialogModel.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using ComPDFKit.PDFDocument;
  2. using Microsoft.Office.Interop.Word;
  3. using PDF_Office.Helper;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Task = System.Threading.Tasks.Task;
  11. namespace PDF_Office.Model.Dialog.ToolsDialogs
  12. {
  13. public class CompressDialogModel
  14. {
  15. CPDFDocument tempDocument = null;
  16. IntPtr compressingIntpr = IntPtr.Zero;
  17. public enum EnumQualityLevel {
  18. StatusLarge=100,
  19. StatusStandard=80,
  20. StatusDefault=40,
  21. StatusLittle =40,
  22. StatusMicro=10
  23. }
  24. public EnumQualityLevel CompressQuality { get; set; }
  25. public static string PageIndex { get; set; }
  26. public void CompressClear()
  27. {
  28. tempDocument.CompressFile_Cancel(compressingIntpr);
  29. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  30. }
  31. private int GetIndex(int pageindex)
  32. {
  33. App.mainWindowViewModel.Value = (pageindex + 1);
  34. if (App.mainWindowViewModel.Value == App.mainWindowViewModel.MaxValue) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  35. return 0;
  36. }
  37. public async void OnOpened(Prism.Services.Dialogs.DialogResult e)
  38. {
  39. if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel||e.Result==Prism.Services.Dialogs.ButtonResult.None) { return; }
  40. CPDFDocument doc = null;
  41. CPDFDocument.GetPageIndexDelegate indexDelegate = null;
  42. string filepath = "";
  43. string password = "";
  44. int compressquality = 45;
  45. doc = e.Parameters.GetValue<CPDFDocument>(ParameterNames.PDFDocument);
  46. filepath = e.Parameters.GetValue<string>(ParameterNames.FilePath);
  47. password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
  48. compressquality = e.Parameters.GetValue<int>("compressDialogModel.CompressQuality");
  49. if (doc != null)
  50. {
  51. App.mainWindowViewModel.Value = 0;
  52. App.mainWindowViewModel.ProgressTitle = "Compressing";
  53. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible;
  54. App.mainWindowViewModel.MaxValue = doc.PageCount;
  55. CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath);
  56. document.UnlockWithPassword(password);
  57. indexDelegate += GetIndex;
  58. compressingIntpr = document.CompressFile_Init(compressquality, indexDelegate);
  59. //GC.KeepAlive(indexDelegate);
  60. tempDocument = document;
  61. Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
  62. App.mainWindowViewModel.ProcessCloseAction = CompressClear;
  63. if (await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); })) { CommonHelper.ShowFileBrowser(filepath); }
  64. document.Release();
  65. }
  66. }
  67. }
  68. }