PasswordWindow.xaml.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using ComPDFKitViewer.PdfViewer;
  2. using System;
  3. using System.Windows;
  4. using System.Windows.Input;
  5. using ComPDFKit.PDFDocument;
  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 document;
  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. PasswordDialog.Canceled += PasswordDialog_Canceled;
  29. PasswordDialog.Confirmed += PasswordDialog_Confirmed;
  30. PasswordDialog.Closed += PasswordDialog_Closed;
  31. }
  32. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  33. {
  34. document = pdfViewer.Document;
  35. }
  36. public void InitWithDocument(CPDFDocument document)
  37. {
  38. this.document = document;
  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. if (document != null)
  54. {
  55. if(PasswordType == PasswordType.UserPassword)
  56. {
  57. document.UnlockWithPassword(e);
  58. if (document.IsLocked == false)
  59. {
  60. PasswordEventArgs passwordEventArgs = new PasswordEventArgs(e);
  61. CloseWindow(passwordEventArgs);
  62. }
  63. else
  64. {
  65. PasswordDialog.SetShowError("Wrong Password", Visibility.Visible);
  66. }
  67. }
  68. else
  69. {
  70. if (document.CheckOwnerPassword(e))
  71. {
  72. PasswordEventArgs passwordEventArgs = new PasswordEventArgs(e);
  73. CloseWindow(passwordEventArgs);
  74. }
  75. else
  76. {
  77. PasswordDialog.SetShowError("Wrong Password", Visibility.Visible);
  78. }
  79. }
  80. }
  81. }
  82. private void PasswordDialog_Canceled(object sender, EventArgs e)
  83. {
  84. PasswordEventArgs passwordEventArgs = new PasswordEventArgs(string.Empty);
  85. CloseWindow(passwordEventArgs);
  86. }
  87. // The processing logic when the pop-up window is closed
  88. private void CloseWindow(PasswordEventArgs dialogResult)
  89. {
  90. // Trigger the close event and pass the return value
  91. DialogClosed?.Invoke(this, dialogResult);
  92. // Close the pop-up window
  93. Close();
  94. }
  95. private void PasswordWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  96. {
  97. if (e.LeftButton == MouseButtonState.Pressed)
  98. {
  99. DragMove();
  100. }
  101. }
  102. }
  103. public class PasswordEventArgs : EventArgs
  104. {
  105. public string DialogResult { get; set; }
  106. public PasswordEventArgs(string dialogResult)
  107. {
  108. DialogResult = dialogResult;
  109. }
  110. }
  111. }