DigitalSignatureControl.xaml.cs 12 KB

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