SetPasswordDialogViewModel.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Office.Model;
  3. using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
  4. using PDF_Office.Views.HomePanel.RecentFiles;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Services.Dialogs;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Windows.Controls;
  13. using System.Windows.Forms;
  14. using Forms = System.Windows.Forms;
  15. namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
  16. {
  17. public class SetPasswordDialogViewModel : BindableBase, IDialogAware
  18. {
  19. private CPDFDocument document;
  20. public DelegateCommand ConfirmPasswordCommand { get; set; }
  21. public DelegateCommand InputPasswordCommand { get; set; }
  22. private PasswordModel passwordModel = new PasswordModel();
  23. private string _permissionPassword;
  24. public string PermissionPassword
  25. {
  26. get { return _permissionPassword; }
  27. set
  28. {
  29. _permissionPassword = value;
  30. RaisePropertyChanged("UserPassword");
  31. }
  32. }
  33. private string _allowsCopying;
  34. public string AllowsCopying
  35. {
  36. get { return _allowsCopying; }
  37. set
  38. {
  39. _allowsCopying = value;
  40. RaisePropertyChanged("UserPassword");
  41. }
  42. }
  43. private string _allowsPrinting;
  44. public string AllowsPrinting
  45. {
  46. get { return _allowsPrinting; }
  47. set
  48. {
  49. _allowsPrinting = value;
  50. RaisePropertyChanged("PermissionPassword");
  51. }
  52. }
  53. public SetPasswordDialogViewModel()
  54. {
  55. ConfirmPasswordCommand = new DelegateCommand(ConfirmPassword);
  56. InputPasswordCommand = new DelegateCommand(InputPassword);
  57. }
  58. public void ConfirmPassword()
  59. {
  60. FolderBrowserDialog folderDialog = new FolderBrowserDialog();
  61. Forms.SaveFileDialog sfd = new Forms.SaveFileDialog();
  62. /*
  63. *设置这个对话框的起始保存路径
  64. */
  65. sfd.InitialDirectory = @"D:\";
  66. /*
  67. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  68. */
  69. sfd.Filter = "PDF|*.pdf;";
  70. /*
  71. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  72. **/
  73. sfd.FileName = document.FileName + "_SetPassword.pdf";
  74. if (sfd.ShowDialog() == Forms.DialogResult.OK)
  75. {
  76. /*
  77. * 做一些工作
  78. */
  79. if (AllowsCopying == "True")
  80. {
  81. Trace.WriteLine("111111111"+"True");
  82. passwordModel.PermissionsInfo.AllowsCopying = true;
  83. }
  84. else {
  85. passwordModel.PermissionsInfo.AllowsCopying = false;
  86. }
  87. if (AllowsPrinting == "True")
  88. {
  89. passwordModel.PermissionsInfo.AllowsPrinting = true;
  90. }
  91. else {
  92. Trace.WriteLine("2222222"+"false");
  93. passwordModel.PermissionsInfo.AllowsPrinting = false;
  94. }
  95. passwordModel.PermissionPassword=PermissionPassword;
  96. document.Encrypt(PasswordModel.UserPassword, passwordModel.PermissionPassword, passwordModel.PermissionsInfo);
  97. document.WriteToFilePath(sfd.FileName);
  98. Trace.WriteLine(PasswordModel.UserPassword + "nouser"+ passwordModel.PermissionPassword + "copy"+passwordModel.PermissionsInfo.AllowsCopying+ "print"+ passwordModel.PermissionsInfo.AllowsPrinting);
  99. MessageBox.Show(sfd.FileName + " Saved Successfully.");
  100. }
  101. else
  102. {
  103. MessageBox.Show("Cancel.");
  104. }
  105. }
  106. public void InputPassword()
  107. {
  108. //UserPassword = PasswordModel.UserPassword;
  109. Trace.WriteLine("0_0");
  110. }
  111. public string Title => "";
  112. public event Action<IDialogResult> RequestClose;
  113. public bool CanCloseDialog()
  114. {
  115. return true;
  116. }
  117. public void OnDialogClosed()
  118. {
  119. }
  120. public void OnDialogOpened(IDialogParameters parameters)
  121. {
  122. CPDFDocument doc = null;
  123. parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
  124. if (doc != null)
  125. {
  126. document = doc;
  127. }
  128. }
  129. }
  130. }