123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using ComPDFKit.PDFDocument;
- using Microsoft.Office.Interop.Word;
- using PDF_Master.CustomControl;
- using PDF_Master.Helper;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Task = System.Threading.Tasks.Task;
- namespace PDF_Master.Model.Dialog.ToolsDialogs
- {
- public class CompressDialogModel
- {
- CPDFDocument tempDocument = null;
- private IntPtr compressingIntpr = IntPtr.Zero;
- //委托函数为全局变量
- private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
- 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()
- {
- if (!compressingIntpr.Equals(IntPtr.Zero))
- {
- tempDocument.CompressFile_Cancel(compressingIntpr);
- }
- App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
- }
- private int GetIndex(int pageindex)
- {
- try
- {
- App.mainWindowViewModel.Value = (pageindex + 1);
- if (App.mainWindowViewModel.Value == App.mainWindowViewModel.MaxValue) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
- return 0;
- }
- catch { Trace.WriteLine("g了嘛"); return -1; }
- }
- public async void OnOpened(Prism.Services.Dialogs.DialogResult e)
- {
- string filepath = "";
- string password = "";
- try
- {
- if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel || e.Result == Prism.Services.Dialogs.ButtonResult.None) { return; }
- CPDFDocument doc = null;
- compressingIntpr = IntPtr.Zero;
- int compressquality = 45;
- doc = e.Parameters.GetValue<CPDFDocument>(ParameterNames.PDFDocument);
- filepath = e.Parameters.GetValue<string>(ParameterNames.FilePath);
- try { if (File.Exists(filepath)) File.Delete(filepath); }
- catch
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("FileNotExistWarning"), App.ServiceLoader.GetString("Text_ok"));
- return;
- }
- password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
- compressquality = e.Parameters.GetValue<int>("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((float)compressquality, indexDelegate);
- GC.KeepAlive(indexDelegate);//关键 防止委托过早回收
- tempDocument = document;
- Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
- App.mainWindowViewModel.ProcessCloseAction = CompressClear;
- Trace.WriteLine("怎么啦");
- if (await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); }))
- {
- CommonHelper.ShowFileBrowser(filepath);
- }
- Trace.WriteLine("零零零零怎么");
- document.Release();
- }
- }
- catch
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", "Compress file error", App.ServiceLoader.GetString("Text_ok"));
- tempDocument.CompressFile_Cancel(compressingIntpr);
- App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
- }
- }
- }
- }
|