CPDFSignatureUI.xaml.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFAnnotation.Form;
  4. using ComPDFKit.Tool;
  5. using Compdfkit_Tools.Data;
  6. using Compdfkit_Tools.PDFControl;
  7. using Compdfkit_Tools.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. namespace Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI
  18. {
  19. public partial class CPDFSignatureUI : UserControl
  20. {
  21. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  22. public ObservableCollection<CPDFSignatureData> SignatureList { get; set; }
  23. private SignatureParam signatureParam;
  24. private PDFViewControl viewControl;
  25. private CPDFSignatureWidget signatureWidget;
  26. public CPDFSignatureUI()
  27. {
  28. InitializeComponent();
  29. }
  30. public void SetFormProperty(AnnotParam param, PDFViewControl view, CPDFAnnotation sign)
  31. {
  32. signatureParam = (SignatureParam) param;
  33. viewControl = view;
  34. signatureWidget = (CPDFSignatureWidget)sign;
  35. }
  36. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  37. {
  38. SignatureList = new ObservableCollection<CPDFSignatureData>();
  39. Binding binding = new Binding();
  40. binding.Source = SignatureList;
  41. SignatureListBox.SetBinding(ItemsControl.ItemsSourceProperty, binding);
  42. LoadSettings();
  43. }
  44. private void LoadSettings()
  45. {
  46. SignatureList items = Settings.Default.SignatureList;
  47. if (items != null)
  48. {
  49. for (int i = 0; i < items.Count; i++)
  50. {
  51. CPDFSignatureData customStamp = new CPDFSignatureData();
  52. customStamp.SourcePath = items[i].SourcePath;
  53. customStamp.DrawingPath = items[i].DrawingPath;
  54. customStamp.inkThickness = items[i].inkThickness;
  55. customStamp.inkColor = items[i].inkColor;
  56. customStamp.Type = items[i].Type;
  57. customStamp.AnnotationType = CPDFAnnotationType.Signature;
  58. SignatureList.Add(customStamp);
  59. }
  60. }
  61. }
  62. private void Signature_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  63. {
  64. if (signatureParam == null || viewControl == null)
  65. {
  66. PropertyChanged?.Invoke(this, (sender as ListBoxItem).DataContext as CPDFSignatureData);
  67. }
  68. else
  69. {
  70. FillForm(((sender as ListBoxItem).DataContext as CPDFSignatureData).SourcePath);
  71. }
  72. }
  73. private void FillForm(string ImagePath)
  74. {
  75. if (!string.IsNullOrEmpty(ImagePath))
  76. {
  77. using (FileStream fileData = File.OpenRead(ImagePath))
  78. {
  79. BitmapFrame frame = null;
  80. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  81. if (decoder != null && decoder.Frames.Count > 0)
  82. {
  83. frame = decoder.Frames[0];
  84. }
  85. if (frame != null)
  86. {
  87. byte[] ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  88. int ImageWidth = frame.PixelWidth;
  89. int ImageHeight = frame.PixelHeight;
  90. frame.CopyPixels(ImageArray, frame.PixelWidth * 4, 0);
  91. if(signatureWidget!=null && signatureWidget.IsValid())
  92. {
  93. signatureWidget.UpdateApWithImage(ImageArray, ImageWidth, ImageHeight, C_Scale_Type.fitCenter, 0);
  94. if(viewControl!=null && viewControl.PDFViewTool!=null)
  95. {
  96. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  97. pdfViewer.UpDataAnnotFrame();
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. private void CreateSignature_Click(object sender, RoutedEventArgs e)
  105. {
  106. CPDFCreateSignatureDialog createStampDialog = new CPDFCreateSignatureDialog();
  107. createStampDialog.Owner = Window.GetWindow(this);
  108. createStampDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  109. createStampDialog.ShowDialog();
  110. UpDataSignature(createStampDialog.cPDFSignatureData);
  111. }
  112. private void UpDataSignature(CPDFSignatureData SignatureData)
  113. {
  114. if (SignatureData != null)
  115. {
  116. SignatureList.Add(SignatureData);
  117. SignatureList Signature = Settings.Default.SignatureList;
  118. if (Signature == null)
  119. {
  120. Signature = Settings.Default.SignatureList = new SignatureList();
  121. }
  122. Signature.Add(SignatureData);
  123. Settings.Default.Save();
  124. }
  125. }
  126. private void Delete_Click(object sender, RoutedEventArgs e)
  127. {
  128. Button ThisButton = sender as Button;
  129. if (ThisButton != null)
  130. {
  131. CPDFSignatureData data = ThisButton.DataContext as CPDFSignatureData;
  132. if (data != null)
  133. {
  134. int index = SignatureList.IndexOf(data);
  135. if (index != -1)
  136. {
  137. CPDFDeleteDialog cPDFDeleteDialog = new CPDFDeleteDialog("Warning", "Are you sure to delete?");
  138. cPDFDeleteDialog.Owner = Window.GetWindow(this);
  139. cPDFDeleteDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  140. cPDFDeleteDialog.ShowDialog();
  141. if (!cPDFDeleteDialog.IsDelete)
  142. {
  143. PropertyChanged?.Invoke(this, null);
  144. return;
  145. }
  146. if (data.SourcePath != null)
  147. {
  148. try
  149. {
  150. if (File.Exists(data.SourcePath))
  151. {
  152. File.Delete(data.SourcePath);
  153. }
  154. }
  155. catch (Exception)
  156. {
  157. }
  158. }
  159. SignatureList.RemoveAt(index);
  160. SignatureList Signature = Settings.Default.SignatureList;
  161. if (Signature != null)
  162. {
  163. Signature.RemoveAt(index);
  164. Settings.Default.Save();
  165. }
  166. PropertyChanged?.Invoke(this, null);
  167. }
  168. }
  169. }
  170. }
  171. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  172. {
  173. signatureParam = null;
  174. viewControl = null;
  175. }
  176. }
  177. }