CPDFSignatureUI.xaml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. viewControl.PDFViewTool.IsDocumentModified = true;
  75. }
  76. private void FillForm(string imagePath)
  77. {
  78. if (!string.IsNullOrEmpty(imagePath))
  79. {
  80. byte[] imageData = null;
  81. int imageWidth = 0;
  82. int imageHeight = 0;
  83. PDFHelp.ImagePathToByte(imagePath, ref imageData, ref imageWidth, ref imageHeight);
  84. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  85. {
  86. if (signatureWidget != null && signatureWidget.IsValid())
  87. {
  88. signatureWidget.UpdateApWithImage(imageData, imageWidth, imageHeight, C_Scale_Type.fitCenter, 0);
  89. if (viewControl != null && viewControl.PDFViewTool != null)
  90. {
  91. viewControl.UpdateAnnotFrame();
  92. }
  93. }
  94. }
  95. }
  96. }
  97. private void CreateSignature_Click(object sender, RoutedEventArgs e)
  98. {
  99. CPDFCreateSignatureDialog createStampDialog = new CPDFCreateSignatureDialog();
  100. createStampDialog.Owner = Window.GetWindow(this);
  101. createStampDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  102. createStampDialog.ShowDialog();
  103. UpDataSignature(createStampDialog.cPDFSignatureData);
  104. }
  105. private void UpDataSignature(CPDFSignatureData SignatureData)
  106. {
  107. if (SignatureData != null)
  108. {
  109. SignatureList.Add(SignatureData);
  110. SignatureList Signature = Settings.Default.SignatureList;
  111. if (Signature == null)
  112. {
  113. Signature = Settings.Default.SignatureList = new SignatureList();
  114. }
  115. Signature.Add(SignatureData);
  116. Settings.Default.Save();
  117. }
  118. }
  119. private void Delete_Click(object sender, RoutedEventArgs e)
  120. {
  121. Button ThisButton = sender as Button;
  122. if (ThisButton != null)
  123. {
  124. CPDFSignatureData data = ThisButton.DataContext as CPDFSignatureData;
  125. if (data != null)
  126. {
  127. int index = SignatureList.IndexOf(data);
  128. if (index != -1)
  129. {
  130. CPDFDeleteDialog cPDFDeleteDialog = new CPDFDeleteDialog(LanguageHelper.CommonManager.GetString("Caption_Warning"),
  131. LanguageHelper.CommonManager.GetString("Warn_Delete"));
  132. cPDFDeleteDialog.Owner = Window.GetWindow(this);
  133. cPDFDeleteDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  134. cPDFDeleteDialog.ShowDialog();
  135. if (!cPDFDeleteDialog.IsDelete)
  136. {
  137. PropertyChanged?.Invoke(this, null);
  138. return;
  139. }
  140. if (data.SourcePath != null)
  141. {
  142. try
  143. {
  144. if (File.Exists(data.SourcePath))
  145. {
  146. File.Delete(data.SourcePath);
  147. }
  148. }
  149. catch (Exception)
  150. {
  151. }
  152. }
  153. SignatureList.RemoveAt(index);
  154. SignatureList Signature = Settings.Default.SignatureList;
  155. if (Signature != null)
  156. {
  157. Signature.RemoveAt(index);
  158. Settings.Default.Save();
  159. }
  160. PropertyChanged?.Invoke(this, null);
  161. }
  162. }
  163. }
  164. }
  165. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  166. {
  167. signatureParam = null;
  168. viewControl = null;
  169. }
  170. }
  171. }