CPDFSignatureUI.xaml.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFAnnotation.Form;
  4. using ComPDFKit.Tool;
  5. using ComPDFKit.Controls.Data;
  6. using ComPDFKit.Controls.PDFControl;
  7. using ComPDFKit.Controls.Properties;
  8. using ComPDFKitViewer;
  9. using System;
  10. using System.Collections.ObjectModel;
  11. using System.IO;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Input;
  16. using System.Windows.Media.Imaging;
  17. using ComPDFKit.Controls.Helper;
  18. using ComPDFKit.Tool.Help;
  19. namespace ComPDFKit.Controls.Annotation.PDFAnnotationPanel.PDFAnnotationUI
  20. {
  21. public partial class CPDFSignatureUI : UserControl
  22. {
  23. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  24. public ObservableCollection<CPDFSignatureData> SignatureList { get; set; }
  25. private SignatureParam signatureParam;
  26. private PDFViewControl viewControl;
  27. private CPDFSignatureWidget signatureWidget;
  28. public CPDFSignatureUI()
  29. {
  30. InitializeComponent();
  31. }
  32. public void SetFormProperty(AnnotParam param, PDFViewControl view, CPDFAnnotation sign)
  33. {
  34. signatureParam = (SignatureParam) param;
  35. viewControl = view;
  36. signatureWidget = (CPDFSignatureWidget)sign;
  37. }
  38. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  39. {
  40. SignatureList = new ObservableCollection<CPDFSignatureData>();
  41. Binding binding = new Binding();
  42. binding.Source = SignatureList;
  43. SignatureListBox.SetBinding(ItemsControl.ItemsSourceProperty, binding);
  44. LoadSettings();
  45. }
  46. private void LoadSettings()
  47. {
  48. SignatureList items = Settings.Default.SignatureList;
  49. if (items != null)
  50. {
  51. for (int i = 0; i < items.Count; i++)
  52. {
  53. CPDFSignatureData customStamp = new CPDFSignatureData();
  54. customStamp.SourcePath = items[i].SourcePath;
  55. customStamp.DrawingPath = items[i].DrawingPath;
  56. customStamp.inkThickness = items[i].inkThickness;
  57. customStamp.inkColor = items[i].inkColor;
  58. customStamp.Type = items[i].Type;
  59. customStamp.AnnotationType = CPDFAnnotationType.Signature;
  60. SignatureList.Add(customStamp);
  61. }
  62. }
  63. }
  64. private void Signature_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  65. {
  66. if (signatureParam == null || viewControl == null)
  67. {
  68. PropertyChanged?.Invoke(this, (sender as ListBoxItem).DataContext as CPDFSignatureData);
  69. }
  70. else
  71. {
  72. FillForm(((sender as ListBoxItem).DataContext as CPDFSignatureData).SourcePath);
  73. }
  74. }
  75. private void FillForm(string imagePath)
  76. {
  77. if (!string.IsNullOrEmpty(imagePath))
  78. {
  79. byte[] imageData = null;
  80. int imageWidth = 0;
  81. int imageHeight = 0;
  82. PDFHelp.ImagePathToByte(imagePath, ref imageData, ref imageWidth, ref imageHeight);
  83. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  84. {
  85. if (signatureWidget != null && signatureWidget.IsValid())
  86. {
  87. signatureWidget.UpdateApWithImage(imageData, imageWidth, imageHeight, C_Scale_Type.fitCenter, 0);
  88. if (viewControl != null && viewControl.PDFViewTool != null)
  89. {
  90. viewControl.UpdateAnnotFrame();
  91. }
  92. }
  93. }
  94. }
  95. }
  96. private void CreateSignature_Click(object sender, RoutedEventArgs e)
  97. {
  98. CPDFCreateSignatureDialog createStampDialog = new CPDFCreateSignatureDialog();
  99. createStampDialog.Owner = Window.GetWindow(this);
  100. createStampDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  101. createStampDialog.ShowDialog();
  102. UpDataSignature(createStampDialog.cPDFSignatureData);
  103. }
  104. private void UpDataSignature(CPDFSignatureData SignatureData)
  105. {
  106. if (SignatureData != null)
  107. {
  108. SignatureList.Add(SignatureData);
  109. SignatureList Signature = Settings.Default.SignatureList;
  110. if (Signature == null)
  111. {
  112. Signature = Settings.Default.SignatureList = new SignatureList();
  113. }
  114. Signature.Add(SignatureData);
  115. Settings.Default.Save();
  116. }
  117. }
  118. private void Delete_Click(object sender, RoutedEventArgs e)
  119. {
  120. Button ThisButton = sender as Button;
  121. if (ThisButton != null)
  122. {
  123. CPDFSignatureData data = ThisButton.DataContext as CPDFSignatureData;
  124. if (data != null)
  125. {
  126. int index = SignatureList.IndexOf(data);
  127. if (index != -1)
  128. {
  129. CPDFDeleteDialog cPDFDeleteDialog = new CPDFDeleteDialog(LanguageHelper.CommonManager.GetString("Caption_Warning"),
  130. LanguageHelper.CommonManager.GetString("Warn_Delete"));
  131. cPDFDeleteDialog.Owner = Window.GetWindow(this);
  132. cPDFDeleteDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  133. cPDFDeleteDialog.ShowDialog();
  134. if (!cPDFDeleteDialog.IsDelete)
  135. {
  136. PropertyChanged?.Invoke(this, null);
  137. return;
  138. }
  139. if (data.SourcePath != null)
  140. {
  141. try
  142. {
  143. if (File.Exists(data.SourcePath))
  144. {
  145. File.Delete(data.SourcePath);
  146. }
  147. }
  148. catch (Exception)
  149. {
  150. }
  151. }
  152. SignatureList.RemoveAt(index);
  153. SignatureList Signature = Settings.Default.SignatureList;
  154. if (Signature != null)
  155. {
  156. Signature.RemoveAt(index);
  157. Settings.Default.Save();
  158. }
  159. PropertyChanged?.Invoke(this, null);
  160. }
  161. }
  162. }
  163. }
  164. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  165. {
  166. signatureParam = null;
  167. viewControl = null;
  168. }
  169. }
  170. }