123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- 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;
- using PDF_Master.Properties;
- using PDFReader_WPF.Helper;
- 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 string T_compress;
- public string T_Compress
- {
- get { return T_compress; }
- set
- {
- SetProperty(ref T_compress, value);
- }
- }
- private string T_security;
- public string T_Security
- {
- get { return T_security; }
- set
- {
- SetProperty(ref T_security, value);
- }
- }
- private string T_merge;
- public string T_Merge
- {
- get { return T_merge; }
- set
- {
- SetProperty(ref T_merge, value);
- }
- }
- private string T_crop;
- public string T_Crop
- {
- get { return T_crop; }
- set
- {
- SetProperty(ref T_crop, value);
- }
- }
- private void InitString()
- {
- T_SetPassWord = App.MainPageLoader.GetString("Security_SetPassword");
- T_RemovePassword = App.MainPageLoader.GetString("Security_RemovePassword");
- T_Compress = App.MainPageLoader.GetString("ToolsBar_Compress");
- T_Security = App.MainPageLoader.GetString("ToolsBar_Security");
- T_Merge = App.MainPageLoader.GetString("ToolsBar_Merge");
- T_Crop = App.MainPageLoader.GetString("ToolsBar_Crop");
- }
- #endregion
- private CPDFViewer PDFViewer;
- private ViewContentViewModel viewContentViewModel;
- public IDialogService dialogs;
- public CropPageUndoManager UndoManager = new CropPageUndoManager();
- public IEventAggregator eventAggregator;
- List<int> cropCurrentPageList = new List<int>();
- public string unicode = null;
- ///// <value>
- ///// 当前页
- ///// </value>
- //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 = "";
- ///// <value>
- ///// 总页数
- ///// </value>
- //public string PageNumber
- //{
- // get { return _pageNumber; }
- // set
- // {
- // SetProperty(ref _pageNumber, value);
- // App.mainWindowViewModel.MaxValue = int.Parse(PageNumber);
- // }
- //}
- /// <summary>
- /// 防止自动保存异步导致弹窗出现两次
- /// </summary>
- private bool _isPopupInCompressProgress;
- public bool IsPopupInCompressProgress
- {
- get { return _isPopupInCompressProgress; }
- set { SetProperty(ref _isPopupInCompressProgress, value); }
- }
- /// <summary>
- /// 防止自动保存异步导致弹窗出现两次
- /// </summary>
- private bool _isPopupInMergeProgress;
- public bool IsPopupInMergeProgress
- {
- get { return _isPopupInMergeProgress; }
- set { SetProperty(ref _isPopupInMergeProgress, value); }
- }
- public DelegateCommand CompressCommand { get; set; }
- public DelegateCommand MergeCommand { get; set; }
- public DelegateCommand SetPasswordCommand { get; set; }
- public DelegateCommand CancelPasswordCommand { get; set; }
- public DelegateCommand<object> 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<object>(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<EnterSelectedEditToolEvent>().Publish(new StringWithUnicode() { Unicode = unicode, EditToolsContentName = args.Name });
- }
- }
- private async void OpenCompressDialog()
- {
- if (IsPopupInCompressProgress)
- {
- return;
- }
- IsPopupInCompressProgress = true;
- DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
- DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Compress);
- DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_Compress");
- if (!ServiceHelper.IAPBeforeFunction())
- {
- DataTrackingHelper.EntryPath = "";
- IsPopupInCompressProgress = false;
- return;
- }
- if (await viewContentViewModel.SaveBeforeOption())
- {
- IsPopupInCompressProgress = false;
- 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);
- });
- }
- IsPopupInCompressProgress = false;
- }
- //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<CPDFDocument>(ParameterNames.PDFDocument);
- // filepath = e.Parameters.GetValue<string>(ParameterNames.FilePath);
- // password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
- // compressquality = e.Parameters.GetValue<int>("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<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); });
- // CommonHelper.ShowFileBrowser(filepath);
- // document.Release();
- // }
- //}
- private async void MergeDialog()
- {
- if (IsPopupInMergeProgress)
- {
- return;
- }
- IsPopupInMergeProgress = true;
- DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
- DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Merge);
- DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_Merge");
- if (!ServiceHelper.IAPBeforeFunction())
- {
- DataTrackingHelper.EntryPath = "";
- IsPopupInMergeProgress = false;
- return;
- }
- if (await viewContentViewModel.SaveBeforeOption())
- {
- IsPopupInMergeProgress = false;
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.PDFViewer, PDFViewer);
- value.Add(ParameterNames.FilePath, PDFViewer.Document.FilePath);
- value.Add(ParameterNames.Unicode, unicode);
- dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
- }
- IsPopupInMergeProgress = false;
- }
- private void OpenSetPasswordDialog()
- {
- DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
- DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.SetPassword);
- DataTrackingHelper.IsClearEntryPath = true;
- DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_SetPassword");
- if (!ServiceHelper.IAPBeforeFunction())
- {
- return;
- }
- VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
- if (result.IsDiscryptied)
- {
- if (result.Password != null)
- {
- PDFViewer.Document.CheckOwnerPassword(result.Password);
- }
- this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
- 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<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
- }
- });
- }
- }
- private void OpenCancelPasswordDialog()
- {
- DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
- DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.RemovePassword);
- DataTrackingHelper.IsClearEntryPath = true;
- DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_RemovePassword");
- ///无可用安全性设置的原因:
- ///文件本身无安全性设置,且没有设置过密码
- ///文件安全性已经被重置
- ///
- if ((!PDFViewer.Document.IsEncrypted && !viewContentViewModel.SecurityInfo.IsPasswordChanged) || (viewContentViewModel.SecurityInfo.IsPasswordRemoved))
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("NoPasswrod_Waring"), App.ServiceLoader.GetString("Text_ok"));
- }
- else
- {
- if (!ServiceHelper.IAPBeforeFunction())
- {
- return;
- }
- VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
- if (result.IsDiscryptied)
- {
- if (result.Password != null)
- {
- PDFViewer.Document.CheckOwnerPassword(result.Password);
- }
- this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
- 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<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusRemoveSecuritySuccessfully, Unicode = unicode });
- }
- });
- }
- }
- }
- private void CropCurrentPageWM()
- {
- DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
- DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Crop);
- DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_Crop");
- viewContentViewModel.IsLoading = Visibility.Visible;
- WaitCropCurrentPageWM();
- viewContentViewModel.IsLoading = Visibility.Collapsed;
- DataTrackingHelper.EntryPath = "";
- }
- private void CropAllPagesWM()
- {
- DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
- DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Crop);
- DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_Crop");
- viewContentViewModel.IsLoading = Visibility.Visible;
- WaitCropAllPagesWM();
- viewContentViewModel.IsLoading = Visibility.Collapsed;
- DataTrackingHelper.EntryPath = "";
- //PDFViewer.UndoManager.AddHistory();
- }
- private void WaitCropCurrentPageWM()
- {
- if (!ServiceHelper.IAPBeforeFunction())
- {
- return;
- }
- App.Current.Dispatcher.Invoke(new Action(() => {
- if (PDFViewer.CurrentIndex >= 0)
- {
- UndoManager.cropPageList.Add(PDFViewer.CurrentIndex);
- }
- UndoManager.ADDCropCurrentPageWM();
- //此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
- PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
- PDFViewer.UndoManager.AddHistory(UndoManager);
- PDFViewer.UndoManager.CanSave = true;
- }));
- //5.22 SDK更新后剪切线程异常
- // await Task.Run(() =>
- //{
- // if (PDFViewer.CurrentIndex >= 0)
- // {
- // UndoManager.cropPageList.Add(PDFViewer.CurrentIndex);
- // }
- // UndoManager.ADDCropCurrentPageWM();
- // //此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
- // PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
- // PDFViewer.UndoManager.AddHistory(UndoManager);
- // PDFViewer.UndoManager.CanSave = true;
- //});
- }
- private void WaitCropAllPagesWM()
- {
- if (!ServiceHelper.IAPBeforeFunction())
- {
- return;
- }
- App.Current.Dispatcher.Invoke(new Action(() => {
- List<int> cropPagesList = new List<int>();
- for (int i = 0; i < PDFViewer.Document.PageCount; i++)
- {
- cropPagesList.Add(i);
- }
- UndoManager.cropPageList.AddRange(cropPagesList);
- UndoManager.ADDCropAllPagesWM();
- //此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
- PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
- PDFViewer.UndoManager.AddHistory(UndoManager);
- PDFViewer.UndoManager.CanSave = true;
- }));
- //5.22 SDK更新后剪切线程异常
- // await Task.Run(() =>
- //{
- //List<int> cropPagesList = new List<int>();
- //for (int i = 0; i < PDFViewer.Document.PageCount; i++)
- //{
- // cropPagesList.Add(i);
- //}
- //UndoManager.cropPageList.AddRange(cropPagesList);
- //UndoManager.ADDCropAllPagesWM();
- ////此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
- //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<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- if (PDFViewer != null)
- {
- UndoManager.setPDFViewer(PDFViewer);
- //DFViewer.UndoManager.AddHistory(UndoManager);
- }
- }
- }
- }
|