CPDFSignatureUI.xaml.cs 7.0 KB

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