using ComPDFKit.PDFDocument; using ComPDFKitViewer.PdfViewer; using Microsoft.Office.Interop.Word; using PDF_Master.CustomControl; using PDF_Master.EventAggregators; using PDF_Master.Helper; using PDF_Master.Model; using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Windows; using System.Threading.Tasks; using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel; using Task = System.Threading.Tasks.Task; using PDF_Master.Model.Dialog.ToolsDialogs; using ComPDFKitViewer; namespace PDF_Master.ViewModels.Tools { public class ToolsBarContentViewModel : BindableBase, INavigationAware { #region 文案 private string T_setPassWord; public string T_SetPassWord { get { return T_setPassWord; } set { SetProperty(ref T_setPassWord, value); } } private string T_removePassword; public string T_RemovePassword { get { return T_removePassword; } set { SetProperty(ref T_removePassword, value); } } private void InitString() { T_SetPassWord = App.MainPageLoader.GetString("Security_SetPassword"); T_RemovePassword = App.MainPageLoader.GetString("Security_RemovePassword"); } #endregion private CPDFViewer PDFViewer; private ViewContentViewModel viewContentViewModel; public IDialogService dialogs; public CropPageUndoManager UndoManager = new CropPageUndoManager(); public IEventAggregator eventAggregator; List cropCurrentPageList = new List(); private CPDFDocument.GetPageIndexDelegate indexDelegate = null; public string unicode = null; /// /// 是否是第一次加载 /// private bool isFirstLoad = true; private string _pageIndex = "0"; ///// ///// 当前页 ///// //public string PageIndex //{ // get { return _pageIndex; } // set // { // SetProperty(ref _pageIndex, value); // App.mainWindowViewModel.Value = int.Parse(PageIndex); // } //} //private CPDFDocument tempDocument; //private IntPtr compressingIntpr = IntPtr.Zero; //private string _pageNumber = ""; ///// ///// 总页数 ///// //public string PageNumber //{ // get { return _pageNumber; } // set // { // SetProperty(ref _pageNumber, value); // App.mainWindowViewModel.MaxValue = int.Parse(PageNumber); // } //} public DelegateCommand CompressCommand { get; set; } public DelegateCommand MergeCommand { get; set; } public DelegateCommand SetPasswordCommand { get; set; } public DelegateCommand CancelPasswordCommand { get; set; } public DelegateCommand SetEditToolsCommand { get; set; } public DelegateCommand SetWatermarkCommand { get; set; } public DelegateCommand CropCurrentPageWMCommand { get; set; } public DelegateCommand CropAllPagesWMCommand { get; set; } public ToolsBarContentViewModel(IDialogService dialogService, IEventAggregator eventAggregator) { InitString(); dialogs = dialogService; this.eventAggregator = eventAggregator; unicode = App.mainWindowViewModel.SelectedItem.Unicode; CompressCommand = new DelegateCommand(OpenCompressDialog); MergeCommand = new DelegateCommand(MergeDialog); SetPasswordCommand = new DelegateCommand(OpenSetPasswordDialog); CancelPasswordCommand = new DelegateCommand(OpenCancelPasswordDialog); SetEditToolsCommand = new DelegateCommand(SetEditTools); CropCurrentPageWMCommand = new DelegateCommand(CropCurrentPageWM); CropAllPagesWMCommand = new DelegateCommand(CropAllPagesWM); } private void SetEditTools(object e) { var args = e as System.Windows.Controls.Button; if (args != null) { this.eventAggregator.GetEvent().Publish(new StringWithUnicode() { Unicode = unicode, EditToolsContentName = args.Name }); } } private void OpenCompressDialog() { DialogParameters value = new DialogParameters(); value.Add(ParameterNames.PDFViewer, PDFViewer); dialogs.ShowDialog(DialogNames.CompressDialog, value, e => { CompressDialogModel compressDialogModel = new CompressDialogModel(); compressDialogModel.OnOpened((Prism.Services.Dialogs.DialogResult)e); // OnOpened((Prism.Services.Dialogs.DialogResult)e); }); } //private int GetIndex(int pageindex) //{ // PageIndex = (pageindex + 1).ToString(); // App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible; // if (PageIndex == PageNumber) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; } // return 0; //} //private void CompressClear() //{ // tempDocument.CompressFile_Cancel(compressingIntpr); // App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; //} //public async void OnOpened(Prism.Services.Dialogs.DialogResult e) //{ // if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel) { return; } // CPDFDocument doc = 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) // { // 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); // App.mainWindowViewModel.ProcessCloseAction = CompressClear; // await Task.Run(() => { return document.CompressFile_Start(compressingIntpr, filepath); }); // CommonHelper.ShowFileBrowser(filepath); // document.Release(); // } //} private void MergeDialog() { DialogParameters value = new DialogParameters(); value.Add(ParameterNames.PDFViewer, PDFViewer); dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { }); } private void OpenSetPasswordDialog() { VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs); if (result.IsDiscryptied) { if (result.Password != null&&!string.IsNullOrEmpty(PDFViewer.Document.FilePath)) { //string filePath = PDFViewer.Document.FilePath; //PDFViewer.CloseDocument(); //PDFViewer.InitDocument(filePath); //if (PDFViewer.Document != null) //{ // PDFViewer.Load(); //} PDFViewer.Document.CheckOwnerPassword(result.Password); } DialogParameters value = new DialogParameters(); value.Add(ParameterNames.PDFViewer, this.PDFViewer); value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel); dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e => { if (e.Result == ButtonResult.OK) { SecurityHelper.IsPasswordChanged = true; if (viewContentViewModel.SecurityInfo.IsPasswordChanged) { viewContentViewModel.PDFViewer.UndoManager.CanSave = true; } this.eventAggregator.GetEvent().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode }); } }); } } private void OpenCancelPasswordDialog() { ///无可用安全性设置的原因: ///文件本身无安全性设置,且没有设置过密码 ///文件安全性已经被重置 /// if ((!PDFViewer.Document.IsEncrypted && !viewContentViewModel.SecurityInfo.IsPasswordChanged) || (viewContentViewModel.SecurityInfo.IsPasswordRemoved)) { MessageBoxEx.Show("No security settings available "); } else { VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs); if (result.IsDiscryptied) { if (result.Password != null) { PDFViewer.Document.CheckOwnerPassword(result.Password); } DialogParameters value = new DialogParameters(); value.Add(ParameterNames.PDFDocument, PDFViewer.Document); value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel); dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e => { if (e.Result == ButtonResult.OK) { if (viewContentViewModel.SecurityInfo.IsPasswordRemoved) { viewContentViewModel.PDFViewer.UndoManager.CanSave = true; } this.eventAggregator.GetEvent().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusRemoveSecuritySuccessfully, Unicode = unicode }); } }); } } } private async void CropCurrentPageWM() { viewContentViewModel.IsLoading = Visibility.Visible; await WaitCropCurrentPageWM(); viewContentViewModel.IsLoading = Visibility.Collapsed; } private async void CropAllPagesWM() { viewContentViewModel.IsLoading = Visibility.Visible; await WaitCropAllPagesWM(); viewContentViewModel.IsLoading = Visibility.Collapsed; //PDFViewer.UndoManager.AddHistory(); } private async Task WaitCropCurrentPageWM() { //付费锁 if (!App.IsLogin) { dialogs.ShowDialog(DialogNames.IAPCompareDialog); return; } await Task.Run(() => { if (PDFViewer.CurrentIndex >= 0) { UndoManager.cropPageList.Add(PDFViewer.CurrentIndex); } UndoManager.ADDCropCurrentPageWM(); PDFViewer?.SetCropMode(true, UndoManager.cropPageList); PDFViewer.UndoManager.AddHistory(UndoManager); PDFViewer.UndoManager.CanSave = true; }); } private async Task WaitCropAllPagesWM() { //付费锁 if (!App.IsLogin) { dialogs.ShowDialog(DialogNames.IAPCompareDialog); return; } await Task.Run(() => { List cropPagesList = new List(); for (int i = 0; i < PDFViewer.Document.PageCount; i++) { cropPagesList.Add(i); } UndoManager.cropPageList.AddRange(cropPagesList); UndoManager.ADDCropAllPagesWM(); PDFViewer?.SetCropMode(true, UndoManager.cropPageList); PDFViewer.UndoManager.AddHistory(UndoManager); PDFViewer.UndoManager.CanSave = true; }); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.ViewContentViewModel, out viewContentViewModel); navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); if (PDFViewer != null) { UndoManager.setPDFViewer(PDFViewer); //DFViewer.UndoManager.AddHistory(UndoManager); isFirstLoad = false; } } } }