DecryptionDialog.xaml.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Runtime.CompilerServices;
  6. using System.Windows;
  7. using System.Windows.Forms;
  8. using Compdfkit_Tools.Common;
  9. using ComPDFKit.PDFDocument;
  10. namespace Compdfkit_Tools.PDFControl
  11. {
  12. public partial class DecryptionDialog : Window, INotifyPropertyChanged
  13. {
  14. private List<string> failedList = new List<string>();
  15. public event PropertyChangedEventHandler PropertyChanged;
  16. public DecryptionDialog()
  17. {
  18. InitializeComponent();
  19. DataContext = this;
  20. }
  21. private void Cancel_Click(object sender, RoutedEventArgs e)
  22. {
  23. this.Close();
  24. }
  25. private void Remove_Click(object sender, RoutedEventArgs e)
  26. {
  27. var dialog = new FolderBrowserDialog();
  28. dialog.ShowDialog();
  29. var savePath = dialog.SelectedPath;
  30. if (string.IsNullOrEmpty(savePath)) return;
  31. foreach (var fileInfoData in FileListControl.FileInfoDataList)
  32. {
  33. try
  34. {
  35. var fullSavePath = savePath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf";
  36. if(!fileInfoData.Document.Decrypt(fullSavePath))
  37. {
  38. failedList.Add(fileInfoData.Location);
  39. }
  40. fileInfoData.Document.Release();
  41. }
  42. catch (Exception exception)
  43. {
  44. failedList.Add(fileInfoData.Location);
  45. }
  46. }
  47. if (failedList.Count > 0)
  48. {
  49. var message = "Failed to remove password for the following files:\n" + string.Join("\n", failedList);
  50. System.Windows.MessageBox.Show(message, "Failed to remove password", MessageBoxButton.OK, MessageBoxImage.Warning);
  51. }
  52. if(failedList.Count < FileListControl.FileInfoDataList.Count)
  53. {
  54. System.Diagnostics.Process.Start(savePath);
  55. }
  56. this.Close();
  57. }
  58. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  59. {
  60. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  61. }
  62. }
  63. }