PasswordWindow.xaml.cs 4.3 KB

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