123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- 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<object> DisplayPasswordCommand { get; set; }
- public DelegateCommand<object> SetOpenPasswordCommand { get; set; }
- public DelegateCommand<object> SetPermissionsPasswordCommand { get; set; }
- public DelegateCommand<object> 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<object>(DisplayPassword);
- SetOpenPasswordCommand = new DelegateCommand<object>(SetOpenPassword);
- SetPermissionsPasswordCommand = new DelegateCommand<object>(SetPermissionsPassword);
- SetRestrictCommand = new DelegateCommand<object>(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<string> { 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<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- if (parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel))
- {
- haveViewModel = true;
- }
- else
- {
- haveViewModel = false;
- }
- parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out cPDFViewer);
- }
- #endregion
- }
- }
|