123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Input;
- using Compdfkit_Tools.Helper;
- using Compdfkit_Tools.PDFControl;
- using ComPDFKit.DigitalSign;
- using ComPDFKit.Import;
- using ContextMenu = System.Windows.Controls.ContextMenu;
- using MenuItem = System.Windows.Controls.MenuItem;
- using MessageBox = System.Windows.MessageBox;
- using UserControl = System.Windows.Controls.UserControl;
- namespace Compdfkit_Tools.DigitalSignature.CPDFSignatureListControl
- {
- public class SignatureData
- {
- public SignatureStatus Status { get; set; }
- public string Signer { get; set; }
- }
-
- public partial class CPDFSignatureListControl : UserControl
- {
- private PDFViewControl pdfViewer;
- private List<SignatureData> signatureDataList;
- private List<CPDFSignature> signatureList;
- private ContextMenu popContextMenu;
-
- public event EventHandler<CPDFSignature> ViewCertificateEvent;
- public event EventHandler<CPDFSignature> ViewSignatureEvent;
- public event EventHandler DeleteSignatureEvent;
- public CPDFSignatureListControl()
- {
- InitializeComponent();
- DataContext = this;
- popContextMenu = new ContextMenu();
- MenuItem viewSignatureDetailsMenu = new MenuItem();
- viewSignatureDetailsMenu.Header = "Signature Details";
- viewSignatureDetailsMenu.Click += ViewSignatureDetailsMenu_Click;
- popContextMenu.Items.Add(viewSignatureDetailsMenu);
-
- MenuItem viewCertificateDetailsMenu = new MenuItem();
- viewCertificateDetailsMenu.Header = "Certificate Details";
- viewCertificateDetailsMenu.Click += ViewCertificateDetailsMenu_Click;
- popContextMenu.Items.Add(viewCertificateDetailsMenu);
-
- MenuItem deleteMenu = new MenuItem();
- deleteMenu.Header = "Delete";
- deleteMenu.Click += DeleteMenu_Click;
- popContextMenu.Items.Add(deleteMenu);
- }
-
- private void ViewSignatureDetailsMenu_Click(object sender, RoutedEventArgs e)
- {
- int index = SignatureList.SelectedIndex;
- if (index >= 0 && index < signatureList.Count)
- {
- ViewSignatureEvent?.Invoke(this, signatureList[index]);
- }
- }
-
- private void DeleteMenu_Click(object sender, RoutedEventArgs e)
- {
- // custom button content
- string messageBoxText = "Are you sure to delete it?";
- string caption = "Delete Signature";
- MessageBoxButton button = MessageBoxButton.OKCancel;
- MessageBoxImage icon = MessageBoxImage.Warning;
- MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, icon, MessageBoxResult.Cancel);
- if(result != MessageBoxResult.OK)
- {
- return;
- }
- int index = SignatureList.SelectedIndex;
- if (index >= 0 && index < signatureList.Count)
- {
- var widget = signatureList[index].GetSignatureWidget(pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument());
- pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument().RemoveSignature(signatureList[index], true);
- widget.ResetForm();
- widget.SetIsLocked(false);
- pdfViewer.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
- DeleteSignatureEvent?.Invoke(this, null);
- }
- }
-
- private void ViewCertificateDetailsMenu_Click(object sender, RoutedEventArgs e)
- {
- int index = SignatureList.SelectedIndex;
- if (index >= 0 && index < signatureList.Count)
- {
- ViewCertificateEvent?.Invoke(this, signatureList[index]);
- }
- }
- public void InitWithPDFViewer(PDFViewControl newPDFView)
- {
- pdfViewer = newPDFView;
- }
-
- public void LoadSignatureList()
- {
- if (pdfViewer != null && pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument() != null)
- {
- signatureList = SignatureHelper.SignatureList;
- if (signatureList != null)
- {
- SignatureList.ItemsSource = null;
- signatureDataList = new List<SignatureData>();
- foreach (CPDFSignature signature in signatureList)
- {
- var item = new SignatureData();
- CPDFSigner signer = signature.SignerList.First();
- bool isSignVerified = signer.IsSignVerified;
- bool isCertTrusted = signer.IsCertTrusted;
- bool notModified = signature.ModifyInfoList.Count == 0;
- if (isSignVerified && isCertTrusted && notModified)
- {
- item.Status = SignatureStatus.Valid;
- }
- else if (isSignVerified && !isCertTrusted && notModified)
- {
- item.Status = SignatureStatus.Unknown;
- }
- else
- {
- item.Status = SignatureStatus.Invalid;
- }
-
- item.Signer = signature.Name + "'s signature";
- signatureDataList.Add(item);
- }
- if (signatureList.Count > 0)
- {
- SignatureList.ContextMenu = popContextMenu;
- }
- SignatureList.ItemsSource = signatureDataList;
- SignatureList.Visibility = Visibility.Visible;
- }
- else
- {
- SignatureList.Visibility = Visibility.Collapsed;
- }
- }
- }
- private void SignatureList_ContextMenuOpening(object sender, ContextMenuEventArgs e)
- {
- try
- {
- if (SignatureList != null && SignatureList.SelectedIndex == -1)
- {
- foreach (var item in popContextMenu.Items)
- {
- (item as MenuItem).IsEnabled = false;
- }
- }
- else
- {
- foreach (var item in popContextMenu.Items)
- {
- (item as MenuItem).IsEnabled = true;
- }
- }
- }
- catch (Exception ex)
- {
- }
- }
- private void SignatureList_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
- {
- SignatureList.SelectedIndex = -1;
- }
-
- private void SignatureList_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- int index = SignatureList.SelectedIndex;
- if (index >= 0 && index < signatureList.Count)
- {
- CRect rect = signatureList[index].GetPageBound(pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument());
- Point point = new Point(rect.left, rect.top);
- pdfViewer.PDFViewTool.GetCPDFViewer().GoToPage(signatureList[index].GetPageIndex(pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument()),point);
- }
- }
- }
-
- public class SignatureStatusToPathConverter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if (value is SignatureStatus signatureStatus && parameter is CPDFSignatureListControl cpdfSignatureListControl)
- {
- switch (signatureStatus)
- {
- case SignatureStatus.Valid:
- return cpdfSignatureListControl.FindResource("ValidSignaturePath");
- case SignatureStatus.Invalid:
- return cpdfSignatureListControl.FindResource("InvalidSignaturePath");
- case SignatureStatus.Unknown:
- return cpdfSignatureListControl.FindResource("UnknownSignaturePath");
- default:
- return null;
- }
- }
- return null;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- return null;
- }
- }
- }
|