PasswordWindow.xaml.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using ComPDFKit.PDFDocument;
  2. using System;
  3. using System.Windows;
  4. namespace Compdfkit_Tools.Common
  5. {
  6. /// <summary>
  7. /// Interaction logic for PasswordWindow.xaml
  8. /// </summary>
  9. public partial class PasswordWindow : Window
  10. {
  11. private CPDFDocument pdfDoc;
  12. public delegate void DialogCloseEventHandler(object sender, PasswordEventArgs e);
  13. public event DialogCloseEventHandler DialogClosed;
  14. public PasswordWindow()
  15. {
  16. InitializeComponent();
  17. }
  18. private void PasswordDialog_Loaded(object sender, RoutedEventArgs e)
  19. {
  20. PasswordDialog.Canceled += PasswordDialog_Canceled;
  21. PasswordDialog.Confirmed += PasswordDialog_Confirmed;
  22. PasswordDialog.Closed += PasswordDialog_Closed;
  23. }
  24. public void InitDocument(CPDFDocument pdfDoc)
  25. {
  26. this.pdfDoc = pdfDoc;
  27. }
  28. private void PasswordDialog_Unloaded(object sender, RoutedEventArgs e)
  29. {
  30. PasswordDialog.Canceled -= PasswordDialog_Canceled;
  31. PasswordDialog.Confirmed -= PasswordDialog_Confirmed;
  32. PasswordDialog.Closed -= PasswordDialog_Closed;
  33. }
  34. private void PasswordDialog_Closed(object sender, EventArgs e)
  35. {
  36. PasswordEventArgs passwordEventArgs = new PasswordEventArgs(string.Empty);
  37. CloseWindow(passwordEventArgs);
  38. }
  39. private void PasswordDialog_Confirmed(object sender, string e)
  40. {
  41. if (pdfDoc != null)
  42. {
  43. pdfDoc.UnlockWithPassword(e);
  44. if (pdfDoc.IsLocked == false)
  45. {
  46. PasswordEventArgs passwordEventArgs = new PasswordEventArgs(e);
  47. CloseWindow(passwordEventArgs);
  48. }
  49. else
  50. {
  51. PasswordDialog.SetShowError("Wrong Password", Visibility.Visible);
  52. }
  53. }
  54. }
  55. private void PasswordDialog_Canceled(object sender, EventArgs e)
  56. {
  57. PasswordEventArgs passwordEventArgs = new PasswordEventArgs(string.Empty);
  58. CloseWindow(passwordEventArgs);
  59. }
  60. // The processing logic when the pop-up window is closed
  61. private void CloseWindow(PasswordEventArgs dialogResult)
  62. {
  63. // Trigger the close event and pass the return value
  64. DialogClosed?.Invoke(this, dialogResult);
  65. // Close the pop-up window
  66. Close();
  67. }
  68. }
  69. public class PasswordEventArgs : EventArgs
  70. {
  71. public string DialogResult { get; set; }
  72. public PasswordEventArgs(string dialogResult)
  73. {
  74. DialogResult = dialogResult;
  75. }
  76. }
  77. }