CPDFDigitalSignatureBarControl.xaml.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Runtime.CompilerServices;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Controls.Primitives;
  8. using System.Windows.Media;
  9. using System.Windows.Shapes;
  10. using ComPDFKit.Controls.Helper;
  11. namespace ComPDFKit.Controls.Common
  12. {
  13. /// <summary>
  14. /// Interaction logic for CPDFDigitalSignatureBarControl.xaml
  15. /// </summary>
  16. public partial class CPDFDigitalSignatureBarControl : UserControl
  17. {
  18. private string addSig = LanguageHelper.ToolBarManager.GetString("Button_NewSig");
  19. private string verifySig = LanguageHelper.ToolBarManager.GetString("Button_VerifySig");
  20. private bool isFirstLoad = true;
  21. private int counter = 0;
  22. public event EventHandler<DigitalSignatureAction> DigitalSignatureActionChanged;
  23. public enum DigitalSignatureAction
  24. {
  25. AddSignatureField,
  26. Signing,
  27. VerifySignature
  28. }
  29. #region data
  30. Dictionary<string, string> ButtonDict;
  31. public event PropertyChangedEventHandler PropertyChanged;
  32. protected void OnPropertyChanged([CallerMemberName] string name = null)
  33. {
  34. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  35. }
  36. #endregion
  37. #region Create Default UI
  38. private void ClearToolState(UIElement sender)
  39. {
  40. foreach (UIElement child in DigitalSignBarGrid.Children)
  41. {
  42. if (child is ToggleButton toggle && ((child as ToggleButton) != sender))
  43. {
  44. toggle.IsChecked = false;
  45. }
  46. }
  47. }
  48. public void ClearAllToolState()
  49. {
  50. foreach (UIElement child in DigitalSignBarGrid.Children)
  51. {
  52. if (child is ToggleButton toggle)
  53. {
  54. toggle.IsChecked = false;
  55. }
  56. }
  57. }
  58. public CPDFDigitalSignatureBarControl()
  59. {
  60. ButtonDict = new Dictionary<string, string>
  61. {
  62. {addSig,"M12.0663 0.731427C11.8457 0.510843 11.4792 0.543891 11.3017 0.800376L8.50221 4.84406C8.47541 4.88277 8.43581 4.91078 8.39039 4.92317L4.70212 5.92906C4.62329 5.95056 4.56548 6.01698 4.55485 6.09799C4.49741 6.53554 4.28835 7.98652 3.86452 9.50018C3.50509 10.7839 2.41361 13.2636 1.80714 14.6016C1.43791 14.4321 0.986655 14.4994 0.682542 14.8035C0.292017 15.194 0.292017 15.8272 0.682542 16.2177C1.07307 16.6082 1.70623 16.6082 2.09676 16.2177C2.40731 15.9072 2.4709 15.4432 2.28754 15.0698C3.47427 14.5032 5.62247 13.5334 7.40006 13.0357C9.40855 12.4733 10.4209 12.3578 10.7798 12.3344C10.8716 12.3284 10.9526 12.2662 10.9768 12.1775L11.9771 8.50985C11.9895 8.46442 12.0175 8.42483 12.0562 8.39803L16.0999 5.59856C16.3563 5.42099 16.3894 5.05449 16.1688 4.83391L12.0663 0.731427ZM7.84417 7.82298C7.79817 7.77698 7.72119 7.78646 7.68771 7.84224L5.83809 10.925C5.78453 11.0142 5.88602 11.1157 5.97529 11.0622L9.058 9.21253C9.11378 9.17905 9.12326 9.10207 9.07726 9.05607L7.84417 7.82298ZM15.527 15.5935C15.4522 15.5983 15.3766 15.6007 15.3006 15.6007C15.2254 15.6007 15.1507 15.5983 15.0767 15.5937L13.5309 18.2711C13.4525 18.4069 13.2553 18.4037 13.1813 18.2653L12.5771 17.1345C12.5412 17.0673 12.4702 17.0263 12.3941 17.0288L11.1126 17.0709C10.9558 17.0761 10.8544 16.9069 10.9329 16.7711L12.4536 14.1371C12.0425 13.5634 11.8006 12.8603 11.8006 12.1007C11.8006 10.1677 13.3676 8.60071 15.3006 8.60071C17.2336 8.60071 18.8006 10.1677 18.8006 12.1007C18.8006 12.8594 18.5592 13.5616 18.149 14.1349L19.671 16.7711C19.7494 16.9069 19.648 17.0761 19.4912 17.0709L18.2098 17.0288C18.1336 17.0263 18.0627 17.0673 18.0268 17.1345L17.4225 18.2653C17.3486 18.4037 17.1514 18.4069 17.0729 18.2711L15.527 15.5935Z"},
  63. {verifySig, "M12.0663 0.846661C11.8457 0.626078 11.4792 0.659126 11.3017 0.91561L8.50221 4.95929C8.47541 4.998 8.43581 5.02601 8.39039 5.0384L4.70212 6.0443C4.62329 6.06579 4.56548 6.13221 4.55485 6.21322C4.49741 6.65078 4.28835 8.10176 3.86452 9.61542C3.50509 10.8991 2.41361 13.3788 1.80714 14.7169C1.43791 14.5473 0.986655 14.6146 0.682542 14.9187C0.292017 15.3093 0.292017 15.9424 0.682542 16.333C1.07307 16.7235 1.70623 16.7235 2.09676 16.333C2.40731 16.0224 2.4709 15.5584 2.28754 15.185C3.47427 14.6185 5.62247 13.6487 7.40006 13.151C9.40855 12.5886 10.4209 12.473 10.7798 12.4496C10.8716 12.4436 10.9526 12.3815 10.9768 12.2927L11.9771 8.62508C11.9895 8.57966 12.0175 8.54006 12.0562 8.51327L16.0999 5.71379C16.3563 5.53623 16.3894 5.16973 16.1688 4.94914L12.0663 0.846661ZM7.84417 7.93821C7.79817 7.89221 7.72119 7.90169 7.68771 7.95748L5.83809 11.0402C5.78453 11.1295 5.88602 11.2309 5.97529 11.1774L9.058 9.32776C9.11378 9.29429 9.12326 9.2173 9.07726 9.1713L7.84417 7.93821ZM11.8814 12.374C11.8814 12.1507 12.0293 11.9545 12.244 11.8932L15.3033 11.0191C15.3931 10.9935 15.4883 10.9935 15.578 11.0191L18.6374 11.8932C18.852 11.9545 19 12.1507 19 12.374V15.9132C19 16.0119 18.9713 16.1077 18.9146 16.1886C18.5137 16.7603 16.7809 19.1154 15.4407 19.1154C14.1004 19.1154 12.3677 16.7603 11.9667 16.1886C11.91 16.1077 11.8814 16.0119 11.8814 15.9132V12.374ZM14.4114 15.9256L14.4114 15.9256L15.1305 16.6447L17.4549 14.3203L16.7358 13.6012L15.1305 15.2065L14.1993 14.2754L13.4802 14.9945L14.4114 15.9256Z"},
  64. };
  65. InitializeComponent();
  66. }
  67. /// <summary>
  68. /// When the "Add Signatrue Field" button is pressed,
  69. /// it triggers an event: entering the state of creating a signature field.
  70. /// Clicking again will return to the state where signing is possible.
  71. /// </summary>
  72. /// <param name="sender"></param>
  73. /// <param name="e"></param>
  74. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  75. {
  76. if (isFirstLoad)
  77. {
  78. foreach (KeyValuePair<string, string> data in ButtonDict)
  79. {
  80. string Path = data.Value;
  81. string name = data.Key;
  82. Geometry annotationGeometry = Geometry.Parse(Path);
  83. Path path = new Path
  84. {
  85. Width = 20,
  86. Height = 20,
  87. Data = annotationGeometry,
  88. Fill = new SolidColorBrush(Color.FromRgb(0x43, 0x47, 0x4D))
  89. };
  90. CreateButtonForPath(path, name);
  91. }
  92. isFirstLoad = false;
  93. }
  94. }
  95. private void CreateButtonForPath(Path path, String name)
  96. {
  97. StackPanel stackPanel = new StackPanel();
  98. TextBlock textBlock = new TextBlock();
  99. if (path != null)
  100. {
  101. stackPanel.Orientation = Orientation.Horizontal;
  102. stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
  103. stackPanel.Children.Add(path);
  104. }
  105. if (!string.IsNullOrEmpty(name))
  106. {
  107. textBlock.Text = name;
  108. textBlock.VerticalAlignment = VerticalAlignment.Center;
  109. textBlock.Margin = new Thickness(8, 0, 0, 0);
  110. textBlock.FontSize = 12;
  111. stackPanel.Children.Add(textBlock);
  112. }
  113. Style style = (Style)FindResource("RoundMarginToggleButtonStyle");
  114. ToggleButton button = new ToggleButton();
  115. button.BorderThickness = new Thickness(0);
  116. button.Padding = new Thickness(10, 5, 10, 5);
  117. button.Tag = name;
  118. button.ToolTip = name;
  119. button.Style = style;
  120. button.Content = stackPanel;
  121. if (name == addSig)
  122. {
  123. button.Click -= AddDigitalSignatureBtn_Click;
  124. button.Click += AddDigitalSignatureBtn_Click;
  125. }
  126. else if (name == verifySig)
  127. {
  128. button.Click -= VerifySignatureBtn_Click;
  129. button.Click += VerifySignatureBtn_Click;
  130. }
  131. DigitalSignBarGrid.ColumnDefinitions.Add(new ColumnDefinition());
  132. DigitalSignBarGrid.Width += 180;
  133. Grid.SetColumn(button, counter++);
  134. DigitalSignBarGrid.Children.Add(button);
  135. }
  136. private void AddDigitalSignatureBtn_Click(object sender, RoutedEventArgs e)
  137. {
  138. ClearToolState(sender as ToggleButton);
  139. if ((bool)(sender as ToggleButton).IsChecked)
  140. {
  141. DigitalSignatureActionChanged?.Invoke(sender, DigitalSignatureAction.AddSignatureField);
  142. }
  143. else
  144. {
  145. DigitalSignatureActionChanged?.Invoke(sender, DigitalSignatureAction.Signing);
  146. }
  147. }
  148. private void VerifySignatureBtn_Click(object sender, RoutedEventArgs e)
  149. {
  150. if ((bool)(sender as ToggleButton).IsChecked)
  151. {
  152. DigitalSignatureActionChanged?.Invoke(sender, DigitalSignatureAction.VerifySignature);
  153. }
  154. }
  155. #endregion
  156. }
  157. }