using ComPDFKit.PDFDocument; using PDF_Office.Model; using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Windows.Forms; using Forms = System.Windows.Forms; namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs { public class SetPasswordDialogViewModel : BindableBase, IDialogAware { #region 参数和属性 private SetPasswordDialogModel setPasswordDialogModel = new SetPasswordDialogModel(); private CPDFDocument document; private bool _canOpen; public bool CanOpen { get { return _canOpen; } set { _canOpen = value; RaisePropertyChanged("isChecked"); } } private bool _canEdit; public bool CanEdit { get { return _canEdit; } set { _canEdit = value; RaisePropertyChanged("isChecked"); } } private string _changeMod = "0"; /// ///"0"为ChangeMod.None; ///"1"为ChangeMod.ChangePage; ///"2"为ChangeMod.FormAndSignature; ///"3"为ChangeMod.AnnotAndFormAndSignature; ///"4"为ChangeMod.ExceptAbstrat; /// public string ChangeMod { get { return _changeMod; } set { _changeMod = value; } } private string _printMod = "0"; /// ///"0"为PrintMod.None; ///"1"为PrintMod.LowDpi; ///"2"为PrintMod.HighDpi; /// public string PrintMod { get { return _printMod; } set { _printMod = value; } } private string _isEnableConfirm = "False"; public string IsEnabledConfirm { get { return _isEnableConfirm; } set { SetProperty(ref _isEnableConfirm, value); } } #endregion #region 委托声明 public DelegateCommand DelegateSetOpenCommand { get; set; } public DelegateCommand DelegateSetEditCommand { get; set; } public DelegateCommand DelegateCanOpenTextChangedCommand { get; set; } public DelegateCommand DelegateCanEditTextChangedCommand { get; set; } public DelegateCommand DelegateConfirmEncryptCommand { get; set; } public DelegateCommand DelegateCancelEncryptCommand { get; set; } #endregion public SetPasswordDialogViewModel() { SetPasswordDialogModel.PasswordForOpen = ""; SetPasswordDialogModel.PasswordForEdit = ""; DelegateConfirmEncryptCommand = new DelegateCommand(ConfirmEncrypt); DelegateCancelEncryptCommand = new DelegateCommand(CancelEncrypt); DelegateSetOpenCommand = new DelegateCommand(SetCanOpen); DelegateSetEditCommand = new DelegateCommand(SetCanEdit); DelegateCanOpenTextChangedCommand = new DelegateCommand(CanOpenTextChanged); DelegateCanEditTextChangedCommand = new DelegateCommand(CanEditTextChanged); } #region 检查和初始化 private void CheckCanConfirmEncrypt() { if (setPasswordDialogModel.CanOpen) { if (SetPasswordDialogModel.PasswordForOpen.Length > 0) { IsEnabledConfirm = "True"; return; } } if (setPasswordDialogModel.CanEdit) { if (SetPasswordDialogModel.PasswordForEdit.Length > 0) { IsEnabledConfirm = "True"; return; } } IsEnabledConfirm = "False"; } private void InitPermissionsDictionary(ref Dictionary GetPrintMod, ref Dictionary GetChangeMod) { GetPrintMod.Add("0", SetPasswordDialogModel.PrintMod.None); GetPrintMod.Add("1", SetPasswordDialogModel.PrintMod.LowDpi); GetPrintMod.Add("2", SetPasswordDialogModel.PrintMod.HighDpi); GetChangeMod.Add("0", SetPasswordDialogModel.ChangeMod.None); GetChangeMod.Add("1", SetPasswordDialogModel.ChangeMod.ChangePage); GetChangeMod.Add("2", SetPasswordDialogModel.ChangeMod.FormAndSignature); GetChangeMod.Add("3", SetPasswordDialogModel.ChangeMod.AnnotAndFormAndSignature); GetChangeMod.Add("4", SetPasswordDialogModel.ChangeMod.ExceptAbstrat); } #endregion #region 逻辑函数 public void SetCanOpen() { setPasswordDialogModel.CanOpen = CanOpen; CheckCanConfirmEncrypt(); } public void SetCanEdit() { setPasswordDialogModel.CanEdit = CanEdit; CheckCanConfirmEncrypt(); } public void CanOpenTextChanged() { CheckCanConfirmEncrypt(); } public void CanEditTextChanged() { CheckCanConfirmEncrypt(); } public void ConfirmEncrypt() { string openPassword = ""; string editPassword = ""; FolderBrowserDialog folderDialog = new FolderBrowserDialog(); Forms.SaveFileDialog sfd = new Forms.SaveFileDialog(); /* *设置这个对话框的起始保存路径 */ sfd.InitialDirectory = document.FilePath; /* *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;” */ sfd.Filter = "PDF|*.pdf;"; /* *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮 **/ sfd.FileName = document.FileName + "_SetPassword.pdf"; if (sfd.ShowDialog() == Forms.DialogResult.OK) { /* * 做一些工作 */ CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo(); if (setPasswordDialogModel.CanOpen) { if (!string.IsNullOrEmpty(SetPasswordDialogModel.PasswordForOpen)) { openPassword = SetPasswordDialogModel.PasswordForOpen; } } if (setPasswordDialogModel.CanEdit) { if (!string.IsNullOrEmpty(SetPasswordDialogModel.PasswordForEdit)) { editPassword = SetPasswordDialogModel.PasswordForEdit; Dictionary GetPrintMod = new Dictionary(); Dictionary GetChangeMod = new Dictionary(); ; InitPermissionsDictionary(ref GetPrintMod, ref GetChangeMod); permissionsInfo = setPasswordDialogModel.CreatePermissionsInfo(GetPrintMod[PrintMod], GetChangeMod[ChangeMod]); } } document.Encrypt(openPassword, editPassword, permissionsInfo); document.WriteToFilePath(sfd.FileName); MessageBox.Show(sfd.FileName + " 保存成功"); RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel)); } } public void CancelEncrypt() => RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel)); #endregion #region 框架相关 public string Title => ""; public event Action RequestClose; public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { CPDFDocument doc = null; parameters.TryGetValue(ParameterNames.PDFDocument, out doc); if (doc != null) { document = doc; } } #endregion } }