DigitalSignatureControl.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using Compdfkit_Tools.PDFControl;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using System;
  5. using System.ComponentModel;
  6. using System.Runtime.CompilerServices;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Controls.Primitives;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using Compdfkit_Tools.Helper;
  13. using ComPDFKit.PDFAnnotation.Form;
  14. namespace Compdfkit_Tools.PDFControl
  15. {
  16. public partial class DigitalSignatureControl : UserControl, INotifyPropertyChanged
  17. {
  18. private bool isFirstLoad = true;
  19. public PDFViewControl PDFViewControl = new PDFViewControl();
  20. private SignatureStatusBarControl signatureStatusBarControl;
  21. private PanelState panelState = PanelState.GetInstance();
  22. private CPDFDisplaySettingsControl displaySettingsControl = null;
  23. private bool _isActive = false;
  24. public event EventHandler<bool> OnCanSaveChanged;
  25. private CPDFSignatureWidget currentSignatureWidget;
  26. public event PropertyChangedEventHandler PropertyChanged;
  27. protected void OnPropertyChanged([CallerMemberName] string name = null)
  28. {
  29. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  30. }
  31. public bool IsActive
  32. {
  33. get => _isActive;
  34. set
  35. {
  36. _isActive = value;
  37. OnPropertyChanged();
  38. }
  39. }
  40. public bool CanUndo
  41. {
  42. get
  43. {
  44. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  45. {
  46. return PDFViewControl.PDFView.UndoManager.CanUndo;
  47. }
  48. return false;
  49. }
  50. }
  51. public bool CanRedo
  52. {
  53. get
  54. {
  55. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  56. {
  57. return PDFViewControl.PDFView.UndoManager.CanRedo;
  58. }
  59. return false;
  60. }
  61. }
  62. private bool CanSave
  63. {
  64. get
  65. {
  66. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  67. {
  68. return PDFViewControl.PDFView.UndoManager.CanSave;
  69. }
  70. return false;
  71. }
  72. }
  73. public DigitalSignatureControl()
  74. {
  75. InitializeComponent();
  76. DataContext = this;
  77. }
  78. public void ClearViewerControl()
  79. {
  80. PDFGrid.Child = null;
  81. BotaContainer.Child = null;
  82. PropertyContainer.Child = null;
  83. SignatureStatusBorder.Child = null;
  84. }
  85. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  86. {
  87. PDFViewControl.PDFView = pdfViewer;
  88. PDFGrid.Child = PDFViewControl;
  89. FloatPageTool.InitWithPDFViewer(pdfViewer);
  90. InitialControl();
  91. DigitalSignatureBarControl.DigitalSignatureActionChanged -= DigitalSignatureBarControl_DigitalSignatureActionChanged;
  92. DigitalSignatureBarControl.DigitalSignatureActionChanged += DigitalSignatureBarControl_DigitalSignatureActionChanged;
  93. PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  94. PDFViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  95. panelState.PropertyChanged -= PanelState_PropertyChanged;
  96. panelState.PropertyChanged += PanelState_PropertyChanged;
  97. }
  98. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  99. {
  100. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  101. AddCertificationDialog addCertificationControl = new AddCertificationDialog
  102. {
  103. Owner = parentWindow
  104. };
  105. currentSignatureWidget = (e as WidgetSignArgs).Sign;
  106. addCertificationControl.FillSignatureEvent -= AddCertificationControl_FillSignatureEvent;
  107. addCertificationControl.FillSignatureEvent += AddCertificationControl_FillSignatureEvent;
  108. addCertificationControl.ShowDialog();
  109. }
  110. private void AddCertificationControl_FillSignatureEvent(object sender, CertificateAccess e)
  111. {
  112. FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog
  113. {
  114. FilePath = e.filePath,
  115. Password = e.password,
  116. SignatureWidget = currentSignatureWidget,
  117. Document = PDFViewControl.PDFView.Document
  118. };
  119. fillDigitalSignatureDialog.ShowDialog();
  120. }
  121. private void DigitalSignatureBarControl_DigitalSignatureActionChanged(object sender, Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction e)
  122. {
  123. if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.AddSignatrueField)
  124. {
  125. CreateSign();
  126. }
  127. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.Signing)
  128. {
  129. PDFViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  130. }
  131. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.VerifySignature)
  132. {
  133. ToggleButton button = sender as ToggleButton;
  134. button.IsChecked = false;
  135. }
  136. }
  137. private string GetTime()
  138. {
  139. DateTime dateTime = DateTime.Now;
  140. return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
  141. }
  142. private void CreateSign()
  143. {
  144. PDFViewControl.PDFView.SetMouseMode(MouseModes.FormEditTool);
  145. WidgetSignArgs signArgs = new WidgetSignArgs
  146. {
  147. LineWidth = 1,
  148. LineColor = Colors.Black,
  149. FieldName = "Signature" + GetTime()
  150. };
  151. PDFViewControl.PDFView.SetToolParam(signArgs);
  152. }
  153. private void InitialControl()
  154. {
  155. PDFViewControl.PDFView?.SetMouseMode(MouseModes.Viewer);
  156. //PDFViewControl.PDFView?.Load();
  157. PDFViewControl.PDFView?.SetShowLink(true);
  158. PDFGrid.Child = PDFViewControl;
  159. PDFViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  160. PDFViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  161. PDFViewControl.PDFView.SetFormFieldHighlight(true);
  162. }
  163. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  164. {
  165. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  166. {
  167. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  168. }
  169. else if (e.PropertyName == nameof(PanelState.RightPanel))
  170. {
  171. if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  172. {
  173. ExpandRightPropertyPanel((IsActive)?displaySettingsControl:null, Visibility.Visible);
  174. }
  175. else
  176. {
  177. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  178. }
  179. }
  180. }
  181. public void ExpandLeftPanel(bool isExpand)
  182. {
  183. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  184. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  185. if (isExpand)
  186. {
  187. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  188. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  189. }
  190. else
  191. {
  192. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  193. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  194. }
  195. }
  196. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  197. {
  198. PropertyContainer.Width = 260;
  199. PropertyContainer.Child = propertytPanel;
  200. PropertyContainer.Visibility = visible;
  201. }
  202. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  203. {
  204. this.BotaContainer.Child = botaControl;
  205. }
  206. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  207. {
  208. this.displaySettingsControl = displaySettingsControl;
  209. }
  210. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  211. {
  212. this.signatureStatusBarControl = signatureStatusBarControl;
  213. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  214. if (signatureStatusBarControl.Status != SignatureStatus.None)
  215. {
  216. SignatureStatusBorder.Visibility = Visibility.Visible;
  217. }
  218. else
  219. {
  220. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  221. }
  222. }
  223. /// <summary>
  224. /// Undo Redo Event Noitfy
  225. /// </summary>
  226. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  227. {
  228. OnPropertyChanged(e.PropertyName);
  229. if (e.PropertyName == "CanSave")
  230. {
  231. OnCanSaveChanged?.Invoke(this, CanSave);
  232. }
  233. }
  234. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  235. {
  236. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  237. {
  238. PDFViewControl.PDFView.UndoManager?.Undo();
  239. }
  240. }
  241. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  242. {
  243. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  244. {
  245. PDFViewControl.PDFView.UndoManager?.Redo();
  246. }
  247. }
  248. private void UndoButton_Click(object sender, RoutedEventArgs e)
  249. {
  250. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  251. {
  252. PDFViewControl.PDFView.UndoManager?.Undo();
  253. }
  254. }
  255. private void RedoButton_Click(object sender, RoutedEventArgs e)
  256. {
  257. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  258. {
  259. PDFViewControl.PDFView.UndoManager?.Redo();
  260. }
  261. }
  262. }
  263. }