using ComPDFKit.PDFDocument; using Microsoft.Office.Interop.Word; using PDF_Office.Helper; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using Task = System.Threading.Tasks.Task; namespace PDF_Office.Model.Dialog.ToolsDialogs { public class CompressDialogModel { CPDFDocument tempDocument = null; IntPtr compressingIntpr = IntPtr.Zero; public enum EnumQualityLevel { StatusLarge=100, StatusStandard=80, StatusDefault=40, StatusLittle =40, StatusMicro=10 } public EnumQualityLevel CompressQuality { get; set; } public static string PageIndex { get; set; } public void CompressClear() { tempDocument.CompressFile_Cancel(compressingIntpr); App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; } private int GetIndex(int pageindex) { App.mainWindowViewModel.Value = (pageindex + 1); if (App.mainWindowViewModel.Value == App.mainWindowViewModel.MaxValue) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; } return 0; } public async void OnOpened(Prism.Services.Dialogs.DialogResult e) { if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel||e.Result==Prism.Services.Dialogs.ButtonResult.None) { return; } CPDFDocument doc = null; CPDFDocument.GetPageIndexDelegate indexDelegate = null; string filepath = ""; string password = ""; int compressquality = 45; doc = e.Parameters.GetValue(ParameterNames.PDFDocument); filepath = e.Parameters.GetValue(ParameterNames.FilePath); password = e.Parameters.GetValue(ParameterNames.PassWord); compressquality = e.Parameters.GetValue("compressDialogModel.CompressQuality"); if (doc != null) { App.mainWindowViewModel.Value = 0; App.mainWindowViewModel.ProgressTitle = "Compressing"; App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible; App.mainWindowViewModel.MaxValue = doc.PageCount; CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath); document.UnlockWithPassword(password); indexDelegate += GetIndex; compressingIntpr = document.CompressFile_Init(compressquality, indexDelegate); //GC.KeepAlive(indexDelegate); tempDocument = document; Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality); App.mainWindowViewModel.ProcessCloseAction = CompressClear; if (await Task.Run(() => { return document.CompressFile_Start(compressingIntpr, filepath); })) { CommonHelper.ShowFileBrowser(filepath); } document.Release(); } } } }