123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using ComPDFKit.PDFDocument;
- using Microsoft.Xaml.Behaviors.Layout;
- using PDF_Office.Model;
- using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
- using PDF_Office.Views.HomePanel.RecentFiles;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Windows.Controls;
- using System.Windows.Forms;
- using Forms = System.Windows.Forms;
- namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
- {
- public class SetPasswordDialogViewModel : BindableBase, IDialogAware
- {
- private SetPasswordDialogModel setPasswordDialogModel = new SetPasswordDialogModel();
- private CPDFDocument document;
- 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; }
- public DelegateCommand InputPasswordCommand { get; set; }
-
- 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";
- public string ChangeMod
- {
- get { return _changeMod; }
- set
- {
- _changeMod = value;
- }
- }
- private string _printMod="0";
- public string PrintMod
- {
- get { return _printMod; }
- set
- {
- _printMod = value;
- }
- }
- private string _isEnableConfirm = "False";
- public string IsEnabledConfirm
- {
- get { return _isEnableConfirm; }
- set
- {
- SetProperty(ref _isEnableConfirm, value);
- }
- }
- public SetPasswordDialogViewModel()
- {
- SetPasswordDialogModel.CanOpenPassword = "";
- SetPasswordDialogModel.CanEditPassword = "";
- DelegateConfirmEncryptCommand = new DelegateCommand(ConfirmEncrypt);
- DelegateCancelEncryptCommand = new DelegateCommand(CancelEncrypt);
- DelegateSetOpenCommand = new DelegateCommand(SetCanOpen);
- DelegateSetEditCommand = new DelegateCommand(SetCanEdit);
- DelegateCanOpenTextChangedCommand = new DelegateCommand(CanOpenTextChanged);
- DelegateCanEditTextChangedCommand = new DelegateCommand(CanEditTextChanged);
- }
- private void CheckCanConfirmEncrypt()
- {
- if (setPasswordDialogModel.CanOpen)
- {
- if (SetPasswordDialogModel.CanOpenPassword.Length > 0)
- {
- IsEnabledConfirm = "True";
- return;
- }
- }
- if (setPasswordDialogModel.CanEdit)
- {
- if (SetPasswordDialogModel.CanEditPassword.Length > 0)
- {
- IsEnabledConfirm = "True";
- return;
- }
- }
- IsEnabledConfirm = "False";
- }
- public void SetCanOpen()
- {
- setPasswordDialogModel.CanOpen = CanOpen;
- CheckCanConfirmEncrypt();
- }
- public void SetCanEdit()
- {
- setPasswordDialogModel.CanEdit = CanEdit;
- CheckCanConfirmEncrypt();
- }
- public void CanOpenTextChanged()
- {
- CheckCanConfirmEncrypt();
- }
- public void CanEditTextChanged()
- {
- CheckCanConfirmEncrypt();
- }
- 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);
- }
- 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.CanOpenPassword))
- {
- openPassword = SetPasswordDialogModel.CanOpenPassword;
- }
- }
- if (setPasswordDialogModel.CanEdit)
- {
- if (!string.IsNullOrEmpty(SetPasswordDialogModel.CanEditPassword))
- {
- editPassword = SetPasswordDialogModel.CanEditPassword;
- 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));
- 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;
- }
- }
- }
- }
|