EncryptionDialog.xaml.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.PDFDocument;
  12. namespace Compdfkit_Tools.PDFControl
  13. {
  14. public partial class EncryptionDialog : Window, INotifyPropertyChanged
  15. {
  16. public event PropertyChangedEventHandler PropertyChanged;
  17. public bool CanEncrypt => FileListControl.FileNum > 0 && SetEncryptionControl.ValidSetting;
  18. private CPDFDocument document;
  19. public EncryptionDialog()
  20. {
  21. InitializeComponent();
  22. DataContext = this;
  23. FileListControl.FileNumChanged += (sender, args) =>
  24. {
  25. OnPropertyChanged(nameof(CanEncrypt));
  26. };
  27. SetEncryptionControl.EncryptionSettingChanged += (sender, args) =>
  28. {
  29. OnPropertyChanged(nameof(CanEncrypt));
  30. };
  31. }
  32. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  33. {
  34. this.Close();
  35. }
  36. private void ButtonEncrypt_Click(object sender, RoutedEventArgs e)
  37. {
  38. var dialog = new FolderBrowserDialog();
  39. dialog.ShowDialog();
  40. var savePath = dialog.SelectedPath;
  41. var permissionsInfo = new CPDFPermissionsInfo();
  42. if (!SetEncryptionControl.IsOwnerPasswordEnabled || string.IsNullOrEmpty(SetEncryptionControl.OwnerPassword))
  43. {
  44. foreach (var fileInfoData in FileListControl.FileInfoDataList)
  45. {
  46. document = CPDFDocument.InitWithFilePath(fileInfoData.Location);
  47. document.Encrypt(SetEncryptionControl.UserPassword, null, permissionsInfo);
  48. document.WriteToFilePath(savePath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf");
  49. }
  50. }
  51. else
  52. {
  53. permissionsInfo.AllowsPrinting = SetEncryptionControl.IsAllowPrint;
  54. permissionsInfo.AllowsCopying = SetEncryptionControl.IsAllowCopy;
  55. foreach (var fileInfoData in FileListControl.FileInfoDataList)
  56. {
  57. document = CPDFDocument.InitWithFilePath(fileInfoData.Location);
  58. document.Encrypt(SetEncryptionControl.UserPassword, SetEncryptionControl.OwnerPassword, permissionsInfo);
  59. document.WriteToFilePath(savePath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf");
  60. }
  61. }
  62. }
  63. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  64. {
  65. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  66. }
  67. }
  68. }