FillDigitalSignatureControl.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Controls.Primitives;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Imaging;
  10. namespace Compdfkit_Tools.PDFControl
  11. {
  12. /// <summary>
  13. /// CPDFSignControl.xaml 的交互逻辑
  14. /// </summary>
  15. public partial class FillDigitalSignatureControl : UserControl
  16. {
  17. private Dictionary<string,Border> TabDict { get; set; }
  18. public FillDigitalSignatureControl()
  19. {
  20. InitializeComponent();
  21. TabDict=new Dictionary<string,Border>();
  22. TabDict["Keyboard"] = KeyboardBorder;
  23. TabDict["Trackpad"] = TrackpadBorder;
  24. TabDict["Image"] = ImageBorder;
  25. TabDict["None"] = NoneBorder;
  26. SetCheckedTab("Keyboard");
  27. }
  28. private void TextAlignBtn_Click(object sender, RoutedEventArgs e)
  29. {
  30. ToggleButton checkBtn=sender as ToggleButton;
  31. if(checkBtn == null)
  32. {
  33. return;
  34. }
  35. checkBtn.IsChecked = true;
  36. if(checkBtn!= TextAlignLeftBtn)
  37. {
  38. TextAlignLeftBtn.IsChecked= false;
  39. }
  40. if (checkBtn != TextAlignRightBtn)
  41. {
  42. TextAlignRightBtn.IsChecked = false;
  43. }
  44. }
  45. private void Border_MouseDown(object sender, MouseButtonEventArgs e)
  46. {
  47. Border clickBorder = sender as Border;
  48. if(clickBorder == null || clickBorder.Tag==null)
  49. {
  50. return;
  51. }
  52. SetCheckedTab(clickBorder.Tag.ToString());
  53. ImagePickPanel.Visibility = Visibility.Hidden;
  54. if (clickBorder== TrackpadBorder)
  55. {
  56. CanvaDrawPopup.Visibility = Visibility.Visible;
  57. }
  58. if(clickBorder== ImageBorder)
  59. {
  60. ImagePickPanel.Visibility = Visibility.Visible;
  61. }
  62. }
  63. private void SetCheckedTab(string tab)
  64. {
  65. if (TabDict != null && TabDict.ContainsKey(tab))
  66. {
  67. foreach (string key in TabDict.Keys)
  68. {
  69. Border checkBorder = TabDict[key];
  70. if(checkBorder == null)
  71. {
  72. continue;
  73. }
  74. checkBorder.BorderThickness = new Thickness(0);
  75. if(key==tab)
  76. {
  77. checkBorder.BorderThickness = new Thickness(0, 0, 0, 2);
  78. }
  79. }
  80. }
  81. }
  82. private void CanvasPopupClose_Click(object sender, RoutedEventArgs e)
  83. {
  84. CanvaDrawPopup.Visibility = Visibility.Collapsed;
  85. }
  86. private void CanvasClearBtn_Click(object sender, RoutedEventArgs e)
  87. {
  88. DrawInkCanvas.Strokes.Clear();
  89. }
  90. public void GetDrawInk()
  91. {
  92. if (DrawInkCanvas != null && DrawInkCanvas.Strokes != null && DrawInkCanvas.Strokes.Count > 0)
  93. {
  94. Rect bound= DrawInkCanvas.Strokes.GetBounds();
  95. DrawingVisual drawVisual = new DrawingVisual();
  96. DrawingContext drawContext= drawVisual.RenderOpen();
  97. foreach (Stroke drawStroke in DrawInkCanvas.Strokes)
  98. {
  99. Pen drawPen = new Pen(new SolidColorBrush(drawStroke.DrawingAttributes.Color), drawStroke.DrawingAttributes.Width);
  100. PathGeometry drawPath = new PathGeometry();
  101. PathFigureCollection Figures = new PathFigureCollection();
  102. PathFigure AddFigure = new PathFigure();
  103. Figures.Add(AddFigure);
  104. drawPath.Figures = Figures;
  105. if(drawStroke.StylusPoints.Count>1)
  106. {
  107. StylusPoint startPoint= drawStroke.StylusPoints[0];
  108. AddFigure.StartPoint= new Point(startPoint.X - bound.X, startPoint.Y - bound.Y);
  109. for (int i = 1;i< drawStroke.StylusPoints.Count;i++)
  110. {
  111. StylusPoint drawPoint = drawStroke.StylusPoints[i];
  112. Point offsetPoint = new Point(drawPoint.X - bound.X, drawPoint.Y - bound.Y);
  113. LineSegment drawSegment = new LineSegment();
  114. drawSegment.Point = offsetPoint;
  115. AddFigure.Segments.Add(drawSegment);
  116. }
  117. }
  118. if(AddFigure.Segments.Count > 0)
  119. {
  120. drawContext.DrawGeometry(null, drawPen, drawPath);
  121. }
  122. }
  123. drawContext.Close();
  124. RenderTargetBitmap renderBitmap=new RenderTargetBitmap((int)bound.Width,(int)bound.Height,96,96,PixelFormats.Pbgra32);
  125. renderBitmap.Render(drawVisual);
  126. }
  127. }
  128. private void ReasonCheckBox_Click(object sender, RoutedEventArgs e)
  129. {
  130. CheckBox checkItem = sender as CheckBox;
  131. if (checkItem == null)
  132. {
  133. return;
  134. }
  135. ReasonPanel.Visibility=checkItem.IsChecked==true? Visibility.Visible: Visibility.Collapsed;
  136. }
  137. }
  138. }