DigitalSignatureControl.xaml.cs 13 KB

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