EncryptionDialog.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Forms;
  11. using Compdfkit_Tools.Common;
  12. using ComPDFKit.PDFDocument;
  13. using MessageBox = System.Windows.MessageBox;
  14. namespace Compdfkit_Tools.PDFControl
  15. {
  16. public partial class EncryptionDialog : Window, INotifyPropertyChanged
  17. {
  18. private int _currentFileNum;
  19. public int CurrentFileNum
  20. {
  21. get => _currentFileNum;
  22. set => UpdateProper(ref _currentFileNum, value);
  23. }
  24. private int _fileNum;
  25. public int FileNum
  26. {
  27. get => _fileNum;
  28. set => UpdateProper(ref _fileNum, value);
  29. }
  30. private bool isEnsure;
  31. public event PropertyChangedEventHandler PropertyChanged;
  32. public bool CanEncrypt
  33. {
  34. get { return FileListControl.FileInfoDataList.Count > 0 && SetEncryptionControl.IsSettingValid; }
  35. }
  36. private List<string> failedList = new List<string>();
  37. public EncryptionDialog()
  38. {
  39. InitializeComponent();
  40. DataContext = this;
  41. CurrentFileNum = 0;
  42. }
  43. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  44. {
  45. this.Close();
  46. }
  47. private void ButtonEncrypt_Click(object sender, RoutedEventArgs e)
  48. {
  49. if(SetEncryptionControl.OwnerPassword == SetEncryptionControl.OwnerPassword)
  50. {
  51. MessageBox.Show("Open password and permission password must be different", "Warning", MessageBoxButton.OK);
  52. return;
  53. }
  54. var dialog = new FolderBrowserDialog();
  55. dialog.ShowDialog();
  56. var savePath = dialog.SelectedPath;
  57. if (string.IsNullOrEmpty(savePath)) return;
  58. CurrentFileNum = 0;
  59. var permissionsInfo = new CPDFPermissionsInfo();
  60. string userPassword = string.Empty;
  61. string ownerPassword = string.Empty;
  62. if (SetEncryptionControl.IsOwnerPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.OwnerPassword))
  63. {
  64. permissionsInfo.AllowsPrinting = SetEncryptionControl.IsAllowPrint;
  65. permissionsInfo.AllowsCopying = SetEncryptionControl.IsAllowCopy;
  66. ownerPassword = SetEncryptionControl.OwnerPassword;
  67. }
  68. if (SetEncryptionControl.IsUserPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.UserPassword))
  69. {
  70. userPassword = SetEncryptionControl.UserPassword;
  71. }
  72. foreach (var fileInfoData in FileListControl.FileInfoDataList)
  73. {
  74. try
  75. {
  76. var fullSavePath = savePath + Path.DirectorySeparatorChar +
  77. Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf";
  78. fileInfoData.Document.Encrypt(userPassword, ownerPassword, permissionsInfo,
  79. (CPDFDocumentEncryptionLevel)SetEncryptionControl.CryptographicLevel);
  80. if (!fileInfoData.Document.WriteToFilePath(fullSavePath))
  81. {
  82. failedList.Add(fileInfoData.Location);
  83. }
  84. fileInfoData.Document.Release();
  85. }
  86. catch (Exception exception)
  87. {
  88. failedList.Add(fileInfoData.Location);
  89. }
  90. }
  91. if (failedList.Count > 0)
  92. {
  93. System.Windows.MessageBox.Show("Failed to encrypt the following files:\n" + string.Join("\n", failedList));
  94. }
  95. if (failedList.Count < FileListControl.FileInfoDataList.Count)
  96. {
  97. System.Diagnostics.Process.Start(savePath);
  98. }
  99. this.Close();
  100. }
  101. protected override void OnClosed(EventArgs e)
  102. {
  103. foreach (var fileInfo in FileListControl.FileInfoDataList)
  104. {
  105. fileInfo.Document.Release();
  106. }
  107. base.OnClosed(e);
  108. }
  109. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  110. {
  111. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  112. }
  113. protected void UpdateProper<T>(ref T properValue,
  114. T newValue,
  115. [CallerMemberName] string properName = "")
  116. {
  117. if (object.Equals(properValue, newValue))
  118. return;
  119. properValue = newValue;
  120. OnPropertyChanged(properName);
  121. }
  122. }
  123. }