EncryptionDialog.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.IsUserPasswordEnabled && SetEncryptionControl.IsOwnerPasswordEnabled)
  50. {
  51. if(SetEncryptionControl.UserPassword == SetEncryptionControl.OwnerPassword)
  52. {
  53. MessageBox.Show("Open password and permission password must be different", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
  54. return;
  55. }
  56. }
  57. var dialog = new FolderBrowserDialog();
  58. dialog.ShowDialog();
  59. var savePath = dialog.SelectedPath;
  60. if (string.IsNullOrEmpty(savePath)) return;
  61. CurrentFileNum = 0;
  62. var permissionsInfo = new CPDFPermissionsInfo();
  63. string userPassword = string.Empty;
  64. string ownerPassword = string.Empty;
  65. if (SetEncryptionControl.IsOwnerPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.OwnerPassword))
  66. {
  67. permissionsInfo.AllowsPrinting = SetEncryptionControl.IsAllowPrint;
  68. permissionsInfo.AllowsCopying = SetEncryptionControl.IsAllowCopy;
  69. ownerPassword = SetEncryptionControl.OwnerPassword;
  70. }
  71. if (SetEncryptionControl.IsUserPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.UserPassword))
  72. {
  73. userPassword = SetEncryptionControl.UserPassword;
  74. }
  75. foreach (var fileInfoData in FileListControl.FileInfoDataList)
  76. {
  77. try
  78. {
  79. var fullSavePath = savePath + Path.DirectorySeparatorChar +
  80. Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf";
  81. fileInfoData.Document.Encrypt(userPassword, ownerPassword, permissionsInfo,
  82. (CPDFDocumentEncryptionLevel)SetEncryptionControl.CryptographicLevel);
  83. if (!fileInfoData.Document.WriteToFilePath(fullSavePath))
  84. {
  85. failedList.Add(fileInfoData.Location);
  86. }
  87. fileInfoData.Document.Release();
  88. }
  89. catch (Exception exception)
  90. {
  91. failedList.Add(fileInfoData.Location);
  92. }
  93. }
  94. if (failedList.Count > 0)
  95. {
  96. System.Windows.MessageBox.Show("Failed to encrypt the following files:\n" + string.Join("\n", failedList));
  97. }
  98. if (failedList.Count < FileListControl.FileInfoDataList.Count)
  99. {
  100. System.Diagnostics.Process.Start(savePath);
  101. }
  102. this.Close();
  103. }
  104. protected override void OnClosed(EventArgs e)
  105. {
  106. foreach (var fileInfo in FileListControl.FileInfoDataList)
  107. {
  108. fileInfo.Document.Release();
  109. }
  110. base.OnClosed(e);
  111. }
  112. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  113. {
  114. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  115. }
  116. protected void UpdateProper<T>(ref T properValue,
  117. T newValue,
  118. [CallerMemberName] string properName = "")
  119. {
  120. if (object.Equals(properValue, newValue))
  121. return;
  122. properValue = newValue;
  123. OnPropertyChanged(properName);
  124. }
  125. }
  126. }