EncryptionDialog.xaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. public event PropertyChangedEventHandler PropertyChanged;
  18. public bool CanEncrypt => FileListControl.FileNum > 0 && SetEncryptionControl.ValidSetting;
  19. private CPDFDocument document;
  20. public EncryptionDialog()
  21. {
  22. InitializeComponent();
  23. DataContext = this;
  24. FileListControl.FileNumChanged += (sender, args) =>
  25. {
  26. OnPropertyChanged(nameof(CanEncrypt));
  27. };
  28. SetEncryptionControl.EncryptionSettingChanged += (sender, args) =>
  29. {
  30. OnPropertyChanged(nameof(CanEncrypt));
  31. };
  32. }
  33. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  34. {
  35. this.Close();
  36. }
  37. private void ButtonEncrypt_Click(object sender, RoutedEventArgs e)
  38. {
  39. var dialog = new FolderBrowserDialog();
  40. dialog.ShowDialog();
  41. var savePath = dialog.SelectedPath;
  42. if (string.IsNullOrEmpty(savePath)) return;
  43. var permissionsInfo = new CPDFPermissionsInfo();
  44. string userPassword = string.Empty;
  45. string ownerPassword = string.Empty;
  46. if (SetEncryptionControl.IsOwnerPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.OwnerPassword))
  47. {
  48. permissionsInfo.AllowsPrinting = SetEncryptionControl.IsAllowPrint;
  49. permissionsInfo.AllowsCopying = SetEncryptionControl.IsAllowCopy;
  50. ownerPassword = SetEncryptionControl.OwnerPassword;
  51. }
  52. if(SetEncryptionControl.IsUserPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.UserPassword))
  53. {
  54. userPassword = SetEncryptionControl.UserPassword;
  55. }
  56. foreach (var fileInfoData in FileListControl.FileInfoDataList)
  57. {
  58. document = CPDFDocument.InitWithFilePath(fileInfoData.Location);
  59. if (document.IsLocked)
  60. {
  61. PasswordWindow window = new PasswordWindow();
  62. window.InitWithDocument(document);
  63. window.Owner = this;
  64. window.ShowDialog();
  65. if (document.IsLocked)
  66. {
  67. continue;
  68. }
  69. }
  70. document.Encrypt(userPassword, ownerPassword, permissionsInfo);
  71. document.WriteToFilePath(savePath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf");
  72. document.Release();
  73. }
  74. }
  75. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  76. {
  77. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  78. }
  79. }
  80. }