CPDFSignatureListControl.xaml.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Input;
  9. using Compdfkit_Tools.Helper;
  10. using Compdfkit_Tools.PDFControl;
  11. using ComPDFKit.DigitalSign;
  12. using ComPDFKit.Import;
  13. using ContextMenu = System.Windows.Controls.ContextMenu;
  14. using MenuItem = System.Windows.Controls.MenuItem;
  15. using MessageBox = System.Windows.MessageBox;
  16. using UserControl = System.Windows.Controls.UserControl;
  17. namespace Compdfkit_Tools.DigitalSignature.CPDFSignatureListControl
  18. {
  19. public class SignatureData
  20. {
  21. public SignatureStatus Status { get; set; }
  22. public string Signer { get; set; }
  23. }
  24. public partial class CPDFSignatureListControl : UserControl
  25. {
  26. private PDFViewControl pdfViewer;
  27. private List<SignatureData> signatureDataList;
  28. private List<CPDFSignature> signatureList;
  29. private ContextMenu popContextMenu;
  30. public event EventHandler<CPDFSignature> ViewCertificateEvent;
  31. public event EventHandler<CPDFSignature> ViewSignatureEvent;
  32. public event EventHandler DeleteSignatureEvent;
  33. public CPDFSignatureListControl()
  34. {
  35. InitializeComponent();
  36. DataContext = this;
  37. popContextMenu = new ContextMenu();
  38. MenuItem viewSignatureDetailsMenu = new MenuItem();
  39. viewSignatureDetailsMenu.Header = LanguageHelper.BotaManager.GetString("Menu_SigDetail");
  40. viewSignatureDetailsMenu.Click -= ViewSignatureDetailsMenu_Click;
  41. viewSignatureDetailsMenu.Click += ViewSignatureDetailsMenu_Click;
  42. popContextMenu.Items.Add(viewSignatureDetailsMenu);
  43. MenuItem viewCertificateDetailsMenu = new MenuItem();
  44. viewCertificateDetailsMenu.Header = LanguageHelper.BotaManager.GetString("Menu_CertDetail");
  45. viewCertificateDetailsMenu.Click -= ViewCertificateDetailsMenu_Click;
  46. viewCertificateDetailsMenu.Click += ViewCertificateDetailsMenu_Click;
  47. popContextMenu.Items.Add(viewCertificateDetailsMenu);
  48. MenuItem deleteMenu = new MenuItem();
  49. deleteMenu.Header = LanguageHelper.BotaManager.GetString("Menu_Delete");
  50. deleteMenu.Click -= DeleteMenu_Click;
  51. deleteMenu.Click += DeleteMenu_Click;
  52. popContextMenu.Items.Add(deleteMenu);
  53. }
  54. private void ViewSignatureDetailsMenu_Click(object sender, RoutedEventArgs e)
  55. {
  56. int index = SignatureList.SelectedIndex;
  57. if (index >= 0 && index < signatureList.Count)
  58. {
  59. ViewSignatureEvent?.Invoke(this, signatureList[index]);
  60. }
  61. }
  62. private void DeleteMenu_Click(object sender, RoutedEventArgs e)
  63. {
  64. string messageBoxText = LanguageHelper.BotaManager.GetString("Text_SureDelete");
  65. MessageBoxButton button = MessageBoxButton.OKCancel;
  66. MessageBoxImage icon = MessageBoxImage.Warning;
  67. MessageBoxResult result = MessageBox.Show(messageBoxText, LanguageHelper.CommonManager.GetString("Caption_Warning"), button, icon, MessageBoxResult.Cancel);
  68. if(result != MessageBoxResult.OK)
  69. {
  70. return;
  71. }
  72. int index = SignatureList.SelectedIndex;
  73. if (index >= 0 && index < signatureList.Count)
  74. {
  75. var widget = signatureList[index].GetSignatureWidget(pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument());
  76. pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument().RemoveSignature(signatureList[index], true);
  77. widget.ResetForm();
  78. widget.SetIsLocked(false);
  79. pdfViewer.PDFViewTool.GetCPDFViewer().UpdateAnnotFrame();
  80. DeleteSignatureEvent?.Invoke(this, null);
  81. }
  82. }
  83. private void ViewCertificateDetailsMenu_Click(object sender, RoutedEventArgs e)
  84. {
  85. int index = SignatureList.SelectedIndex;
  86. if (index >= 0 && index < signatureList.Count)
  87. {
  88. ViewCertificateEvent?.Invoke(this, signatureList[index]);
  89. }
  90. }
  91. public void InitWithPDFViewer(PDFViewControl newPDFView)
  92. {
  93. pdfViewer = newPDFView;
  94. }
  95. public void LoadSignatureList()
  96. {
  97. if (pdfViewer != null && pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument() != null)
  98. {
  99. signatureList = SignatureHelper.SignatureList;
  100. if (signatureList != null)
  101. {
  102. SignatureList.ItemsSource = null;
  103. signatureDataList = new List<SignatureData>();
  104. foreach (CPDFSignature signature in signatureList)
  105. {
  106. var item = new SignatureData();
  107. CPDFSigner signer = signature.SignerList.First();
  108. bool isSignVerified = signer.IsSignVerified;
  109. bool isCertTrusted = signer.IsCertTrusted;
  110. bool notModified = signature.ModifyInfoList.Count == 0;
  111. if (isSignVerified && isCertTrusted && notModified)
  112. {
  113. item.Status = SignatureStatus.Valid;
  114. }
  115. else if (isSignVerified && !isCertTrusted && notModified)
  116. {
  117. item.Status = SignatureStatus.Unknown;
  118. }
  119. else
  120. {
  121. item.Status = SignatureStatus.Invalid;
  122. }
  123. item.Signer = signature.Name + "'s signature";
  124. signatureDataList.Add(item);
  125. }
  126. if (signatureList.Count > 0)
  127. {
  128. SignatureList.ContextMenu = popContextMenu;
  129. }
  130. SignatureList.ItemsSource = signatureDataList;
  131. SignatureList.Visibility = Visibility.Visible;
  132. }
  133. else
  134. {
  135. SignatureList.Visibility = Visibility.Collapsed;
  136. }
  137. }
  138. }
  139. private void SignatureList_ContextMenuOpening(object sender, ContextMenuEventArgs e)
  140. {
  141. try
  142. {
  143. if (SignatureList != null && SignatureList.SelectedIndex == -1)
  144. {
  145. foreach (var item in popContextMenu.Items)
  146. {
  147. (item as MenuItem).IsEnabled = false;
  148. }
  149. }
  150. else
  151. {
  152. foreach (var item in popContextMenu.Items)
  153. {
  154. (item as MenuItem).IsEnabled = true;
  155. }
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. }
  161. }
  162. private void SignatureList_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  163. {
  164. SignatureList.SelectedIndex = -1;
  165. }
  166. private void SignatureList_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  167. {
  168. int index = SignatureList.SelectedIndex;
  169. if (index >= 0 && index < signatureList.Count)
  170. {
  171. CRect rect = signatureList[index].GetPageBound(pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument());
  172. Point point = new Point(rect.left, rect.top);
  173. pdfViewer.PDFViewTool.GetCPDFViewer().GoToPage(signatureList[index].GetPageIndex(pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument()),point);
  174. }
  175. }
  176. }
  177. public class SignatureStatusToPathConverter : IValueConverter
  178. {
  179. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  180. {
  181. if (value is SignatureStatus signatureStatus && parameter is CPDFSignatureListControl cpdfSignatureListControl)
  182. {
  183. switch (signatureStatus)
  184. {
  185. case SignatureStatus.Valid:
  186. return cpdfSignatureListControl.FindResource("ValidSignaturePath");
  187. case SignatureStatus.Invalid:
  188. return cpdfSignatureListControl.FindResource("InvalidSignaturePath");
  189. case SignatureStatus.Unknown:
  190. return cpdfSignatureListControl.FindResource("UnknownSignaturePath");
  191. default:
  192. return null;
  193. }
  194. }
  195. return null;
  196. }
  197. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  198. {
  199. return null;
  200. }
  201. }
  202. }