DigitalSignatureControl.xaml.cs 11 KB

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