using ComPDFKit.PDFDocument; using PDF_Master.CustomControl; using PDF_Master.Model; using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Forms; using System.Windows.Controls; using CheckBox = System.Windows.Controls.CheckBox; using System.Drawing.Printing; using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.SetPasswordDialogModel; using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing; using ComPDFKitViewer.PdfViewer; using Prism.Regions; using PDF_Master.Helper; using System.Diagnostics; using PDF_Master.Properties; namespace PDF_Master.ViewModels.Dialog.ToolsDialogs.SaftyDialogs { public class SetPasswordDialogViewModel : BindableBase, IDialogAware { #region 文案 private string T_title; public string T_Title { get { return T_title; } set { SetProperty(ref T_title, value); } } private string T_openTitle; public string T_OpenTitle { get { return T_openTitle; } set { SetProperty(ref T_openTitle, value); } } private string T_permissionTitle; public string T_PermissionTitle { get { return T_permissionTitle; } set { SetProperty(ref T_permissionTitle, value); } } private string T_restrictPrinting; public string T_RestrictPrinting { get { return T_restrictPrinting; } set { SetProperty(ref T_restrictPrinting, value); } } private string T_restrictCopying; public string T_RestrictCopying { get { return T_restrictCopying; } set { SetProperty(ref T_restrictCopying, value); } } private string T_yes; public string T_Yes { get { return T_yes; } set { SetProperty(ref T_yes, value); } } private string T_no; public string T_No { get { return T_no; } set { SetProperty(ref T_no, value); } } private void InitString() { T_Title = App.MainPageLoader.GetString("SetPassword_Title"); T_OpenTitle = App.MainPageLoader.GetString("SetPassword_OpenTitle"); T_PermissionTitle = App.MainPageLoader.GetString("SetPassword_PermissionTitle"); T_RestrictPrinting = App.MainPageLoader.GetString("SetPassword_RestrictPrinting"); T_RestrictCopying = App.MainPageLoader.GetString("SetPassword_RestrictCopying"); T_Yes = App.MainPageLoader.GetString("SetPassword_YES"); T_No = App.MainPageLoader.GetString("SetPassword_No"); } #endregion #region 参数和属性 private SetPasswordDialogModel setPasswordDialogModel = new SetPasswordDialogModel(); private CPDFViewer cPDFViewer; public IDialogService dialogs; private ViewContentViewModel viewContentViewModel; private EnumPermissionsMod enumPermissionsMod = EnumPermissionsMod.StatusUnknown | EnumPermissionsMod.StatusRestrictPrinting | EnumPermissionsMod.StatusRestrictCopying; private bool haveViewModel = false; private bool _enableConfirm = false; public bool EnableConfirm { get { return _enableConfirm; } set { SetProperty(ref _enableConfirm, value); } } private string _openPassword = ""; public string OpenPassword { get { return _openPassword; } set { SetProperty(ref _openPassword, value); CheckEnableConfirmEncrypt(); } } private string _permissionsPassword = ""; public string PermissionsPassword { get { return _permissionsPassword; } set { SetProperty(ref _permissionsPassword, value); CheckEnableConfirmEncrypt(); } } private string _openPasswordMsg = App.MainPageLoader.GetString("SetPassword_OpenEmptyText"); public string OpenPasswordMsg { get { return _openPasswordMsg; } set { SetProperty(ref _openPasswordMsg, value); } } private string _permissionsPasswordMsg = App.MainPageLoader.GetString("SetPassword_PermissionEmptyText"); public string PermissionsPasswordMsg { get { return _permissionsPasswordMsg; } set { SetProperty(ref _permissionsPasswordMsg, value); } } private Visibility _openTextVisibility = Visibility.Collapsed; public Visibility OpenTextVisibility { get { return _openTextVisibility; } set { SetProperty(ref _openTextVisibility, value); } } private Visibility _openPasswordVisibility = Visibility.Visible; public Visibility OpenPasswordVisibility { get { return _openPasswordVisibility; } set { if (value == Visibility.Visible) { OpenTextVisibility = Visibility.Collapsed; } else { OpenTextVisibility = Visibility.Visible; } SetProperty(ref _openPasswordVisibility, value); } } private Visibility _permissionsPasswordVisibility = Visibility.Visible; public Visibility PermissionsPasswordVisibility { get { return _permissionsPasswordVisibility; } set { if (value == Visibility.Visible) { PermissionsTextVisibility = Visibility.Collapsed; } else { PermissionsTextVisibility = Visibility.Visible; } SetProperty(ref _permissionsPasswordVisibility, value); } } private Visibility _permissionsTextVisibility = Visibility.Collapsed; public Visibility PermissionsTextVisibility { get { return _permissionsTextVisibility; } set { SetProperty(ref _permissionsTextVisibility, value); } } private bool? _isRestrictPrinting; public bool? IsRestrictPrinting { get { return _isRestrictPrinting; } set { SetProperty(ref _isRestrictPrinting, value); } } private bool? _isRestrictCopying; public bool? IsRestrictCopying { get { return _isRestrictCopying; } set { SetProperty(ref _isRestrictCopying, value); } } private bool _needOpenPassword; public bool NeedOpenPassword { get { return _needOpenPassword; } set { SetProperty(ref _needOpenPassword, value); } } private bool _needPermissionsPassword; public bool NeedPermissionsPassword { get { return _needPermissionsPassword; } set { SetProperty(ref _needPermissionsPassword, value); } } private bool _isOpenPasswordDisplayed = false; public bool IsOpenPasswordDisplayed { get { return _isOpenPasswordDisplayed; } set { SetProperty(ref _isOpenPasswordDisplayed, value); } } private bool _isPermissionsPasswordDisplayed = false; public bool IsPermissionsPasswordDisplayed { get { return _isPermissionsPasswordDisplayed; } set { SetProperty(ref _isPermissionsPasswordDisplayed, value); } } #endregion #region 委托声明 public DelegateCommand DisplayPasswordCommand { get; set; } public DelegateCommand SetOpenPasswordCommand { get; set; } public DelegateCommand SetPermissionsPasswordCommand { get; set; } public DelegateCommand SetRestrictCommand { get; set; } public DelegateCommand EncryptCommand { get; set; } public DelegateCommand CancelEncryptCommand { get; set; } public DelegateCommand BatchSetPasswordCommand { get; set; } #endregion public SetPasswordDialogViewModel(IDialogService dialogService) { InitString(); dialogs = dialogService; DisplayPasswordCommand = new DelegateCommand(DisplayPassword); SetOpenPasswordCommand = new DelegateCommand(SetOpenPassword); SetPermissionsPasswordCommand = new DelegateCommand(SetPermissionsPassword); SetRestrictCommand = new DelegateCommand(SetRestrict); EncryptCommand = new DelegateCommand(Encrypt); CancelEncryptCommand = new DelegateCommand(CancelEncrypt); BatchSetPasswordCommand = new DelegateCommand(BatchSetPassword); } #region 检查和初始化 private void CheckEnableConfirmEncrypt() { EnableConfirm = true; if (!(NeedOpenPassword || NeedPermissionsPassword)) { EnableConfirm = false; } if (NeedOpenPassword && string.IsNullOrEmpty(OpenPassword)) { EnableConfirm = false; } if (NeedPermissionsPassword && string.IsNullOrEmpty(PermissionsPassword)) { EnableConfirm = false; } if (NeedPermissionsPassword && (!((bool)IsRestrictCopying || (bool)IsRestrictPrinting))) { EnableConfirm = false; } } #endregion #region 逻辑函数 public void DisplayPassword(object e) { var chk = e as CheckBox; if (chk != null) { if (chk.Name == "DisplayOpenPasswordChk") { if (chk.IsChecked == true) { OpenPasswordVisibility = Visibility.Collapsed; } else { OpenPasswordVisibility = Visibility.Visible; } } else { if (chk.IsChecked == true) { PermissionsPasswordVisibility = Visibility.Collapsed; } else { PermissionsPasswordVisibility = Visibility.Visible; } } } } public void SetOpenPassword(object e) { var chk = e as CheckBox; if (chk != null) { IsOpenPasswordDisplayed = false; OpenPasswordVisibility = Visibility.Visible; } CheckEnableConfirmEncrypt(); } public void SetPermissionsPassword(object e) { var chk = e as CheckBox; if (chk != null) { IsPermissionsPasswordDisplayed = false; PermissionsPasswordVisibility = Visibility.Visible; if (chk.IsChecked == true) { IsRestrictPrinting = true; IsRestrictCopying = true; } else { IsRestrictPrinting = false; IsRestrictCopying = false; } } CheckEnableConfirmEncrypt(); } public void SetRestrict(object e) { var chk = e as CheckBox; if (chk != null) { if ((bool)chk.IsChecked) { if (chk.Tag.ToString() == "Printing") { enumPermissionsMod |= EnumPermissionsMod.StatusRestrictPrinting; } else if (chk.Tag.ToString() == "Copying") { enumPermissionsMod |= EnumPermissionsMod.StatusRestrictCopying; } } else { if (chk.Tag.ToString() == "Printing") { enumPermissionsMod -= EnumPermissionsMod.StatusRestrictPrinting; } else if (chk.Tag.ToString() == "Copying") { enumPermissionsMod -= EnumPermissionsMod.StatusRestrictCopying; } } } CheckEnableConfirmEncrypt(); } private void BatchSetPassword() { DialogParameters setPasswordpdf = new DialogParameters(); setPasswordpdf.Add(ParameterNames.BatchProcessing_Name, "2"); HomePageBatchProcessingDialogModel.FilePaths = new List { cPDFViewer.Document.FilePath.ToString() }; HomePageBatchProcessingDialogModel.BatchProcessingIndex = 2; setPasswordpdf.Add(ParameterNames.FilePath, new string[] { cPDFViewer.Document.FilePath.ToString() }); dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, setPasswordpdf, e => { }); } public void Encrypt() { if (haveViewModel) { viewContentViewModel.SecurityInfo.IsPasswordChanged = true; viewContentViewModel.SecurityInfo.IsPasswordRemoved= false; if (NeedOpenPassword && (!string.IsNullOrEmpty(OpenPassword))) { this.viewContentViewModel.SecurityInfo.NeedOpenPassword = true; this.viewContentViewModel.SecurityInfo.OpenPassword = OpenPassword; } else { this.viewContentViewModel.SecurityInfo.NeedOpenPassword = false; viewContentViewModel.SecurityInfo.OpenPassword = null; } if (NeedPermissionsPassword && (!string.IsNullOrEmpty(PermissionsPassword))) { this.viewContentViewModel.SecurityInfo.NeedPermissionsPassword = true; viewContentViewModel.SecurityInfo.PermissionsPassword = PermissionsPassword; viewContentViewModel.SecurityInfo.CPDFPermissionsInfo = CreatePermissionsInfo(enumPermissionsMod); } else { this.viewContentViewModel.SecurityInfo.NeedPermissionsPassword = false; viewContentViewModel.SecurityInfo.PermissionsPassword = null; viewContentViewModel.SecurityInfo.CPDFPermissionsInfo = CreateDefaultPermissionsInfo(); } RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK)); } else { string openPassword = ""; string permissionsPassword = ""; CPDFPermissionsInfo permissionsInfo = null; if (NeedOpenPassword) { if (!string.IsNullOrEmpty(OpenPassword)) { openPassword = OpenPassword; } } if (NeedPermissionsPassword && (!string.IsNullOrEmpty(PermissionsPassword))) { permissionsPassword = PermissionsPassword; permissionsInfo = CreatePermissionsInfo(enumPermissionsMod); } else { permissionsInfo = CreateDefaultPermissionsInfo(); } System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog(); /* *设置这个对话框的起始保存路径 */ sfd.InitialDirectory = cPDFViewer.Document.FilePath; /* *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;” */ sfd.Filter = "PDF|*.pdf;"; /* *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮 */ sfd.FileName = cPDFViewer.Document.FileName + "_EncryptedFile.pdf"; /* * 做一些工作 */ if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { cPDFViewer.Document.Encrypt(openPassword, permissionsPassword, permissionsInfo); cPDFViewer.Document.WriteToFilePath(sfd.FileName); //保存成功后没必要弹窗提示,直接显示文件浏览器,选中保存后的文档流程更简洁 //AlertsMessage alertsMessage = new AlertsMessage(); //alertsMessage.ShowDialog("保存成功","","确认"); DialogParameters dialogParameters = new DialogParameters(); dialogParameters.Add("FilePath", sfd.FileName); RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, dialogParameters)); } else { } } } //public void Encrypt() //{ // string openPassword = ""; // string permissionsPassword = ""; // CPDFPermissionsInfo permissionsInfo = null; // if (NeedOpenPassword) // { // if (!string.IsNullOrEmpty(OpenPassword)) // { // openPassword = OpenPassword; // } // } // if (NeedPermissionsPassword && (!string.IsNullOrEmpty(PermissionsPassword))) // { // permissionsPassword = PermissionsPassword; // permissionsInfo = CreatePermissionsInfo(enumPermissionsMod); // } // else // { // permissionsInfo = CreateDefaultPermissionsInfo(); // } // System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog(); // /* // *设置这个对话框的起始保存路径 // */ // sfd.InitialDirectory = cPDFViewer.Document.FilePath; // /* // *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;” // */ // sfd.Filter = "PDF|*.pdf;"; // /* // *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮 // */ // sfd.FileName = cPDFViewer.Document.FileName + "_EncryptedFile.pdf"; // /* // * 做一些工作 // */ // if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) // { // cPDFViewer.Document.Encrypt(openPassword, permissionsPassword, permissionsInfo); // cPDFViewer.Document.WriteToFilePath(sfd.FileName); // MessageBoxEx.Show("保存成功"); // RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK)); // } // else // { // } //} 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) { if (parameters.TryGetValue(ParameterNames.ViewContentViewModel, out viewContentViewModel)) { haveViewModel = true; } else { haveViewModel = false; } parameters.TryGetValue(ParameterNames.PDFViewer, out cPDFViewer); } #endregion } }