DigitalSignatureControl.xaml.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using Compdfkit_Tools.DigitalSignature.AddCertificationControl;
  2. using Compdfkit_Tools.PDFControl;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer.PdfViewer;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. using Compdfkit_Tools.DigitalSignature.SignatureStatusBarControl;
  22. namespace Compdfkit_Tools.PDFControl
  23. {
  24. public partial class DigitalSignatureControl : UserControl, INotifyPropertyChanged
  25. {
  26. private bool isFirstLoad = true;
  27. public PDFViewControl PDFViewControl = new PDFViewControl();
  28. public SignatureStatusBarControl SignatureStatusBarControl = new SignatureStatusBarControl();
  29. private bool _isActive = false;
  30. public event EventHandler<bool> OnCanSaveChanged;
  31. public event PropertyChangedEventHandler PropertyChanged;
  32. protected void OnPropertyChanged([CallerMemberName] string name = null)
  33. {
  34. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  35. }
  36. public bool IsActive
  37. {
  38. get => _isActive;
  39. set
  40. {
  41. _isActive = value;
  42. OnPropertyChanged();
  43. }
  44. }
  45. public bool CanUndo
  46. {
  47. get
  48. {
  49. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  50. {
  51. return PDFViewControl.PDFView.UndoManager.CanUndo;
  52. }
  53. return false;
  54. }
  55. }
  56. public bool CanRedo
  57. {
  58. get
  59. {
  60. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  61. {
  62. return PDFViewControl.PDFView.UndoManager.CanRedo;
  63. }
  64. return false;
  65. }
  66. }
  67. private bool CanSave
  68. {
  69. get
  70. {
  71. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  72. {
  73. return PDFViewControl.PDFView.UndoManager.CanSave;
  74. }
  75. return false;
  76. }
  77. }
  78. public DigitalSignatureControl()
  79. {
  80. InitializeComponent();
  81. DataContext = this;
  82. }
  83. public void ClearViewerControl()
  84. {
  85. PDFGrid.Child = null;
  86. BotaContainer.Child = null;
  87. PropertyContainer.Child = null;
  88. }
  89. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  90. {
  91. PDFViewControl.PDFView = pdfViewer;
  92. PDFGrid.Child = PDFViewControl;
  93. FloatPageTool.InitWithPDFViewer(pdfViewer);
  94. InitialControl();
  95. DigitalSignatureBarControl.DigitalSignatureActionChanged -= DigitalSignatureBarControl_DigitalSignatureActionChanged;
  96. DigitalSignatureBarControl.DigitalSignatureActionChanged += DigitalSignatureBarControl_DigitalSignatureActionChanged;
  97. PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  98. PDFViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  99. }
  100. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  101. {
  102. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  103. AddCertificationDialog addCertificationControl = new AddCertificationDialog();
  104. addCertificationControl.Owner = parentWindow;
  105. addCertificationControl.ShowDialog();
  106. }
  107. private void DigitalSignatureBarControl_DigitalSignatureActionChanged(object sender, Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction e)
  108. {
  109. if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.AddSignatrueField)
  110. {
  111. CreateSign();
  112. }
  113. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.Signing)
  114. {
  115. PDFViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  116. }
  117. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.VerifySignature)
  118. {
  119. }
  120. }
  121. private string GetTime()
  122. {
  123. DateTime dateTime = DateTime.Now;
  124. return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
  125. }
  126. private void CreateSign()
  127. {
  128. PDFViewControl.PDFView.SetMouseMode(MouseModes.FormEditTool);
  129. WidgetSignArgs signArgs = new WidgetSignArgs
  130. {
  131. LineWidth = 1,
  132. LineColor = Colors.Black,
  133. FieldName = "Signature" + GetTime()
  134. };
  135. PDFViewControl.PDFView.SetToolParam(signArgs);
  136. }
  137. private void InitialControl()
  138. {
  139. PDFViewControl.PDFView?.SetMouseMode(MouseModes.Viewer);
  140. //PDFViewControl.PDFView?.Load();
  141. PDFViewControl.PDFView?.SetShowLink(true);
  142. PDFGrid.Child = PDFViewControl;
  143. PDFViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  144. PDFViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  145. PDFViewControl.PDFView.SetFormFieldHighlight(true);
  146. SignatureBorder.Child = SignatureStatusBarControl;
  147. SignatureStatusBarControl.Status = SignatureStatusBarControl.SignatureStatus.Invalid;
  148. }
  149. /// <summary>
  150. /// Undo Redo Event Noitfy
  151. /// </summary>
  152. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  153. {
  154. OnPropertyChanged(e.PropertyName);
  155. if (e.PropertyName == "CanSave")
  156. {
  157. OnCanSaveChanged?.Invoke(this, CanSave);
  158. }
  159. }
  160. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  161. {
  162. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  163. {
  164. PDFViewControl.PDFView.UndoManager?.Undo();
  165. }
  166. }
  167. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  168. {
  169. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  170. {
  171. PDFViewControl.PDFView.UndoManager?.Redo();
  172. }
  173. }
  174. private void UndoButton_Click(object sender, RoutedEventArgs e)
  175. {
  176. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  177. {
  178. PDFViewControl.PDFView.UndoManager?.Undo();
  179. }
  180. }
  181. private void RedoButton_Click(object sender, RoutedEventArgs e)
  182. {
  183. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  184. {
  185. PDFViewControl.PDFView.UndoManager?.Redo();
  186. }
  187. }
  188. }
  189. }