123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- using ComPDFKit.PDFDocument;
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Office.Interop.Word;
- using PDF_Office.CustomControl;
- using PDF_Office.EventAggregators;
- using PDF_Office.Helper;
- using PDF_Office.Model;
- using PDF_Office.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_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
- using Task = System.Threading.Tasks.Task;
- namespace PDF_Office.ViewModels.Tools
- {
- public class ToolsBarContentViewModel : BindableBase, INavigationAware
- {
- private CPDFViewer PDFViewer;
- private ViewContentViewModel viewContentViewModel;
- public IDialogService dialogs;
- public IEventAggregator eventAggregator;
- private IntPtr compressingIntpr = IntPtr.Zero;
- private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
- private CPDFDocument tempDocument;
- public string unicode = null;
- /// <summary>
- /// 是否是第一次加载
- /// </summary>
- private bool isFirstLoad = true;
- private string _pageIndex = "0";
- /// <value>
- /// 当前页
- /// </value>
- public string PageIndex
- {
- get { return _pageIndex; }
- set
- {
- SetProperty(ref _pageIndex, value);
- App.mainWindowViewModel.Value = int.Parse(PageIndex);
- }
- }
- private string _pageNumber = "";
- /// <value>
- /// 总页数
- /// </value>
- 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<object> SetEditToolsCommand { get; set; }
- public DelegateCommand SetWatermarkCommand { get; set; }
- public ToolsBarContentViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
- {
- 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);
- }
- 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 void OpenCompressDialog()
- {
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.PDFViewer, PDFViewer);
- dialogs.ShowDialog(DialogNames.CompressDialog, value, 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<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 void MergeDialog()
- {
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.PDFViewer, PDFViewer);
- dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
- }
- private void OpenSetPasswordDialog()
- {
- if (!viewContentViewModel.SecurityInfo.IsPasswordChanged)
- {
- VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
- if (result.IsDiscryptied)
- {
- if (result.Password != null)
- {
- string filePath = PDFViewer.Document.FilePath;
- PDFViewer.Document.Release();
- PDFViewer.InitDocument(filePath);
- PDFViewer.Document.UnlockWithPassword(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<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
- }
- });
- }
- private void OpenCancelPasswordDialog()
- {
- if (!PDFViewer.Document.IsEncrypted)
- {
- MessageBoxEx.Show("No security settings available ");
- }
- else
- {
- VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
- if (result.IsDiscryptied)
- {
- if (result.Password != null)
- {
- string filePath = PDFViewer.Document.FilePath;
- PDFViewer.Document.Release();
- PDFViewer.InitDocument(filePath);
- PDFViewer.Document.UnlockWithPassword(result.Password);
- }
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
- dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e => { });
- }
- }
- }
- 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)
- {
- isFirstLoad = false;
- }
- }
- }
- }
|