PasswordWindow.xaml.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using ComPDFKit.PDFDocument;
  2. using System;
  3. using System.Windows;
  4. using System.Windows.Input;
  5. using ComPDFKit.Controls.Helper;
  6. namespace ComPDFKit.Controls.Common
  7. {
  8. public enum PasswordType : byte
  9. {
  10. UserPassword,
  11. OwnerPassword
  12. }
  13. /// <summary>
  14. /// Interaction logic for PasswordWindow.xaml
  15. /// </summary>
  16. public partial class PasswordWindow : Window
  17. {
  18. private CPDFDocument pdfDoc;
  19. public PasswordType PasswordType = PasswordType.UserPassword;
  20. public delegate void DialogCloseEventHandler(object sender, PasswordEventArgs e);
  21. public event DialogCloseEventHandler DialogClosed;
  22. public string Password { get; private set; }
  23. public PasswordWindow()
  24. {
  25. InitializeComponent();
  26. }
  27. private void PasswordDialog_Loaded(object sender, RoutedEventArgs e)
  28. {
  29. }
  30. public void InitDocument(CPDFDocument pdfDoc)
  31. {
  32. this.pdfDoc = pdfDoc;
  33. PasswordDialog.Canceled -= PasswordDialog_Canceled;
  34. PasswordDialog.Confirmed -= PasswordDialog_Confirmed;
  35. PasswordDialog.Closed -= PasswordDialog_Closed;
  36. PasswordDialog.Canceled += PasswordDialog_Canceled;
  37. PasswordDialog.Confirmed += PasswordDialog_Confirmed;
  38. PasswordDialog.Closed += PasswordDialog_Closed;
  39. }
  40. private void PasswordDialog_Unloaded(object sender, RoutedEventArgs e)
  41. {
  42. PasswordDialog.Canceled -= PasswordDialog_Canceled;
  43. PasswordDialog.Confirmed -= PasswordDialog_Confirmed;
  44. PasswordDialog.Closed -= PasswordDialog_Closed;
  45. }
  46. private void PasswordDialog_Closed(object sender, EventArgs e)
  47. {
  48. PasswordEventArgs passwordEventArgs = new PasswordEventArgs(string.Empty);
  49. CloseWindow(passwordEventArgs);
  50. }
  51. private void PasswordDialog_Confirmed(object sender, string e)
  52. {
  53. string errorMessage = LanguageHelper.CommonManager.GetString("Tip_WrongPassword");
  54. if (pdfDoc != null)
  55. {
  56. if(PasswordType == PasswordType.UserPassword)
  57. {
  58. pdfDoc.UnlockWithPassword(e);
  59. if (pdfDoc.IsLocked == false)
  60. {
  61. PasswordEventArgs passwordEventArgs = new PasswordEventArgs(e);
  62. Password = e;
  63. CloseWindow(passwordEventArgs);
  64. }
  65. else
  66. {
  67. PasswordDialog.SetShowError(errorMessage, Visibility.Visible);
  68. }
  69. }
  70. else
  71. {
  72. if (pdfDoc.UnlockWithPassword(e))
  73. {
  74. if(pdfDoc.CheckOwnerPassword(e))
  75. {
  76. PasswordEventArgs passwordEventArgs = new PasswordEventArgs(e);
  77. Password = e;
  78. CloseWindow(passwordEventArgs);
  79. }
  80. else
  81. {
  82. PasswordDialog.SetShowError(errorMessage, Visibility.Visible);
  83. }
  84. }
  85. else
  86. {
  87. PasswordDialog.SetShowError(errorMessage, Visibility.Visible);
  88. }
  89. }
  90. }
  91. }
  92. private void PasswordDialog_Canceled(object sender, EventArgs e)
  93. {
  94. PasswordEventArgs passwordEventArgs = new PasswordEventArgs(string.Empty);
  95. CloseWindow(passwordEventArgs);
  96. }
  97. // The processing logic when the pop-up window is closed
  98. private void CloseWindow(PasswordEventArgs dialogResult)
  99. {
  100. // Trigger the close event and pass the return value
  101. DialogClosed?.Invoke(this, dialogResult);
  102. // Close the pop-up window
  103. Close();
  104. }
  105. private void PasswordWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  106. {
  107. if (e.LeftButton == MouseButtonState.Pressed)
  108. {
  109. DragMove();
  110. }
  111. }
  112. }
  113. public class PasswordEventArgs : EventArgs
  114. {
  115. public string DialogResult { get; set; }
  116. public PasswordEventArgs(string dialogResult)
  117. {
  118. DialogResult = dialogResult;
  119. }
  120. }
  121. }