SetPasswordDialogViewModel.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using ComPDFKit.PDFDocument;
  2. using Microsoft.Xaml.Behaviors.Layout;
  3. using PDF_Office.Model;
  4. using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
  5. using PDF_Office.Views.HomePanel.RecentFiles;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.Linq;
  13. using System.Windows.Controls;
  14. using System.Windows.Forms;
  15. using Forms = System.Windows.Forms;
  16. namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
  17. {
  18. public class SetPasswordDialogViewModel : BindableBase, IDialogAware
  19. {
  20. private SetPasswordDialogModel setPasswordDialogModel = new SetPasswordDialogModel();
  21. private CPDFDocument document;
  22. public DelegateCommand DelegateSetOpenCommand { get; set; }
  23. public DelegateCommand DelegateSetEditCommand { get; set; }
  24. public DelegateCommand DelegateCanOpenTextChangedCommand { get; set; }
  25. public DelegateCommand DelegateCanEditTextChangedCommand { get; set; }
  26. public DelegateCommand DelegateConfirmEncryptCommand { get; set; }
  27. public DelegateCommand DelegateCancelEncryptCommand { get; set; }
  28. public DelegateCommand InputPasswordCommand { get; set; }
  29. private bool _canOpen;
  30. public bool CanOpen
  31. {
  32. get { return _canOpen; }
  33. set
  34. {
  35. _canOpen = value;
  36. RaisePropertyChanged("isChecked");
  37. }
  38. }
  39. private bool _canEdit;
  40. public bool CanEdit
  41. {
  42. get { return _canEdit; }
  43. set
  44. {
  45. _canEdit = value;
  46. RaisePropertyChanged("isChecked");
  47. }
  48. }
  49. private string _changeMod="0";
  50. public string ChangeMod
  51. {
  52. get { return _changeMod; }
  53. set
  54. {
  55. _changeMod = value;
  56. }
  57. }
  58. private string _printMod="0";
  59. public string PrintMod
  60. {
  61. get { return _printMod; }
  62. set
  63. {
  64. _printMod = value;
  65. }
  66. }
  67. private string _isEnableConfirm = "False";
  68. public string IsEnabledConfirm
  69. {
  70. get { return _isEnableConfirm; }
  71. set
  72. {
  73. SetProperty(ref _isEnableConfirm, value);
  74. }
  75. }
  76. public SetPasswordDialogViewModel()
  77. {
  78. SetPasswordDialogModel.CanOpenPassword = "";
  79. SetPasswordDialogModel.CanEditPassword = "";
  80. DelegateConfirmEncryptCommand = new DelegateCommand(ConfirmEncrypt);
  81. DelegateCancelEncryptCommand = new DelegateCommand(CancelEncrypt);
  82. DelegateSetOpenCommand = new DelegateCommand(SetCanOpen);
  83. DelegateSetEditCommand = new DelegateCommand(SetCanEdit);
  84. DelegateCanOpenTextChangedCommand = new DelegateCommand(CanOpenTextChanged);
  85. DelegateCanEditTextChangedCommand = new DelegateCommand(CanEditTextChanged);
  86. }
  87. private void CheckCanConfirmEncrypt()
  88. {
  89. if (setPasswordDialogModel.CanOpen)
  90. {
  91. if (SetPasswordDialogModel.CanOpenPassword.Length > 0)
  92. {
  93. IsEnabledConfirm = "True";
  94. return;
  95. }
  96. }
  97. if (setPasswordDialogModel.CanEdit)
  98. {
  99. if (SetPasswordDialogModel.CanEditPassword.Length > 0)
  100. {
  101. IsEnabledConfirm = "True";
  102. return;
  103. }
  104. }
  105. IsEnabledConfirm = "False";
  106. }
  107. public void SetCanOpen()
  108. {
  109. setPasswordDialogModel.CanOpen = CanOpen;
  110. CheckCanConfirmEncrypt();
  111. }
  112. public void SetCanEdit()
  113. {
  114. setPasswordDialogModel.CanEdit = CanEdit;
  115. CheckCanConfirmEncrypt();
  116. }
  117. public void CanOpenTextChanged()
  118. {
  119. CheckCanConfirmEncrypt();
  120. }
  121. public void CanEditTextChanged()
  122. {
  123. CheckCanConfirmEncrypt();
  124. }
  125. private void InitPermissionsDictionary(ref Dictionary<string, SetPasswordDialogModel.PrintMod> GetPrintMod,
  126. ref Dictionary<string, SetPasswordDialogModel.ChangeMod> GetChangeMod)
  127. {
  128. GetPrintMod.Add("0", SetPasswordDialogModel.PrintMod.None);
  129. GetPrintMod.Add("1", SetPasswordDialogModel.PrintMod.LowDpi);
  130. GetPrintMod.Add("2", SetPasswordDialogModel.PrintMod.HighDpi);
  131. GetChangeMod.Add("0", SetPasswordDialogModel.ChangeMod.None);
  132. GetChangeMod.Add("1", SetPasswordDialogModel.ChangeMod.ChangePage);
  133. GetChangeMod.Add("2", SetPasswordDialogModel.ChangeMod.FormAndSignature);
  134. GetChangeMod.Add("3", SetPasswordDialogModel.ChangeMod.AnnotAndFormAndSignature);
  135. GetChangeMod.Add("4", SetPasswordDialogModel.ChangeMod.ExceptAbstrat);
  136. }
  137. public void ConfirmEncrypt()
  138. {
  139. string openPassword = "";
  140. string editPassword = "";
  141. FolderBrowserDialog folderDialog = new FolderBrowserDialog();
  142. Forms.SaveFileDialog sfd = new Forms.SaveFileDialog();
  143. /*
  144. *设置这个对话框的起始保存路径
  145. */
  146. sfd.InitialDirectory = document.FilePath;
  147. /*
  148. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  149. */
  150. sfd.Filter = "PDF|*.pdf;";
  151. /*
  152. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  153. **/
  154. sfd.FileName = document.FileName + "_SetPassword.pdf";
  155. if (sfd.ShowDialog() == Forms.DialogResult.OK)
  156. {
  157. /*
  158. * 做一些工作
  159. */
  160. CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo();
  161. if (setPasswordDialogModel.CanOpen)
  162. {
  163. if (!string.IsNullOrEmpty(SetPasswordDialogModel.CanOpenPassword))
  164. {
  165. openPassword = SetPasswordDialogModel.CanOpenPassword;
  166. }
  167. }
  168. if (setPasswordDialogModel.CanEdit)
  169. {
  170. if (!string.IsNullOrEmpty(SetPasswordDialogModel.CanEditPassword))
  171. {
  172. editPassword = SetPasswordDialogModel.CanEditPassword;
  173. Dictionary<string, SetPasswordDialogModel.PrintMod> GetPrintMod = new Dictionary<string, SetPasswordDialogModel.PrintMod>();
  174. Dictionary<string, SetPasswordDialogModel.ChangeMod> GetChangeMod = new Dictionary<string, SetPasswordDialogModel.ChangeMod>(); ;
  175. InitPermissionsDictionary(ref GetPrintMod, ref GetChangeMod);
  176. permissionsInfo = setPasswordDialogModel.CreatePermissionsInfo(GetPrintMod[PrintMod], GetChangeMod[ChangeMod]);
  177. }
  178. }
  179. document.Encrypt(openPassword, editPassword, permissionsInfo);
  180. document.WriteToFilePath(sfd.FileName);
  181. MessageBox.Show(sfd.FileName + " 保存成功");
  182. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  183. }
  184. }
  185. public void CancelEncrypt() =>
  186. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  187. public string Title => "";
  188. public event Action<IDialogResult> RequestClose;
  189. public bool CanCloseDialog()
  190. {
  191. return true;
  192. }
  193. public void OnDialogClosed()
  194. {
  195. }
  196. public void OnDialogOpened(IDialogParameters parameters)
  197. {
  198. CPDFDocument doc = null;
  199. parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
  200. if (doc != null)
  201. {
  202. document = doc;
  203. }
  204. }
  205. }
  206. }