DecryptionDialog.xaml.cs 2.5 KB

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