DigitalSignatureControl.xaml.cs 6.8 KB

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