123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- 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";
- ///<value>
- ///"0"为ChangeMod.None;
- ///"1"为ChangeMod.ChangePage;
- ///"2"为ChangeMod.FormAndSignature;
- ///"3"为ChangeMod.AnnotAndFormAndSignature;
- ///"4"为ChangeMod.ExceptAbstrat;
- ///</value>
- public string ChangeMod
- {
- get { return _changeMod; }
- set
- {
- _changeMod = value;
- }
- }
- private string _printMod = "0";
- ///<value>
- ///"0"为PrintMod.None;
- ///"1"为PrintMod.LowDpi;
- ///"2"为PrintMod.HighDpi;
- ///</value>
- 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<string, SetPasswordDialogModel.PrintMod> GetPrintMod, ref Dictionary<string, SetPasswordDialogModel.ChangeMod> 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<string, SetPasswordDialogModel.PrintMod> GetPrintMod = new Dictionary<string, SetPasswordDialogModel.PrintMod>();
- Dictionary<string, SetPasswordDialogModel.ChangeMod> GetChangeMod = new Dictionary<string, SetPasswordDialogModel.ChangeMod>(); ;
- 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<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- CPDFDocument doc = null;
- parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
- if (doc != null)
- {
- document = doc;
- }
- }
- #endregion
- }
- }
|