using ComPDFKit.DigitalSign; using ComPDFKit.PDFAnnotation; using ComPDFKit.PDFDocument; using Compdfkit_Tools.Helper; using Nager.Country; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Compdfkit_Tools.PDFControl { /// /// AddCustomCertificationControl.xaml 的交互逻辑 /// public partial class AddCustomCertificationControl : UserControl { public event EventHandler SaveEvent; public event EventHandler CancelEvent; public CertificateInfo certificateInfo = new CertificateInfo(); private string _grantorName = string.Empty; public string GrantorName { get => _grantorName; set { _grantorName = value; certificateInfo.GrantorName = value; } } private string _email = string.Empty; public string Email { get => _email; set { _email = value; certificateInfo.Email = value; } } private string _organization = string.Empty; public string Organization { get => _organization; set { _organization = value; certificateInfo.Organization = value; } } private string _organizationalUnit = string.Empty; public string OrganizationalUnit { get => _organizationalUnit; set { _organizationalUnit = value; certificateInfo.OrganizationUnit = value; } } private readonly CountryProvider countryProvider = new CountryProvider(); private readonly List countryNames = new List(); public event EventHandler ContinueEvent; public AddCustomCertificationControl() { InitializeComponent(); this.DataContext = this; FillComboBox(); } private void FillComboBox() { FillComboBoxWithCountries(); FillComboBoxWithAlgorithm(); FillComboBoxWithPropose(); } private void FillComboBoxWithPropose() { PurposeCmb.Items.Clear(); PurposeCmb.Items.Add("Digital Signatures"); PurposeCmb.Items.Add("Data Encryption"); PurposeCmb.Items.Add("Digital Signatures and Data Encryption"); } private void FillComboBoxWithAlgorithm() { AlgorithmCmb.Items.Clear(); AlgorithmCmb.Items.Add("1024-bit RSA"); AlgorithmCmb.Items.Add("2048-bit RSA"); } private void FillComboBoxWithCountries() { AlgorithmCmb.Items.Clear(); var countries = countryProvider.GetCountries(); foreach (var country in countries) { var formattedName = $"{country.Alpha2Code} - {country.CommonName}"; countryNames.Add(formattedName); } AreaCmb.ItemsSource = countryNames; } private void CancelBtn_Click(object sender, RoutedEventArgs e) { CancelEvent?.Invoke(this, EventArgs.Empty); } private void SaveBtn_Click(object sender, RoutedEventArgs e) { if (certificateInfo.GrantorName == string.Empty) { ErrorTipsText.Text = "Please input Grantor Name"; return; } if(certificateInfo.Email == string.Empty) { ErrorTipsText.Text = "Please input Email"; return; } if (!CommonHelper.IsValidEmail(certificateInfo.Email)) { ErrorTipsText.Text = "Email format is not correct"; return; } ContinueEvent?.Invoke(this, CreateCertificationMode.SaveCertificate); } private void AreaCmb_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (AreaCmb.SelectedItem != null) { string selectedText = AreaCmb.SelectedItem.ToString(); string[] parts = selectedText.Split('-'); certificateInfo.Area = parts[0].Trim(); // Extract the Alpha2Code } } private void AlgorithmCmb_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (AlgorithmCmb.SelectedItem != null) { certificateInfo.AlgorithmType = (AlgorithmType)AlgorithmCmb.SelectedIndex; } } private void PurposeCmb_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (PurposeCmb.SelectedItem != null) { certificateInfo.PurposeType = (CPDFCertUsage)PurposeCmb.SelectedIndex; } } } }