DigitalSignatureControl.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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.DigitalSignature.VerifyDigitalSignatureControl;
  13. using Compdfkit_Tools.Helper;
  14. using ComPDFKit.PDFAnnotation.Form;
  15. namespace Compdfkit_Tools.PDFControl
  16. {
  17. public partial class DigitalSignatureControl : UserControl, INotifyPropertyChanged
  18. {
  19. private bool isFirstLoad = true;
  20. public PDFViewControl PDFViewControl = new PDFViewControl();
  21. private SignatureStatusBarControl signatureStatusBarControl;
  22. private PanelState panelState = PanelState.GetInstance();
  23. private CPDFDisplaySettingsControl displaySettingsControl = null;
  24. private CPDFBOTABarControl botaControl;
  25. private bool _isActive = false;
  26. public event EventHandler<bool> OnCanSaveChanged;
  27. private CPDFSignatureWidget currentSignatureWidget;
  28. public event EventHandler<string> AfterFillSignature;
  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. SignatureStatusBorder.Child = null;
  87. }
  88. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  89. {
  90. PDFViewControl.PDFView = pdfViewer;
  91. PDFGrid.Child = PDFViewControl;
  92. FloatPageTool.InitWithPDFViewer(pdfViewer);
  93. InitialControl();
  94. DigitalSignatureBarControl.DigitalSignatureActionChanged -= DigitalSignatureBarControl_DigitalSignatureActionChanged;
  95. DigitalSignatureBarControl.DigitalSignatureActionChanged += DigitalSignatureBarControl_DigitalSignatureActionChanged;
  96. PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  97. PDFViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  98. panelState.PropertyChanged -= PanelState_PropertyChanged;
  99. panelState.PropertyChanged += PanelState_PropertyChanged;
  100. }
  101. private void BotaControlOnDeleteSignatureEvent(object sender, EventArgs e)
  102. {
  103. RefreshDigitalSignatureList();
  104. }
  105. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  106. {
  107. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  108. var signatureWidget = (e as WidgetSignArgs).Sign;
  109. if (signatureWidget.IsSigned())
  110. {
  111. VerifyDigitalSignatureControl verifyDigitalSignatureControl = new VerifyDigitalSignatureControl
  112. {
  113. Owner = parentWindow
  114. };
  115. verifyDigitalSignatureControl.InitWithSignature(signatureWidget.GetSignature());
  116. verifyDigitalSignatureControl.ShowDialog();
  117. }
  118. else
  119. {
  120. AddCertificationDialog addCertificationControl = new AddCertificationDialog
  121. {
  122. Owner = parentWindow
  123. };
  124. currentSignatureWidget = signatureWidget;
  125. addCertificationControl.FillSignatureEvent -= AddCertificationControl_FillSignatureEvent;
  126. addCertificationControl.FillSignatureEvent += AddCertificationControl_FillSignatureEvent;
  127. addCertificationControl.ShowDialog();
  128. }
  129. }
  130. private void AddCertificationControl_FillSignatureEvent(object sender, CertificateAccess e)
  131. {
  132. FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog
  133. {
  134. FilePath = e.filePath,
  135. Password = e.password,
  136. SignatureWidget = currentSignatureWidget,
  137. Document = PDFViewControl.PDFView.Document
  138. };
  139. fillDigitalSignatureDialog.AfterFillSignature += FillDigitalSignatureDialog_AfterFillSignature; ;
  140. fillDigitalSignatureDialog.ShowDialog();
  141. }
  142. private void FillDigitalSignatureDialog_AfterFillSignature(object sender, string e)
  143. {
  144. AfterFillSignature?.Invoke(this, e);
  145. }
  146. private void DigitalSignatureBarControl_DigitalSignatureActionChanged(object sender, Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction e)
  147. {
  148. if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.AddSignatureField)
  149. {
  150. CreateSign();
  151. }
  152. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.Signing)
  153. {
  154. PDFViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  155. }
  156. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.VerifySignature)
  157. {
  158. ToggleButton button = sender as ToggleButton;
  159. button.IsChecked = false;
  160. RefreshDigitalSignatureList();
  161. }
  162. }
  163. private void RefreshDigitalSignatureList()
  164. {
  165. signatureStatusBarControl.SetStatus(SignatureHelper.GetEffectiveSignatureList(PDFViewControl.PDFView.Document.GetSignatureList()));
  166. }
  167. private string GetTime()
  168. {
  169. DateTime dateTime = DateTime.Now;
  170. return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
  171. }
  172. private void CreateSign()
  173. {
  174. PDFViewControl.PDFView.SetMouseMode(MouseModes.FormEditTool);
  175. WidgetSignArgs signArgs = new WidgetSignArgs
  176. {
  177. LineWidth = 1,
  178. LineColor = Colors.Black,
  179. FieldName = "Signature" + GetTime()
  180. };
  181. PDFViewControl.PDFView.SetToolParam(signArgs);
  182. }
  183. private void InitialControl()
  184. {
  185. PDFViewControl.PDFView?.SetMouseMode(MouseModes.Viewer);
  186. //PDFViewControl.PDFView?.Load();
  187. PDFViewControl.PDFView?.SetShowLink(true);
  188. PDFGrid.Child = PDFViewControl;
  189. PDFViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  190. PDFViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  191. PDFViewControl.PDFView.SetFormFieldHighlight(true);
  192. }
  193. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  194. {
  195. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  196. {
  197. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  198. }
  199. else if (e.PropertyName == nameof(PanelState.RightPanel))
  200. {
  201. if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  202. {
  203. ExpandRightPropertyPanel((IsActive) ? displaySettingsControl : null, Visibility.Visible);
  204. }
  205. else
  206. {
  207. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  208. }
  209. }
  210. }
  211. public void ExpandLeftPanel(bool isExpand)
  212. {
  213. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  214. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  215. if (isExpand)
  216. {
  217. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  218. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  219. }
  220. else
  221. {
  222. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  223. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  224. }
  225. }
  226. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  227. {
  228. PropertyContainer.Width = 260;
  229. PropertyContainer.Child = propertytPanel;
  230. PropertyContainer.Visibility = visible;
  231. }
  232. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  233. {
  234. this.BotaContainer.Child = botaControl;
  235. this.botaControl = botaControl;
  236. this.botaControl.DeleteSignatureEvent -= BotaControlOnDeleteSignatureEvent;
  237. this.botaControl.DeleteSignatureEvent += BotaControlOnDeleteSignatureEvent;
  238. }
  239. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  240. {
  241. this.displaySettingsControl = displaySettingsControl;
  242. }
  243. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  244. {
  245. this.signatureStatusBarControl = signatureStatusBarControl;
  246. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  247. if (signatureStatusBarControl.Status != SignatureStatus.None)
  248. {
  249. SignatureStatusBorder.Visibility = Visibility.Visible;
  250. }
  251. else
  252. {
  253. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  254. }
  255. }
  256. /// <summary>
  257. /// Undo Redo Event Noitfy
  258. /// </summary>
  259. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  260. {
  261. OnPropertyChanged(e.PropertyName);
  262. if (e.PropertyName == "CanSave")
  263. {
  264. OnCanSaveChanged?.Invoke(this, CanSave);
  265. }
  266. }
  267. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  268. {
  269. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  270. {
  271. PDFViewControl.PDFView.UndoManager?.Undo();
  272. }
  273. }
  274. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  275. {
  276. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  277. {
  278. PDFViewControl.PDFView.UndoManager?.Redo();
  279. }
  280. }
  281. private void UndoButton_Click(object sender, RoutedEventArgs e)
  282. {
  283. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  284. {
  285. PDFViewControl.PDFView.UndoManager?.Undo();
  286. }
  287. }
  288. private void RedoButton_Click(object sender, RoutedEventArgs e)
  289. {
  290. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  291. {
  292. PDFViewControl.PDFView.UndoManager?.Redo();
  293. }
  294. }
  295. }
  296. }