123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using PDF_Office.Model.Dialog.ToolsDialogs;
- using ComPDFKit.PDFDocument;
- using PDF_Office.Model;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using PDF_Office.Helper;
- namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.CompressDialogs
- {
- public class CompresProgressBarDialogViewModel : BindableBase, IDialogAware
- {
- #region 参数和属性
- private IntPtr compressingIntpr = IntPtr.Zero;
- private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
- private CPDFDocument tempDocument;
- private string _pageIndex = "0";
- /// <value>
- /// 当前页
- /// </value>
- public string PageIndex
- {
- get { return _pageIndex; }
- set
- {
- SetProperty(ref _pageIndex, value);
- }
- }
- private string _pageNumber = "";
- /// <value>
- /// 总页数
- /// </value>
- public string PageNumber
- {
- get { return _pageNumber; }
- set
- {
- SetProperty(ref _pageNumber, value);
- }
- }
- #endregion
- #region 逻辑函数
- private int GetIndex(int pageindex)
- {
- PageIndex = pageindex.ToString();
- return 0;
- }
- #endregion
- #region 框架行为
- public string Title => "";
- public event Action<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- tempDocument.CompressFile_Cancel(compressingIntpr);
- }
- public async void OnDialogOpened(IDialogParameters parameters)
- {
- CPDFDocument doc = null;
- string filepath = "";
- string password = "";
- int compressquality = 45;
- parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
- parameters.TryGetValue<string>(ParameterNames.FilePath, out filepath);
- parameters.TryGetValue<string>(ParameterNames.PassWord, out password);
- parameters.TryGetValue<int>("compressDialogModel.CompressQuality", out compressquality);
- if (doc != null)
- {
- PageNumber = doc.PageCount.ToString();
- 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);
- await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); });
- CommonHelper.ShowFileBrowser(filepath);
- RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
- document.Release();
- }
- }
- #endregion
- }
- }
|