SetPasswordDialogViewModel.cs 8.7 KB

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