EncryptionDialog.xaml.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. namespace Compdfkit_Tools.PDFControl
  14. {
  15. public partial class EncryptionDialog : Window, INotifyPropertyChanged
  16. {
  17. private int _currentFileNum;
  18. public int CurrentFileNum
  19. {
  20. get => _currentFileNum;
  21. set => UpdateProper(ref _currentFileNum, value);
  22. }
  23. private int _fileNum;
  24. public int FileNum
  25. {
  26. get => _fileNum;
  27. set => UpdateProper(ref _fileNum,value);
  28. }
  29. private bool isEnsure;
  30. public event PropertyChangedEventHandler PropertyChanged;
  31. public bool CanEncrypt
  32. {
  33. get { return FileListControl.FileInfoDataList.Count > 0 && SetEncryptionControl.IsSettingValid; }
  34. }
  35. private List<string> failedList = new List<string>();
  36. public EncryptionDialog()
  37. {
  38. InitializeComponent();
  39. DataContext = this;
  40. CurrentFileNum = 0;
  41. }
  42. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  43. {
  44. this.Close();
  45. }
  46. private void ButtonEncrypt_Click(object sender, RoutedEventArgs e)
  47. {
  48. var dialog = new FolderBrowserDialog();
  49. dialog.ShowDialog();
  50. var savePath = dialog.SelectedPath;
  51. if (string.IsNullOrEmpty(savePath)) return;
  52. CurrentFileNum = 0;
  53. var permissionsInfo = new CPDFPermissionsInfo();
  54. string userPassword = string.Empty;
  55. string ownerPassword = string.Empty;
  56. if (SetEncryptionControl.IsOwnerPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.OwnerPassword))
  57. {
  58. permissionsInfo.AllowsPrinting = SetEncryptionControl.IsAllowPrint;
  59. permissionsInfo.AllowsCopying = SetEncryptionControl.IsAllowCopy;
  60. ownerPassword = SetEncryptionControl.OwnerPassword;
  61. }
  62. if(SetEncryptionControl.IsUserPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.UserPassword))
  63. {
  64. userPassword = SetEncryptionControl.UserPassword;
  65. }
  66. foreach (var fileInfoData in FileListControl.FileInfoDataList)
  67. {
  68. try
  69. {
  70. var fullSavePath = savePath + Path.DirectorySeparatorChar +
  71. Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf";
  72. fileInfoData.Document.Encrypt(userPassword, ownerPassword, permissionsInfo,
  73. (CPDFDocumentEncryptionLevel)SetEncryptionControl.CryptographicLevel);
  74. if (!fileInfoData.Document.WriteToFilePath(fullSavePath))
  75. {
  76. failedList.Add(fileInfoData.Location);
  77. }
  78. fileInfoData.Document.Release();
  79. }
  80. catch (Exception exception)
  81. {
  82. failedList.Add(fileInfoData.Location);
  83. }
  84. }
  85. if(failedList.Count > 0)
  86. {
  87. System.Windows.MessageBox.Show("Failed to encrypt the following files:\n" + string.Join("\n", failedList));
  88. }
  89. if(failedList.Count < FileListControl.FileInfoDataList.Count)
  90. {
  91. System.Diagnostics.Process.Start(savePath);
  92. }
  93. this.Close();
  94. }
  95. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  96. {
  97. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  98. }
  99. protected void UpdateProper<T>(ref T properValue,
  100. T newValue,
  101. [CallerMemberName] string properName = "")
  102. {
  103. if (object.Equals(properValue, newValue))
  104. return;
  105. properValue = newValue;
  106. OnPropertyChanged(properName);
  107. }
  108. }
  109. }