CPDFSignatureListControl.xaml.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 = "Signature Details";
  40. viewSignatureDetailsMenu.Click += ViewSignatureDetailsMenu_Click;
  41. popContextMenu.Items.Add(viewSignatureDetailsMenu);
  42. MenuItem viewCertificateDetailsMenu = new MenuItem();
  43. viewCertificateDetailsMenu.Header = "Certificate Details";
  44. viewCertificateDetailsMenu.Click += ViewCertificateDetailsMenu_Click;
  45. popContextMenu.Items.Add(viewCertificateDetailsMenu);
  46. MenuItem deleteMenu = new MenuItem();
  47. deleteMenu.Header = "Delete";
  48. deleteMenu.Click += DeleteMenu_Click;
  49. popContextMenu.Items.Add(deleteMenu);
  50. }
  51. private void ViewSignatureDetailsMenu_Click(object sender, RoutedEventArgs e)
  52. {
  53. int index = SignatureList.SelectedIndex;
  54. if (index >= 0 && index < signatureList.Count)
  55. {
  56. ViewSignatureEvent?.Invoke(this, signatureList[index]);
  57. }
  58. }
  59. private void DeleteMenu_Click(object sender, RoutedEventArgs e)
  60. {
  61. // custom button content
  62. string messageBoxText = "Are you sure to delete it?";
  63. string caption = "Delete Signature";
  64. MessageBoxButton button = MessageBoxButton.OKCancel;
  65. MessageBoxImage icon = MessageBoxImage.Warning;
  66. MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, icon, MessageBoxResult.Cancel);
  67. if(result != MessageBoxResult.OK)
  68. {
  69. return;
  70. }
  71. int index = SignatureList.SelectedIndex;
  72. if (index >= 0 && index < signatureList.Count)
  73. {
  74. var widget = signatureList[index].GetSignatureWidget(pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument());
  75. pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument().RemoveSignature(signatureList[index], true);
  76. widget.ResetForm();
  77. widget.SetIsLocked(false);
  78. pdfViewer.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  79. DeleteSignatureEvent?.Invoke(this, null);
  80. }
  81. }
  82. private void ViewCertificateDetailsMenu_Click(object sender, RoutedEventArgs e)
  83. {
  84. int index = SignatureList.SelectedIndex;
  85. if (index >= 0 && index < signatureList.Count)
  86. {
  87. ViewCertificateEvent?.Invoke(this, signatureList[index]);
  88. }
  89. }
  90. public void InitWithPDFViewer(PDFViewControl newPDFView)
  91. {
  92. pdfViewer = newPDFView;
  93. }
  94. public void LoadSignatureList()
  95. {
  96. if (pdfViewer != null && pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument() != null)
  97. {
  98. signatureList = SignatureHelper.SignatureList;
  99. if (signatureList != null)
  100. {
  101. SignatureList.ItemsSource = null;
  102. signatureDataList = new List<SignatureData>();
  103. foreach (CPDFSignature signature in signatureList)
  104. {
  105. var item = new SignatureData();
  106. CPDFSigner signer = signature.SignerList.First();
  107. bool isSignVerified = signer.IsSignVerified;
  108. bool isCertTrusted = signer.IsCertTrusted;
  109. bool notModified = signature.ModifyInfoList.Count == 0;
  110. if (isSignVerified && isCertTrusted && notModified)
  111. {
  112. item.Status = SignatureStatus.Valid;
  113. }
  114. else if (isSignVerified && !isCertTrusted && notModified)
  115. {
  116. item.Status = SignatureStatus.Unknown;
  117. }
  118. else
  119. {
  120. item.Status = SignatureStatus.Invalid;
  121. }
  122. item.Signer = signature.Name + "'s signature";
  123. signatureDataList.Add(item);
  124. }
  125. if (signatureList.Count > 0)
  126. {
  127. SignatureList.ContextMenu = popContextMenu;
  128. }
  129. SignatureList.ItemsSource = signatureDataList;
  130. SignatureList.Visibility = Visibility.Visible;
  131. }
  132. else
  133. {
  134. SignatureList.Visibility = Visibility.Collapsed;
  135. }
  136. }
  137. }
  138. private void SignatureList_ContextMenuOpening(object sender, ContextMenuEventArgs e)
  139. {
  140. try
  141. {
  142. if (SignatureList != null && SignatureList.SelectedIndex == -1)
  143. {
  144. foreach (var item in popContextMenu.Items)
  145. {
  146. (item as MenuItem).IsEnabled = false;
  147. }
  148. }
  149. else
  150. {
  151. foreach (var item in popContextMenu.Items)
  152. {
  153. (item as MenuItem).IsEnabled = true;
  154. }
  155. }
  156. }
  157. catch (Exception ex)
  158. {
  159. }
  160. }
  161. private void SignatureList_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  162. {
  163. SignatureList.SelectedIndex = -1;
  164. }
  165. private void SignatureList_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  166. {
  167. int index = SignatureList.SelectedIndex;
  168. if (index >= 0 && index < signatureList.Count)
  169. {
  170. CRect rect = signatureList[index].GetPageBound(pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument());
  171. Point point = new Point(rect.left, rect.top);
  172. pdfViewer.PDFViewTool.GetCPDFViewer().GoToPage(signatureList[index].GetPageIndex(pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument()),point);
  173. }
  174. }
  175. }
  176. public class SignatureStatusToPathConverter : IValueConverter
  177. {
  178. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  179. {
  180. if (value is SignatureStatus signatureStatus && parameter is CPDFSignatureListControl cpdfSignatureListControl)
  181. {
  182. switch (signatureStatus)
  183. {
  184. case SignatureStatus.Valid:
  185. return cpdfSignatureListControl.FindResource("ValidSignaturePath");
  186. case SignatureStatus.Invalid:
  187. return cpdfSignatureListControl.FindResource("InvalidSignaturePath");
  188. case SignatureStatus.Unknown:
  189. return cpdfSignatureListControl.FindResource("UnknownSignaturePath");
  190. default:
  191. return null;
  192. }
  193. }
  194. return null;
  195. }
  196. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  197. {
  198. return null;
  199. }
  200. }
  201. }