DecryptionDialog.xaml.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 CPDFDocument document;
  15. private List<string> failedList = new List<string>();
  16. public event PropertyChangedEventHandler PropertyChanged;
  17. public DecryptionDialog()
  18. {
  19. InitializeComponent();
  20. DataContext = this;
  21. }
  22. private void Cancel_Click(object sender, RoutedEventArgs e)
  23. {
  24. this.Close();
  25. }
  26. private void Remove_Click(object sender, RoutedEventArgs e)
  27. {
  28. var dialog = new FolderBrowserDialog();
  29. dialog.ShowDialog();
  30. var savePath = dialog.SelectedPath;
  31. if (string.IsNullOrEmpty(savePath)) return;
  32. foreach (var fileInfoData in FileListControl.FileInfoDataList)
  33. {
  34. document = CPDFDocument.InitWithFilePath(fileInfoData.Location);
  35. if (document.IsLocked)
  36. {
  37. PasswordWindow window = new PasswordWindow();
  38. window.InitWithDocument(document);
  39. window.Owner = this;
  40. window.ShowDialog();
  41. if (document.IsLocked)
  42. {
  43. failedList.Add(fileInfoData.Location);
  44. continue;
  45. }
  46. }
  47. var fullSavePath = savePath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf";
  48. if(!document.Decrypt(fullSavePath))
  49. {
  50. failedList.Add(fileInfoData.Location);
  51. }
  52. document.Release();
  53. }
  54. if (failedList.Count > 0)
  55. {
  56. var message = "Failed to remove password for the following files:\n" + string.Join("\n", failedList);
  57. System.Windows.MessageBox.Show(message, "Failed to remove password", MessageBoxButton.OK, MessageBoxImage.Warning);
  58. }
  59. if(failedList.Count < FileListControl.FileInfoDataList.Count)
  60. {
  61. System.Diagnostics.Process.Start(savePath);
  62. }
  63. this.Close();
  64. }
  65. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  66. {
  67. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  68. }
  69. }
  70. }