Sfoglia il codice sorgente

安全-弹窗校验密码

liuaoran 2 anni fa
parent
commit
bd1ea070fb

+ 1 - 3
PDF Office/CustomControl/MessageBoxEx.cs

@@ -531,7 +531,5 @@ hInstance, int threadID);//设置挂钩
                 _nextHookPtr = IntPtr.Zero;
             }
         }
-    }
-
-   
+    } 
 }

+ 14 - 4
PDF Office/Helper/PasswordBoxHelper.cs

@@ -5,7 +5,7 @@ namespace PDF_Office.Helper
 {
       public class PasswordBoxHelper
     {
-        // 包含两个依赖附加属性 Password   Attach
+        // 依赖附加属性 Password   Attach DisplayText IsWrongValue
         public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordBoxHelper),
               new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));
 
@@ -41,6 +41,16 @@ namespace PDF_Office.Helper
             d.SetValue(PasswordProperty, value);
         }
 
+        public static readonly DependencyProperty IsRightValueProperty = DependencyProperty.RegisterAttached("IsRightValue", typeof(string), typeof(PasswordBoxHelper));
+        public static string GetIsRightValue(DependencyObject d)
+        {
+            return (string)d.GetValue(PasswordProperty);
+        }
+        public static void SetIsRightValue(DependencyObject d, string value)
+        {
+            d.SetValue(PasswordProperty, value);
+        }
+
         static bool _isUpdating = false;
         private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
@@ -55,9 +65,7 @@ namespace PDF_Office.Helper
         {
             PasswordBox pb = (d as PasswordBox);
             pb.PasswordChanged += Pb_PasswordChanged;
-        }
-
-
+        } 
 
         private static void Pb_PasswordChanged(object sender, RoutedEventArgs e)
         {
@@ -66,5 +74,7 @@ namespace PDF_Office.Helper
             SetPassword(pb, pb.Password);
             _isUpdating = false;
         }
+
+
     }
 }

+ 44 - 0
PDF Office/Helper/SecurityHelper.cs

@@ -0,0 +1,44 @@
+using ComPDFKit.PDFDocument;
+using PDF_Office.Model;
+using Prism.Services.Dialogs;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
+
+namespace PDF_Office.Helper
+{
+    public  class SecurityHelper
+    {
+        public static bool CheckHaveAllPermissions(CPDFDocument document)
+        {
+            CPDFPermissionsInfo permissionsInfo = document.GetPermissionsInfo();
+            if (permissionsInfo.AllowsDocumentChanges &&
+                permissionsInfo.AllowsPrinting &&
+                permissionsInfo.AllowsHighQualityPrinting &&
+                permissionsInfo.AllowsCopying &&
+                permissionsInfo.AllowsDocumentAssembly &&
+                permissionsInfo.AllowsFormFieldEntry &&
+                permissionsInfo.AllowsCommenting)
+            {
+                return true;
+            }
+            else
+            {
+                return false;
+            }
+        }
+
+        public static bool VerifyPassword(CPDFDocument document ,EnumPasswordKind enumPasswordKind, IDialogService dialogService)
+        {
+            DialogParameters value = new DialogParameters();
+            value.Add(ParameterNames.PasswordKind, EnumPasswordKind.StatusPermissionsPassword);
+            value.Add(ParameterNames.PDFDocument, document);
+            bool isDiscryptied = false;
+            dialogService.ShowDialog(DialogNames.CheckPasswordDialog, value, e => { isDiscryptied = e.Parameters.GetValue<bool>("CheckPassword"); });
+            return isDiscryptied;
+        }
+    }
+}

+ 2 - 2
PDF Office/Model/Dialog/ToolsDialogs/SaftyDialogs/CheckPasswordDialogModel.cs

@@ -6,8 +6,8 @@ using System.Threading.Tasks;
 
 namespace PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs
 {
-    internal class CheckPasswordDialogModel
+    public class CheckPasswordDialogModel
     {
-        static public string Password = "";
     }
+
 }

+ 6 - 0
PDF Office/Model/Dialog/ToolsDialogs/SaftyDialogs/DeleteSafetySettintgsModel.cs

@@ -14,5 +14,11 @@ namespace PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs
             StatusVerifyPassword,
             StatusDirectDescrypt
         }
+
+        public enum EnumPasswordKind
+        {
+            StatusOpenPassword,
+            StatusPermissionsPassword
+        }
     }
 }

+ 11 - 5
PDF Office/Model/Dialog/ToolsDialogs/SaftyDialogs/SetPasswordDialogModel.cs

@@ -27,17 +27,23 @@ namespace PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs
             StatusRestrictCopying = 1 << 1
         }
 
-        public static CPDFPermissionsInfo CreatePermissionsInfo(EnumPermissionsMod enumPermissionsMod)
+        public static CPDFPermissionsInfo CreateDefaultPermissionsInfo()
         {
             CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo();
             permissionsInfo.AllowsDocumentAssembly = true;
             permissionsInfo.AllowsDocumentChanges = true;
             permissionsInfo.AllowsPrinting = true;
-            permissionsInfo.AllowsCopying= true;
-            permissionsInfo.AllowsCommenting= true;
-            permissionsInfo.AllowsFormFieldEntry= true;
-            permissionsInfo.AllowsHighQualityPrinting= true;
+            permissionsInfo.AllowsCopying = true;
+            permissionsInfo.AllowsCommenting = true;
+            permissionsInfo.AllowsFormFieldEntry = true;
+            permissionsInfo.AllowsHighQualityPrinting = true;
             permissionsInfo.AllowsFormFieldEntry = true;
+            return permissionsInfo;
+        }
+        public static CPDFPermissionsInfo CreatePermissionsInfo(EnumPermissionsMod enumPermissionsMod)
+        {
+            CPDFPermissionsInfo permissionsInfo = CreateDefaultPermissionsInfo();
+
             if ((enumPermissionsMod & EnumPermissionsMod.StatusRestrictPrinting) > 0)
             {
                 permissionsInfo.AllowsPrinting = false;

+ 5 - 0
PDF Office/Model/ParameterNames.cs

@@ -106,5 +106,10 @@ namespace PDF_Office.Model
         public static string PrintQueue = "PrintQueue";
         public static string PrintSettingsInfo = "PrintSettingsInfo";
         public static string PrintCurrentPage = "PrintCurrentPage";
+
+        /// <summary>
+        /// 密码
+        /// </summary>
+        public static string PasswordKind = "PasswordKind";
     }
 }

+ 2 - 0
PDF Office/PDF Office.csproj

@@ -328,6 +328,7 @@
     <Compile Include="Helper\RichTextBoxHelper.cs" />
     <Compile Include="Helper\SDKLisenceHelper.cs" />
     <Compile Include="Helper\PictureConverter.cs" />
+    <Compile Include="Helper\SecurityHelper.cs" />
     <Compile Include="Helper\SetterAction.cs" />
     <Compile Include="Helper\SettingHelper.cs" />
     <Compile Include="Helper\ToolMethod.cs" />
@@ -1973,6 +1974,7 @@
     </Resource>
     <Resource Include="Resources\BOTA\no_outline.png" />
     <Resource Include="Resources\PropertyPanel\nosign.png" />
+    <Resource Include="Resources\Dialog\password.png" />
     <Content Include="source\AnalysisWord\Res\word\_rels\document.xml.rels">
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
     </Content>

BIN
PDF Office/Resources/Dialog/password.png


+ 137 - 19
PDF Office/ViewModels/Dialog/ToolsDialogs/SaftyDialogs/CheckPasswordDialogViewModel.cs

@@ -1,54 +1,163 @@
 using ComPDFKit.PDFDocument;
+using PDF_Office.Helper;
 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.Linq;
-using System.Xml.Linq;
+using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
+
+using Visibility = System.Windows.Visibility;
 
 namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
 {
-    public class CheckPasswordDialogViewModel : BindableBase,IDialogAware
-    { 
+    public class CheckPasswordDialogViewModel : BindableBase, IDialogAware
+    {
         #region 参数和属性
         private CPDFDocument document;
+        private CPDFDocument tempDocument;
         #endregion
 
+        private EnumPasswordKind passwordKind = EnumPasswordKind.StatusOpenPassword;
+        private string _password = "";
+        public string Password
+        {
+            get { return _password; }
+            set
+            {
+                SetProperty(ref _password, value);
+                IsRightValue = "unknow";
+                if (!string.IsNullOrEmpty(value)){
+                    ClearPasswordButtonVisibility = Visibility.Visible;
+                }
+                else
+                {
+                    ClearPasswordButtonVisibility = Visibility.Collapsed;
+                }
+            }
+        }
+
+        private string _inputPasswordMsg;
+        public string InputPasswordMsg
+        {
+            get { return _inputPasswordMsg; }
+            set { SetProperty(ref _inputPasswordMsg, value); }
+        }
+
+        private string _checkPasswordMsg;
+        public string CheckPasswordMsg
+        {
+            get { return _checkPasswordMsg; }
+            set { SetProperty(ref _checkPasswordMsg, value); }
+        }
+
+        private string _errorPasswordMsg;
+        public string ErrorPasswordMsg
+        {
+            get { return _errorPasswordMsg; }
+            set { SetProperty(ref _errorPasswordMsg, value); }
+        }
+
+        private string _isRightValue = "unknown";
+        public string IsRightValue
+        {
+            get { return _isRightValue; }
+            set { 
+                SetProperty(ref _isRightValue, value);
+                if(value == "false")
+                {
+                    ErrorPasswordMsgVisibility = Visibility.Visible;
+                }
+                else
+                {
+                    ErrorPasswordMsgVisibility = Visibility.Collapsed;
+                }
+            }
+        }
+
+        private Visibility _clearPasswordButtonVisibility = Visibility.Collapsed;
+        public Visibility ClearPasswordButtonVisibility
+        {
+            get { return _clearPasswordButtonVisibility; }
+            set { SetProperty(ref _clearPasswordButtonVisibility, value); }
+        }
+
+        private Visibility _errorPasswordMsgVisibility = Visibility.Collapsed;
+        public Visibility ErrorPasswordMsgVisibility
+        {
+            get { return _errorPasswordMsgVisibility; }
+            set { SetProperty(ref _errorPasswordMsgVisibility, value); }
+        }
         #region 委托声明
-        public DelegateCommand DelegateConfirmCheckPasswordCommand { get; set; }
-        public DelegateCommand DelegateCancelCheckPasswordCommand { get; set; }
+        public DelegateCommand ConfirmCommand { get; set; }
+        public DelegateCommand CancelCommand { get; set; }
+
+        public DelegateCommand ClearPasswordCommand { get; set; }
         #endregion
 
         public CheckPasswordDialogViewModel()
         {
-            CheckPasswordDialogModel.Password = "";
-            DelegateConfirmCheckPasswordCommand = new DelegateCommand(ConfirmCheckPassword);
-            DelegateCancelCheckPasswordCommand = new DelegateCommand(CancelCheckPassword);
+            ConfirmCommand = new DelegateCommand(Confirm);
+            CancelCommand = new DelegateCommand(Cancel);
+            ClearPasswordCommand = new DelegateCommand(ClearPassword);
         }
 
         #region 逻辑函数
-        private void ConfirmCheckPassword()
+        private void Confirm()
         {
-            if (false)//TODO: 用于检测输入密码的权限,以及是否输入了正确的密码
-            {  }
-            var dialogResult = new DialogResult(ButtonResult.OK);
-            dialogResult.Parameters.Add("CheckPassword", true);
-            RequestClose.Invoke(dialogResult);
+            if (passwordKind == EnumPasswordKind.StatusOpenPassword)//TODO: 用于检测输入密码的权限,以及是否输入了正确的密码
+            {
+                if (tempDocument.UnlockWithPassword(Password))
+                {
+                    var dialogResult = new DialogResult(ButtonResult.OK);
+                    dialogResult.Parameters.Add("CheckPasswordResult", true);
+                    RequestClose.Invoke(dialogResult);
+                }
+                else
+                {
+                    IsRightValue = "false";
+                }
+            }
+            else
+            {
+                if (tempDocument.UnlockWithPassword(Password))
+                {
+                    if (SecurityHelper.CheckHaveAllPermissions(tempDocument))
+                    {
+                        var dialogResult = new DialogResult(ButtonResult.OK);
+                        dialogResult.Parameters.Add("CheckPasswordResult", true);
+                        RequestClose.Invoke(dialogResult);
+                    }
+                    else
+                    {
+                        IsRightValue = "false";
+                        tempDocument.Release();
+                        tempDocument = CPDFDocument.InitWithFilePath(document.FilePath);
+                    }
+                }
+                else
+                {
+                    IsRightValue = "false";
+                }
+            }
+
         }
 
-        private void CancelCheckPassword()
+        private void Cancel()
         {
             var dialogResult = new DialogResult(ButtonResult.Cancel);
             dialogResult.Parameters.Add("CheckPassword", false);
             RequestClose.Invoke(dialogResult);
         }
+
+        private void ClearPassword()
+        {
+            Password= "";
+        }
         #endregion
 
         #region 框架相关
-        public string Title =>""; 
+        public string Title => "";
         public event Action<IDialogResult> RequestClose;
 
         public bool CanCloseDialog()
@@ -58,15 +167,24 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
 
         public void OnDialogClosed()
         {
+            if (tempDocument != null)
+            {
+                tempDocument.Release();
+            }
         }
 
         public void OnDialogOpened(IDialogParameters parameters)
         {
             CPDFDocument doc = null;
             parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
+            parameters.TryGetValue<EnumPasswordKind>(ParameterNames.PasswordKind, out passwordKind);
             if (doc != null)
             {
                 document = doc;
+                tempDocument = CPDFDocument.InitWithFilePath(document.FilePath);
+                InputPasswordMsg = "\"" + document.FileName + ".pdf\"" + " is protected, please enter a Document Open Password.";
+                CheckPasswordMsg = "Password";
+                ErrorPasswordMsg = "Tips Text";
             }
         }
         #endregion

+ 89 - 37
PDF Office/ViewModels/Dialog/ToolsDialogs/SaftyDialogs/DeleteSafetySettingsDialogViewModel.cs

@@ -1,11 +1,14 @@
 using ComPDFKit.PDFDocument;
+using PDF_Office.CustomControl;
+using PDF_Office.Helper;
 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;
 using System.Windows.Forms;
+using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
 
 namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
 {
@@ -25,36 +28,18 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
         #endregion
 
         #region 委托声明
-        public DelegateCommand DelegateCheckPasswordCommand { get; set; }
-        public DelegateCommand DelegateCancelCommand { get; set; }
+        public DelegateCommand RemoveSecurityCommand { get; set; }
+        public DelegateCommand CancelCommand { get; set; }
         #endregion
 
         public DeleteSafetySettingsDialogViewModel(IDialogService dialogService)
         {
             dialogs = dialogService;
-            DelegateCheckPasswordCommand = new DelegateCommand(OpenCheckPasswordDialog);
-            DelegateCancelCommand = new DelegateCommand(Cancel);
+            RemoveSecurityCommand = new DelegateCommand(RemoveSecurity);
+            CancelCommand = new DelegateCommand(Cancel);
         }
 
         #region 检查函数
-        private bool CheckHaveAllPermissions()
-        {
-            CPDFPermissionsInfo permissionsInfo = document.GetPermissionsInfo();
-            if (permissionsInfo.AllowsDocumentChanges &&
-                permissionsInfo.AllowsPrinting &&
-                permissionsInfo.AllowsHighQualityPrinting &&
-                permissionsInfo.AllowsCopying &&
-                //permissionsInfo.AllowsDocumentAssembly&&
-                //TODO: 这个权限有问题
-                permissionsInfo.AllowsCommenting)
-            {
-                return true;
-            }
-            else
-            {
-                return false;
-            }
-        }
 
         /// <summary>
         /// 检测是否需要输入密码
@@ -66,31 +51,98 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
         /// </returns>
         private DeleteSafetySettintgsModel.EnumNeedPassword CheckNeedPassword()
         {
-            if (document.IsLocked)
-            {
-                return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
-            }
-            else { }
-            if (document.IsEncrypted)
+            try
             {
-                if (CheckHaveAllPermissions())
+                if (document.IsLocked)
                 {
-                    return DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt;
+                    return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
                 }
                 else
                 {
-                    return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
+                    if (SecurityHelper.CheckHaveAllPermissions(document))
+                    {
+                        return DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt;
+                    }
+                    else
+                    {
+                        return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
+                    }
                 }
             }
-            else
+            catch
             {
-                return DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt;
+                return EnumNeedPassword.StatusCannotDecrypt;
             }
         }
         #endregion
 
         #region 逻辑函数
-        private void OpenCheckPasswordDialog()
+        public void RemoveSecurity()
+        {
+            EnumNeedPassword enumNeedPassword = CheckNeedPassword();
+            bool isDiscryptied = false;
+            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
+            System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
+            /*
+             *设置这个对话框的起始保存路径
+             */
+            sfd.InitialDirectory = document.FilePath;
+            /*
+             *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
+             */
+            sfd.Filter = "PDF|*.pdf;";
+            /*
+             *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
+             */
+            sfd.FileName = document.FileName + "_CompressFile.pdf";
+            /*
+             * 做一些工作
+             */
+            if (enumNeedPassword == EnumNeedPassword.StatusVerifyPassword)
+            {
+                DialogParameters value = new DialogParameters();
+                value.Add("PasswordKind", EnumPasswordKind.StatusPermissionsPassword);
+                dialogs.ShowDialog(DialogNames.CheckPasswordDialog, value, e => { isDiscryptied = e.Parameters.GetValue<bool>("CheckPassword"); });
+                if (isDiscryptied)
+                {
+                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+                    {
+                        document.Descrypt(sfd.FileName);
+                        MessageBoxEx.Show("保存成功");
+                        RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
+                    }
+                    else
+                    {
+                        MessageBox.Show("取消保存");
+                    }
+                }
+                else
+                {
+                }
+            }
+            else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt)
+            {
+                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+                {
+                    document.Descrypt(sfd.FileName);
+                    MessageBoxEx.Show("Ok");
+                    RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
+                }
+                else
+                {
+                    MessageBoxEx.Show("取消保存");
+                }
+            }
+            else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt)
+            {
+                MessageBoxEx.Show("无可用安全性设置");
+                RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
+            }
+            else
+            { }
+        }
+
+        private void CheckPassword()
         {
             FolderBrowserDialog folderDialog = new FolderBrowserDialog();
             System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
@@ -113,13 +165,11 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
             {
                 bool IfDiscryptied = false;
                 DialogParameters value = new DialogParameters();
-                RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
                 dialogs.ShowDialog(DialogNames.CheckPasswordDialog, value, e => { IfDiscryptied = e.Parameters.GetValue<bool>("CheckPassword"); });
                 if (IfDiscryptied)
                 {
                     if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                     {
-
                         document.Descrypt(sfd.FileName);
                         MessageBox.Show("保存成功");
                         RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
@@ -181,7 +231,9 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
             {
                 document = doc;
                 DeleteSecurityMsg = "Are you sure you want to remove the security settings for”" + document.FileName + "” documents?";
+                SecurityHelper.VerifyPassword(document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
             }
+
         }
         #endregion
     }

+ 20 - 13
PDF Office/ViewModels/Dialog/ToolsDialogs/SaftyDialogs/SetPasswordDialogViewModel.cs

@@ -24,7 +24,7 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
         private SetPasswordDialogModel setPasswordDialogModel = new SetPasswordDialogModel();
         private CPDFDocument document;
 
-        private EnumPermissionsMod enumPermissionsMod = EnumPermissionsMod.StatusUnknown|EnumPermissionsMod.StatusRestrictPrinting|EnumPermissionsMod.StatusRestrictCopying;
+        private EnumPermissionsMod enumPermissionsMod = EnumPermissionsMod.StatusUnknown | EnumPermissionsMod.StatusRestrictPrinting | EnumPermissionsMod.StatusRestrictCopying;
 
         private bool _enableConfirm = false;
         public bool EnableConfirm
@@ -167,7 +167,7 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
         {
             get { return _isPermissionsPasswordDisplayed; }
             set { SetProperty(ref _isPermissionsPasswordDisplayed, value); }
-        } 
+        }
         #endregion
 
         #region 委托声明
@@ -176,7 +176,8 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
         public DelegateCommand<object> SetPermissionsPasswordCommand { get; set; }
         public DelegateCommand<object> SetRestrictCommand { get; set; }
         public DelegateCommand EncryptCommand { get; set; }
-         
+        public DelegateCommand CancelEncryptCommand { get; set; }
+
         #endregion
 
         public SetPasswordDialogViewModel()
@@ -186,6 +187,7 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
             SetPermissionsPasswordCommand = new DelegateCommand<object>(SetPermissionsPassword);
             SetRestrictCommand = new DelegateCommand<object>(SetRestrict);
             EncryptCommand = new DelegateCommand(Encrypt);
+            CancelEncryptCommand = new DelegateCommand(CancelEncrypt);
         }
 
         #region 检查和初始化
@@ -310,6 +312,7 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
 
         public void Encrypt()
         {
+
             string openPassword = "";
             string permissionsPassword = "";
             FolderBrowserDialog folderDialog = new FolderBrowserDialog();
@@ -328,30 +331,34 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
             sfd.FileName = document.FileName + "_SetPassword.pdf";
             if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
-                CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo();
+                CPDFPermissionsInfo permissionsInfo = null;
 
                 if (NeedOpenPassword)
                 {
                     if (!string.IsNullOrEmpty(OpenPassword))
                     {
-                        openPassword= OpenPassword;
+                        openPassword = OpenPassword;
                     }
                 }
-                if (NeedPermissionsPassword){
-                    if (!string.IsNullOrEmpty(PermissionsPassword))
-                    {
-                        permissionsPassword = PermissionsPassword;
-                    }
+                if (NeedPermissionsPassword && (!string.IsNullOrEmpty(PermissionsPassword)))
+                {
+                    permissionsPassword = PermissionsPassword;
+                    permissionsInfo = CreatePermissionsInfo(enumPermissionsMod);
+                }
+                else
+                {
+                    permissionsInfo = CreateDefaultPermissionsInfo();
                 }
-                permissionsInfo = CreatePermissionsInfo(enumPermissionsMod);
-                document.Encrypt(openPassword, permissionsPassword, permissionsInfo);
 
+                document.Encrypt(openPassword, permissionsPassword, permissionsInfo);
                 document.WriteToFilePath(sfd.FileName);
                 MessageBoxEx.Show(sfd.FileName + " 保存成功");
-                RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
+                RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
             }
         }
 
+        public void CancelEncrypt() => RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
+
         #endregion
         #region 框架相关
         public string Title => "";

+ 53 - 53
PDF Office/Views/Dialog/ToolsDialogs/CompressDialogs/CompressDialog.xaml

@@ -1,58 +1,58 @@
-<UserControl x:Class="PDF_Office.Views.Dialog.ToolsDialogs.CompressDialogs.CompressDialog"
-             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
-             xmlns:cus="clr-namespace:PDF_Office.CustomControl"
-             prism:Dialog.WindowStyle="{StaticResource DialogWindowStyle}"
-             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
-             xmlns:prism="http://prismlibrary.com/" xmlns:compressdialogs="clr-namespace:PDF_Office.ViewModels.Dialog.ToolsDialogs.CompressDialogs" 
-             prism:ViewModelLocator.AutoWireViewModel="True"
-             d:DesignHeight="226"
-             d:DesignWidth="468"
-             mc:Ignorable="d">
+<UserControl x:Class="PDF_Office.Views.Dialog.ToolsDialogs.CompressDialogs.CompressDialog"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+             xmlns:cus="clr-namespace:PDF_Office.CustomControl"
+             prism:Dialog.WindowStyle="{StaticResource DialogWindowStyle}"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+             xmlns:prism="http://prismlibrary.com/" xmlns:compressdialogs="clr-namespace:PDF_Office.ViewModels.Dialog.ToolsDialogs.CompressDialogs" 
+             prism:ViewModelLocator.AutoWireViewModel="True"
+             d:DesignHeight="226"
+             d:DesignWidth="468"
+             mc:Ignorable="d">
     <cus:DialogContent Header="Compress">
         <cus:DialogContent.Content>
-            <Grid Grid.Row="1" Width="436">
-                <StackPanel>
-                    <RadioButton   Margin="0,10,0,8"  Command="{Binding LargeQualityCommand}" >
-                        <TextBlock Text="Large Fill Size"></TextBlock>
-                    </RadioButton>
-
-                    <RadioButton  Margin="0,0,0,8"  Command="{Binding StandardQualityCommand}">
-                        <TextBlock Text="Standard File Size"></TextBlock>
-                    </RadioButton>
-                    <RadioButton  Margin="0,0,0,8"  Command="{Binding LittleQualityCommand}" IsChecked="True">
-                        <TextBlock Text="Small File Size"></TextBlock>
-                    </RadioButton>
-                    <RadioButton  Margin="0,0,0,0"  Command="{Binding MicroQualityCommand}">
-                        <TextBlock Text="Minimum File Size"></TextBlock>
-                    </RadioButton>
-                </StackPanel>
+            <Grid Grid.Row="1" Width="436">
+                <StackPanel>
+                    <RadioButton   Margin="0,10,0,8"  Command="{Binding LargeQualityCommand}" >
+                        <TextBlock Text="Large Fill Size"></TextBlock>
+                    </RadioButton>
+
+                    <RadioButton  Margin="0,0,0,8"  Command="{Binding StandardQualityCommand}">
+                        <TextBlock Text="Standard File Size"></TextBlock>
+                    </RadioButton>
+                    <RadioButton  Margin="0,0,0,8"  Command="{Binding LittleQualityCommand}" IsChecked="True">
+                        <TextBlock Text="Small File Size"></TextBlock>
+                    </RadioButton>
+                    <RadioButton  Margin="0,0,0,0"  Command="{Binding MicroQualityCommand}">
+                        <TextBlock Text="Minimum File Size"></TextBlock>
+                    </RadioButton>
+                </StackPanel>
             </Grid>
-        </cus:DialogContent.Content>
-        <cus:DialogContent.BottmBar>
-        <Grid Grid.Row="2" Width="468"  VerticalAlignment="Center">
-            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
-                <Button Background="WhiteSmoke" Width="100" Height="25" Margin="10,0,0,0" Command="{Binding BatchCompressCommand}">
-                    <Border>
-                        <TextBlock Text="批量处理" ></TextBlock>
-                    </Border>
-                </Button>
-            </StackPanel>
-            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
-                <Button Background="WhiteSmoke" Width="100" Height="25" Margin="0,0,10,0" Command="{Binding CompressCommand}">
-                    <Border>
-                        <TextBlock Text="压缩"></TextBlock>
-                    </Border>
-                </Button>
-                <Button Background="WhiteSmoke" Width="100" Height="25" Margin="0,0,10,0" Command="{Binding ConfirmCompressCommand}">
-                    <Border>
-                        <TextBlock Text="取消" ></TextBlock>
-                    </Border>
-                </Button>
-            </StackPanel>
-        </Grid>
-        
+        </cus:DialogContent.Content>
+        <cus:DialogContent.BottmBar>
+        <Grid Grid.Row="2" Width="468"  VerticalAlignment="Center">
+            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
+                <Button Background="WhiteSmoke" Width="100" Height="25" Margin="10,0,0,0" Command="{Binding BatchCompressCommand}">
+                    <Border>
+                        <TextBlock Text="批量处理" ></TextBlock>
+                    </Border>
+                </Button>
+            </StackPanel>
+            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
+                <Button Background="WhiteSmoke" Width="100" Height="25" Margin="0,0,10,0" Command="{Binding CompressCommand}">
+                    <Border>
+                        <TextBlock Text="压缩"></TextBlock>
+                    </Border>
+                </Button>
+                <Button Background="WhiteSmoke" Width="100" Height="25" Margin="0,0,10,0" Command="{Binding ConfirmCompressCommand}">
+                    <Border>
+                        <TextBlock Text="取消" ></TextBlock>
+                    </Border>
+                </Button>
+            </StackPanel>
+        </Grid>
+        
         </cus:DialogContent.BottmBar>
     </cus:DialogContent>
-</UserControl>
+</UserControl>

+ 117 - 23
PDF Office/Views/Dialog/ToolsDialogs/SaftyDialogs/CheckPasswordDialog.xaml

@@ -1,28 +1,122 @@
 <UserControl x:Class="PDF_Office.Views.Dialog.ToolsDialogs.SaftyDialogs.CheckPasswordDialog"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-             xmlns:prism="http://prismlibrary.com/"             
+             xmlns:prism="http://prismlibrary.com/" xmlns:cus="clr-namespace:PDF_Office.CustomControl"
+             prism:Dialog.WindowStyle="{StaticResource DialogWindowStyle}"
+             xmlns:help="clr-namespace:PDF_Office.Helper" xmlns:saftydialogs="clr-namespace:PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
              prism:ViewModelLocator.AutoWireViewModel="True">
-    <Grid>
-        <Grid.RowDefinitions>
-            <RowDefinition Name="HeadRow" Height="95" />
-            <RowDefinition Height="50"/>
-        </Grid.RowDefinitions>
-        <StackPanel Grid.Row="0" Orientation="Vertical" VerticalAlignment="Center" Margin="20" Width="320">
-            <TextBlock  Text="该文件已开启权限保护,请输入密码" FontSize="14" Margin="0,0,0,15"/>
-            <PasswordBox Name="ForCheckPasswordBox" PasswordChanged="ForCheckPasswordBox_PasswordChanged"></PasswordBox>
-        </StackPanel>
-        <StackPanel Grid.Row="1"  Orientation="Horizontal" HorizontalAlignment="Right" >
-            <Button Background="WhiteSmoke" Width="100" Height="25" Margin="40,0,20,0" Command="{Binding  DelegateConfirmCheckPasswordCommand}">
-                <Border>
-                    <TextBlock Text="确定" ></TextBlock>
-                </Border>
-            </Button>
-            <Button Background="WhiteSmoke" Width="100" Height="25" Margin="0,0,10,0" Command="{Binding DelegateCancelCheckPasswordCommand}">
-                <Border>
-                    <TextBlock Text="取消" ></TextBlock>
-                </Border>
-            </Button>
-        </StackPanel>
-    </Grid>
+    <UserControl.Resources>
+        <Style TargetType="PasswordBox" x:Key="CheckPasswordBoxStyle">
+            <Setter Property="Height" Value="32"/>
+            <Setter Property="FontSize" Value="14"/>
+            <Setter Property="Template">
+                <Setter.Value>
+                    <ControlTemplate TargetType="PasswordBox">
+                        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" 
+                                    BorderThickness="{TemplateBinding BorderThickness}" 
+                                    Background="{TemplateBinding Background}" 
+                                    SnapsToDevicePixels="True"
+                                    CornerRadius="5">
+                            <Grid>
+                                <Grid.ColumnDefinitions>
+                                    <ColumnDefinition></ColumnDefinition>
+                                    <ColumnDefinition Width="24"></ColumnDefinition>
+                                </Grid.ColumnDefinitions>
+                                <Grid Grid.Column="0" Margin="8,0,0,0">
+                                    <TextBlock Text="{TemplateBinding help:PasswordBoxHelper.DisplayText}" Grid.Column="1" VerticalAlignment="Center" Foreground="#BBB"
+                                           Name="markText" Visibility="Collapsed" FontSize="12" />
+                                    <ScrollViewer x:Name="PART_ContentHost" Focusable="false"
+                                                  HorizontalScrollBarVisibility="Hidden" 
+                                                  VerticalScrollBarVisibility="Hidden"
+                                                  VerticalAlignment="Center" MinHeight="20"/>
+                                </Grid>
+                            </Grid>
+                        </Border>
+
+                        <ControlTemplate.Triggers>
+                            <Trigger Property="IsEnabled" Value="false">
+                                <Setter Property="Opacity" TargetName="border" Value="0.56"/>
+                            </Trigger>
+                            <Trigger Property="IsMouseOver" Value="true">
+                                <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
+                            </Trigger>
+                            <Trigger Property="IsKeyboardFocused" Value="true">
+                                <Setter Property="BorderBrush" TargetName="border" Value="#1770F4"/>
+                            </Trigger>
+                            <DataTrigger Binding="{Binding Path = Password}" Value="">
+                                <Setter Property="Visibility" TargetName="markText" Value="Visible"/>
+                            </DataTrigger>
+                            <DataTrigger Binding="{Binding Path=IsRightValue}" Value="false">
+                                <Setter Property="BorderBrush" TargetName="border" Value="#F3465B"/>
+                            </DataTrigger>
+                        </ControlTemplate.Triggers>
+                    </ControlTemplate>
+                </Setter.Value>
+            </Setter>
+        </Style>
+
+        <Style  TargetType="Button" x:Key="DeletePasswordButtonStyle">
+            <Setter Property="Height" Value="14"></Setter>
+            <Setter Property="Width" Value="14"></Setter>
+            <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
+            <Setter Property="VerticalContentAlignment" Value="Center"></Setter>
+            <Setter Property="Template">
+                <Setter.Value>
+                    <ControlTemplate TargetType="{x:Type ButtonBase}">
+                        <Grid>
+                            <Viewbox Stretch="Uniform">
+                                <Grid>
+                                    <Path Data="M7 14C10.866 14 14 10.866 14 7C14 3.13401 10.866 0 7 0C3.13401 0 0 3.13401 0 7C0 10.866 3.13401 14 7 14ZM10.5303 4.53038L8.06069 7L10.5303 9.46962L9.46967 10.5303L7.00002 8.06066L4.53033 10.5303L3.46967 9.46967L5.93936 7L3.46967 4.53033L4.53033 3.46967L7.00002 5.93934L9.46967 3.46971L10.5303 4.53038Z" Fill="#CED0D4"></Path>
+                                </Grid>
+                            </Viewbox>
+                        </Grid>
+                    </ControlTemplate>
+                </Setter.Value>
+            </Setter>
+        </Style>
+    </UserControl.Resources>
+    <cus:DialogContent Header="Open Pasword" FontFamily="Segoe UI">
+        <cus:DialogContent.Content>
+            <Grid Margin="16,0,16,0" Width="400" Height="106">
+                <Grid.ColumnDefinitions>
+                    <ColumnDefinition Width="80"></ColumnDefinition>
+                    <ColumnDefinition></ColumnDefinition>
+                </Grid.ColumnDefinitions>
+                <Image Source="pack://application:,,,/Resources/Dialog/password.png"  Margin="0,-20,0,0"></Image>
+                <Grid Grid.Column="1" Margin="8,0,0,0">
+                    <Grid.RowDefinitions>
+                        <RowDefinition Height="44"></RowDefinition>
+                        <RowDefinition Height="40"></RowDefinition>
+                        <RowDefinition></RowDefinition>
+                    </Grid.RowDefinitions>
+                    <Grid Grid.Row="0">
+                        <TextBlock TextWrapping="Wrap" Block.TextAlignment="Left" Text="{Binding InputPasswordMsg, Mode=TwoWay}" FontSize="14" FontFamily="Segoe UI"> </TextBlock>
+                    </Grid>
+                    <Grid  Grid.Row="1"  Margin="0,8,0,0">
+                        <Grid.ColumnDefinitions>
+                            <ColumnDefinition></ColumnDefinition>
+                            <ColumnDefinition Width="23"></ColumnDefinition>
+                        </Grid.ColumnDefinitions>
+                        <PasswordBox PasswordChar="*" Style="{StaticResource CheckPasswordBoxStyle}"  help:PasswordBoxHelper.Password="{Binding Password, Mode=TwoWay , UpdateSourceTrigger=PropertyChanged}" help:PasswordBoxHelper.DisplayText="{Binding CheckPasswordMsg, Mode=TwoWay}" help:PasswordBoxHelper.IsRightValue="{Binding IsRightValue, Mode=TwoWay}" Grid.ColumnSpan="2"></PasswordBox>
+                        <Button Style="{StaticResource DeletePasswordButtonStyle}" Grid.Column="1" HorizontalAlignment="Left" Command="{Binding ClearPasswordCommand}" Visibility="{Binding ClearPasswordButtonVisibility, Mode=TwoWay}"></Button>
+                    </Grid>
+                    <Grid Grid.Row="2" Margin="0,2,0,0">
+                        <TextBlock Text="{Binding ErrorPasswordMsg, Mode=TwoWay}" Foreground="#F3465B" Visibility="{Binding ErrorPasswordMsgVisibility, Mode=TwoWay}"></TextBlock>
+                    </Grid>
+                </Grid>
+            </Grid>
+        </cus:DialogContent.Content>
+        <cus:DialogContent.BottmBar>
+            <Grid Margin="16">
+                <StackPanel VerticalAlignment="Center" Height="32" Orientation="Horizontal" HorizontalAlignment="Right">
+                    <Button Width="67" Margin="0,0,16,0" Style="{StaticResource Btn.cta}" Command="{Binding ConfirmCommand}">
+                        <TextBlock Text="Open"></TextBlock>
+                    </Button>
+                    <Button Width="67" Style="{StaticResource btn.ghost}" Command="{Binding CancelCommand}">
+                        <TextBlock Text="Cancel"></TextBlock>
+                    </Button>
+                </StackPanel>
+            </Grid>
+        </cus:DialogContent.BottmBar>
+    </cus:DialogContent>
 </UserControl>

+ 0 - 6
PDF Office/Views/Dialog/ToolsDialogs/SaftyDialogs/CheckPasswordDialog.xaml.cs

@@ -12,12 +12,6 @@ namespace PDF_Office.Views.Dialog.ToolsDialogs.SaftyDialogs
         public CheckPasswordDialog()
         {
             InitializeComponent();
-            CheckPasswordDialogModel.Password=ForCheckPasswordBox.Password;
-        }
-
-        private void ForCheckPasswordBox_PasswordChanged(object sender, System.Windows.RoutedEventArgs e)
-        {
-            CheckPasswordDialogModel.Password = ForCheckPasswordBox.Password;
         }
     }
 }

+ 4 - 3
PDF Office/Views/Dialog/ToolsDialogs/SaftyDialogs/DeleteSafetySettingsDialog.xaml

@@ -5,6 +5,7 @@
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:saftydialogs="clr-namespace:PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs" d:DataContext="{d:DesignInstance Type=saftydialogs:DeleteSafetySettingsDialogViewModel}"
              prism:ViewModelLocator.AutoWireViewModel="True"
+           prism:Dialog.WindowStyle="{StaticResource DialogWindowStyle}"
              Height="156"
              Width="392"
              mc:Ignorable="d">
@@ -13,14 +14,14 @@
             <RowDefinition Name="HeadRow" Height="88" />
             <RowDefinition Height="80"/>
         </Grid.RowDefinitions>
-        <TextBlock Grid.Row="0" Text="{Binding DeleteSecurityMsg, Mode=TwoWay}" TextWrapping="Wrap" FontSize="14" Margin="16,32,16,0" Block.TextAlignment="Center"/>
+        <TextBlock Grid.Row="0" Text="{Binding DeleteSecurityMsg, Mode=TwoWay}" TextWrapping="Wrap" FontSize="14" Margin="16,32,16,0" Block.TextAlignment="Left"/>
         <StackPanel Grid.Row="1"  Orientation="Horizontal" HorizontalAlignment="Right">
-            <Button Background="WhiteSmoke" Width="100" Height="25" Margin="0,0,16,0" Command="{Binding DelegateCheckPasswordCommand}">
+            <Button Background="WhiteSmoke" Width="100" Height="25" Margin="0,0,16,0" Command="{Binding RemoveSecurityCommand}">
                 <Border>
                     <TextBlock Text="确定" ></TextBlock>
                 </Border>
             </Button>
-            <Button Background="WhiteSmoke" Width="100" Height="25" Margin="0,0,16,0" Command="{Binding DelegateCancelCommand}">
+            <Button Background="WhiteSmoke" Width="100" Height="25" Margin="0,0,16,0" Command="{Binding CancelCommand}">
                 <Border>
                     <TextBlock Text="取消" ></TextBlock>
                 </Border>