DigitalSignatureControl.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 PropertyChangedEventHandler PropertyChanged;
  29. protected void OnPropertyChanged([CallerMemberName] string name = null)
  30. {
  31. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  32. }
  33. public bool IsActive
  34. {
  35. get => _isActive;
  36. set
  37. {
  38. _isActive = value;
  39. OnPropertyChanged();
  40. }
  41. }
  42. public bool CanUndo
  43. {
  44. get
  45. {
  46. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  47. {
  48. return PDFViewControl.PDFView.UndoManager.CanUndo;
  49. }
  50. return false;
  51. }
  52. }
  53. public bool CanRedo
  54. {
  55. get
  56. {
  57. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  58. {
  59. return PDFViewControl.PDFView.UndoManager.CanRedo;
  60. }
  61. return false;
  62. }
  63. }
  64. private bool CanSave
  65. {
  66. get
  67. {
  68. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  69. {
  70. return PDFViewControl.PDFView.UndoManager.CanSave;
  71. }
  72. return false;
  73. }
  74. }
  75. public DigitalSignatureControl()
  76. {
  77. InitializeComponent();
  78. DataContext = this;
  79. }
  80. public void ClearViewerControl()
  81. {
  82. PDFGrid.Child = null;
  83. BotaContainer.Child = null;
  84. PropertyContainer.Child = null;
  85. SignatureStatusBorder.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. panelState.PropertyChanged -= PanelState_PropertyChanged;
  98. panelState.PropertyChanged += PanelState_PropertyChanged;
  99. }
  100. private void BotaControlOnDeleteSignatureEvent(object sender, EventArgs e)
  101. {
  102. RefreshDigitalSignatureList();
  103. }
  104. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  105. {
  106. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  107. var signatureWidget = (e as WidgetSignArgs).Sign;
  108. if (signatureWidget.IsSigned())
  109. {
  110. VerifyDigitalSignatureControl verifyDigitalSignatureControl = new VerifyDigitalSignatureControl
  111. {
  112. Owner = parentWindow
  113. };
  114. verifyDigitalSignatureControl.InitWithSignature(signatureWidget.GetSignature());
  115. verifyDigitalSignatureControl.ShowDialog();
  116. }
  117. else
  118. {
  119. AddCertificationDialog addCertificationControl = new AddCertificationDialog
  120. {
  121. Owner = parentWindow
  122. };
  123. currentSignatureWidget = signatureWidget;
  124. addCertificationControl.FillSignatureEvent -= AddCertificationControl_FillSignatureEvent;
  125. addCertificationControl.FillSignatureEvent += AddCertificationControl_FillSignatureEvent;
  126. addCertificationControl.ShowDialog();
  127. }
  128. }
  129. private void AddCertificationControl_FillSignatureEvent(object sender, CertificateAccess e)
  130. {
  131. FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog
  132. {
  133. FilePath = e.filePath,
  134. Password = e.password,
  135. SignatureWidget = currentSignatureWidget,
  136. Document = PDFViewControl.PDFView.Document
  137. };
  138. fillDigitalSignatureDialog.ShowDialog();
  139. }
  140. private void DigitalSignatureBarControl_DigitalSignatureActionChanged(object sender, Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction e)
  141. {
  142. if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.AddSignatureField)
  143. {
  144. CreateSign();
  145. }
  146. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.Signing)
  147. {
  148. PDFViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  149. }
  150. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.VerifySignature)
  151. {
  152. ToggleButton button = sender as ToggleButton;
  153. button.IsChecked = false;
  154. RefreshDigitalSignatureList();
  155. }
  156. }
  157. private void RefreshDigitalSignatureList()
  158. {
  159. signatureStatusBarControl.SetStatus(PDFViewControl.PDFView.Document.GetSignatureList());
  160. }
  161. private string GetTime()
  162. {
  163. DateTime dateTime = DateTime.Now;
  164. return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
  165. }
  166. private void CreateSign()
  167. {
  168. PDFViewControl.PDFView.SetMouseMode(MouseModes.FormEditTool);
  169. WidgetSignArgs signArgs = new WidgetSignArgs
  170. {
  171. LineWidth = 1,
  172. LineColor = Colors.Black,
  173. FieldName = "Signature" + GetTime()
  174. };
  175. PDFViewControl.PDFView.SetToolParam(signArgs);
  176. }
  177. private void InitialControl()
  178. {
  179. PDFViewControl.PDFView?.SetMouseMode(MouseModes.Viewer);
  180. //PDFViewControl.PDFView?.Load();
  181. PDFViewControl.PDFView?.SetShowLink(true);
  182. PDFGrid.Child = PDFViewControl;
  183. PDFViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  184. PDFViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  185. PDFViewControl.PDFView.SetFormFieldHighlight(true);
  186. }
  187. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  188. {
  189. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  190. {
  191. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  192. }
  193. else if (e.PropertyName == nameof(PanelState.RightPanel))
  194. {
  195. if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  196. {
  197. ExpandRightPropertyPanel((IsActive)?displaySettingsControl:null, Visibility.Visible);
  198. }
  199. else
  200. {
  201. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  202. }
  203. }
  204. }
  205. public void ExpandLeftPanel(bool isExpand)
  206. {
  207. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  208. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  209. if (isExpand)
  210. {
  211. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  212. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  213. }
  214. else
  215. {
  216. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  217. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  218. }
  219. }
  220. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  221. {
  222. PropertyContainer.Width = 260;
  223. PropertyContainer.Child = propertytPanel;
  224. PropertyContainer.Visibility = visible;
  225. }
  226. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  227. {
  228. this.BotaContainer.Child = botaControl;
  229. this.botaControl = botaControl;
  230. this.botaControl.DeleteSignatureEvent -= BotaControlOnDeleteSignatureEvent;
  231. this.botaControl.DeleteSignatureEvent += BotaControlOnDeleteSignatureEvent;
  232. }
  233. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  234. {
  235. this.displaySettingsControl = displaySettingsControl;
  236. }
  237. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  238. {
  239. this.signatureStatusBarControl = signatureStatusBarControl;
  240. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  241. if (signatureStatusBarControl.Status != SignatureStatus.None)
  242. {
  243. SignatureStatusBorder.Visibility = Visibility.Visible;
  244. }
  245. else
  246. {
  247. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  248. }
  249. }
  250. /// <summary>
  251. /// Undo Redo Event Noitfy
  252. /// </summary>
  253. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  254. {
  255. OnPropertyChanged(e.PropertyName);
  256. if (e.PropertyName == "CanSave")
  257. {
  258. OnCanSaveChanged?.Invoke(this, CanSave);
  259. }
  260. }
  261. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  262. {
  263. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  264. {
  265. PDFViewControl.PDFView.UndoManager?.Undo();
  266. }
  267. }
  268. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  269. {
  270. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  271. {
  272. PDFViewControl.PDFView.UndoManager?.Redo();
  273. }
  274. }
  275. private void UndoButton_Click(object sender, RoutedEventArgs e)
  276. {
  277. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  278. {
  279. PDFViewControl.PDFView.UndoManager?.Undo();
  280. }
  281. }
  282. private void RedoButton_Click(object sender, RoutedEventArgs e)
  283. {
  284. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  285. {
  286. PDFViewControl.PDFView.UndoManager?.Redo();
  287. }
  288. }
  289. }
  290. }