FillDigitalSignatureControl.xaml.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFAnnotation.Form;
  4. using ComPDFKit.PDFDocument;
  5. using ComPDFKit.PDFPage;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Reflection;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Controls.Primitives;
  13. using System.Windows.Ink;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Media.Media3D;
  18. namespace Compdfkit_Tools.PDFControl
  19. {
  20. /// <summary>
  21. /// CPDFSignControl.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class FillDigitalSignatureControl : UserControl
  24. {
  25. private Dictionary<string, Border> TabDict { get; set; }
  26. private SignatureConfig tempSignatureConfig = new SignatureConfig();
  27. private string _filePath = string.Empty;
  28. public string FilePath
  29. {
  30. get => _filePath;
  31. set
  32. {
  33. _filePath = value;
  34. }
  35. }
  36. public FillDigitalSignatureControl()
  37. {
  38. InitializeComponent();
  39. TabDict = new Dictionary<string, Border>
  40. {
  41. ["Keyboard"] = KeyboardBorder,
  42. ["Trackpad"] = TrackpadBorder,
  43. ["Image"] = ImageBorder,
  44. ["None"] = NoneBorder
  45. };
  46. SetCheckedTab("Keyboard");
  47. CreateTempSignature();
  48. }
  49. private void CreateTempSignature()
  50. {
  51. CPDFDocument tempDocument = CPDFDocument.CreateDocument();
  52. tempDocument.InsertPage(0, 200, 200, null);
  53. CPDFPage page = tempDocument.PageAtIndex(0);
  54. CPDFSignatureWidget signatureWidget = page.CreateWidget(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS) as CPDFSignatureWidget;
  55. signatureWidget.SetRect(new CRect(0, 100, 100, 0));
  56. signatureWidget.UpdataApWithSignature(tempSignatureConfig);
  57. signatureWidget.UpdateFormAp();
  58. //if(signatureWidget.UpdateApWithImage("C:\\Users\\dkan\\Pictures\\Screenshots\\hao.jpg", "", 0))
  59. //{
  60. // signatureWidget.UpdateAp();
  61. //}
  62. byte[] signatureBitmapBytes = GetTempSignatureImage(signatureWidget, out int width, out int height);
  63. tempDocument.WriteToFilePath("E:\\testfile1.pdf");
  64. signatureWidget.ReleaseAnnot();
  65. if (signatureBitmapBytes.Length > 0)
  66. {
  67. PixelFormat fmt = PixelFormats.Bgra32;
  68. BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, signatureBitmapBytes, (width * fmt.BitsPerPixel + 7) / 8);
  69. imageControl.Source = bps;
  70. }
  71. else
  72. {
  73. imageControl.Source = null;
  74. }
  75. }
  76. public static byte[] GetTempSignatureImage(CPDFSignatureWidget signatureWidget, out int width, out int height)
  77. {
  78. CRect rect = signatureWidget.GetRect();
  79. var flags = BindingFlags.NonPublic | BindingFlags.Static;
  80. var dpiProperty = typeof(SystemParameters).GetProperty("Dpi", flags);
  81. int dpi = (int)dpiProperty.GetValue(null, null);
  82. width = (int)(rect.width() * dpi / 72D * 2);
  83. height = (int)(rect.height() * dpi / 72D * 2);
  84. byte[] imageData = new byte[width * height * 4];
  85. signatureWidget.RenderAnnot(width, height, imageData, CPDFAppearanceType.Normal);
  86. return imageData;
  87. }
  88. private void TextAlignBtn_Click(object sender, RoutedEventArgs e)
  89. {
  90. ToggleButton checkBtn = sender as ToggleButton;
  91. if (checkBtn == null)
  92. {
  93. return;
  94. }
  95. checkBtn.IsChecked = true;
  96. if (checkBtn != TextAlignLeftBtn)
  97. {
  98. TextAlignLeftBtn.IsChecked = false;
  99. }
  100. if (checkBtn != TextAlignRightBtn)
  101. {
  102. TextAlignRightBtn.IsChecked = false;
  103. }
  104. }
  105. private void Border_MouseDown(object sender, MouseButtonEventArgs e)
  106. {
  107. Border clickBorder = sender as Border;
  108. if (clickBorder == null || clickBorder.Tag == null)
  109. {
  110. return;
  111. }
  112. SetCheckedTab(clickBorder.Tag.ToString());
  113. ImagePickPanel.Visibility = Visibility.Hidden;
  114. if (clickBorder == TrackpadBorder)
  115. {
  116. CanvaDrawPopup.Visibility = Visibility.Visible;
  117. }
  118. if (clickBorder == ImageBorder)
  119. {
  120. ImagePickPanel.Visibility = Visibility.Visible;
  121. }
  122. }
  123. private void SetCheckedTab(string tab)
  124. {
  125. if (TabDict != null && TabDict.ContainsKey(tab))
  126. {
  127. foreach (string key in TabDict.Keys)
  128. {
  129. Border checkBorder = TabDict[key];
  130. if (checkBorder == null)
  131. {
  132. continue;
  133. }
  134. checkBorder.BorderThickness = new Thickness(0);
  135. if (key == tab)
  136. {
  137. checkBorder.BorderThickness = new Thickness(0, 0, 0, 2);
  138. }
  139. }
  140. }
  141. }
  142. private void CanvasPopupClose_Click(object sender, RoutedEventArgs e)
  143. {
  144. CanvaDrawPopup.Visibility = Visibility.Collapsed;
  145. }
  146. private void CanvasClearBtn_Click(object sender, RoutedEventArgs e)
  147. {
  148. DrawInkCanvas.Strokes.Clear();
  149. }
  150. public void GetDrawInk()
  151. {
  152. if (DrawInkCanvas != null && DrawInkCanvas.Strokes != null && DrawInkCanvas.Strokes.Count > 0)
  153. {
  154. Rect bound = DrawInkCanvas.Strokes.GetBounds();
  155. DrawingVisual drawVisual = new DrawingVisual();
  156. DrawingContext drawContext = drawVisual.RenderOpen();
  157. foreach (Stroke drawStroke in DrawInkCanvas.Strokes)
  158. {
  159. Pen drawPen = new Pen(new SolidColorBrush(drawStroke.DrawingAttributes.Color), drawStroke.DrawingAttributes.Width);
  160. PathGeometry drawPath = new PathGeometry();
  161. PathFigureCollection Figures = new PathFigureCollection();
  162. PathFigure AddFigure = new PathFigure();
  163. Figures.Add(AddFigure);
  164. drawPath.Figures = Figures;
  165. if (drawStroke.StylusPoints.Count > 1)
  166. {
  167. StylusPoint startPoint = drawStroke.StylusPoints[0];
  168. AddFigure.StartPoint = new Point(startPoint.X - bound.X, startPoint.Y - bound.Y);
  169. for (int i = 1; i < drawStroke.StylusPoints.Count; i++)
  170. {
  171. StylusPoint drawPoint = drawStroke.StylusPoints[i];
  172. Point offsetPoint = new Point(drawPoint.X - bound.X, drawPoint.Y - bound.Y);
  173. LineSegment drawSegment = new LineSegment();
  174. drawSegment.Point = offsetPoint;
  175. AddFigure.Segments.Add(drawSegment);
  176. }
  177. }
  178. if (AddFigure.Segments.Count > 0)
  179. {
  180. drawContext.DrawGeometry(null, drawPen, drawPath);
  181. }
  182. }
  183. drawContext.Close();
  184. RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)bound.Width, (int)bound.Height, 96, 96, PixelFormats.Pbgra32);
  185. renderBitmap.Render(drawVisual);
  186. }
  187. }
  188. private void ReasonCheckBox_Click(object sender, RoutedEventArgs e)
  189. {
  190. CheckBox checkItem = sender as CheckBox;
  191. if (checkItem == null)
  192. {
  193. return;
  194. }
  195. ReasonPanel.Visibility = checkItem.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
  196. }
  197. }
  198. }