CPDFFreehandUI.xaml.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFAnnotation.Form;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.Tool;
  5. using ComPDFKit.Tool.Help;
  6. using Compdfkit_Tools.Data;
  7. using Compdfkit_Tools.PDFControl;
  8. using System;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Media;
  12. namespace Compdfkit_Tools.PDFControlUI
  13. {
  14. public partial class CPDFFreehandUI : UserControl
  15. {
  16. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  17. public event EventHandler<bool> EraseClickHandler;
  18. public event EventHandler<double> EraseChangeHandler;
  19. private InkParam inkParam = null;
  20. private CPDFInkAnnotation cPDFAnnotation = null;
  21. private PDFViewControl pdfViewerControl = null;
  22. private CPDFDocument cPDFDocument = null;
  23. public CPDFFreehandUI()
  24. {
  25. InitializeComponent();
  26. ColorPickerControl.ColorChanged -= ColorPickerControl_ColorChanged;
  27. CPDFOpacityControl.OpacityChanged -= CPDFOpacityControl_OpacityChanged;
  28. CPDFThicknessControl.ThicknessChanged -= CPDFThicknessControl_ThicknessChanged;
  29. ColorPickerControl.ColorChanged += ColorPickerControl_ColorChanged;
  30. CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
  31. CPDFThicknessControl.ThicknessChanged += CPDFThicknessControl_ThicknessChanged;
  32. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  33. EraseThickness.ThicknessChanged -= EraseThickness_ThicknessChanged;
  34. EraseThickness.ThicknessChanged += EraseThickness_ThicknessChanged;
  35. }
  36. private void EraseThickness_ThicknessChanged(object sender, EventArgs e)
  37. {
  38. EraseChangeHandler?.Invoke(this, EraseThickness.Thickness);
  39. EraseCircle.Width = EraseThickness.Thickness * 6;
  40. EraseCircle.Height = EraseThickness.Thickness * 6;
  41. }
  42. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  43. {
  44. if (cPDFAnnotation == null)
  45. {
  46. PropertyChanged?.Invoke(this, GetFreehandData());
  47. }
  48. else
  49. {
  50. double transparent = CPDFOpacityControl.OpacityValue / 100.0;
  51. if(transparent<=1)
  52. {
  53. transparent = transparent * 255;
  54. }
  55. cPDFAnnotation.SetTransparency((byte)transparent);
  56. pdfViewerControl.UpdateAnnotFrame();
  57. }
  58. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  59. }
  60. private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
  61. {
  62. if (cPDFAnnotation == null)
  63. {
  64. PropertyChanged?.Invoke(this, GetFreehandData());
  65. }
  66. else
  67. {
  68. cPDFAnnotation.SetThickness(CPDFThicknessControl.Thickness);
  69. pdfViewerControl.UpdateAnnotFrame();
  70. }
  71. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  72. }
  73. private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
  74. {
  75. if (cPDFAnnotation == null)
  76. {
  77. PropertyChanged?.Invoke(this, GetFreehandData());
  78. }
  79. else
  80. {
  81. byte[] Color = new byte[3];
  82. Color[0] = ((SolidColorBrush)ColorPickerControl.Brush).Color.R;
  83. Color[1] = ((SolidColorBrush)ColorPickerControl.Brush).Color.G;
  84. Color[2] = ((SolidColorBrush)ColorPickerControl.Brush).Color.B;
  85. cPDFAnnotation.SetInkColor(Color);
  86. pdfViewerControl.UpdateAnnotFrame();
  87. }
  88. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  89. }
  90. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  91. {
  92. if (cPDFAnnotation == null)
  93. {
  94. PropertyChanged?.Invoke(this, GetFreehandData());
  95. }
  96. else
  97. {
  98. byte[] Color = new byte[3];
  99. Color[0] = ((SolidColorBrush)ColorPickerControl.Brush).Color.R;
  100. Color[1] = ((SolidColorBrush)ColorPickerControl.Brush).Color.G;
  101. Color[2] = ((SolidColorBrush)ColorPickerControl.Brush).Color.B;
  102. cPDFAnnotation.SetContent(NoteTextBox.Text);
  103. pdfViewerControl.UpdateAnnotFrame();
  104. }
  105. }
  106. public void SetPresentAnnotAttrib(AnnotParam annotParam, CPDFAnnotation annotation, CPDFDocument doc, PDFViewControl cPDFViewer)
  107. {
  108. inkParam = (InkParam)annotParam;
  109. cPDFAnnotation = (CPDFInkAnnotation)annotation;
  110. pdfViewerControl = cPDFViewer;
  111. cPDFDocument = doc;
  112. if (inkParam==null)
  113. {
  114. return;
  115. }
  116. ColorPickerControl.Brush = new SolidColorBrush(ParamConverter.ConverterByteForColor(inkParam.InkColor));
  117. CPDFOpacityControl.OpacityValue = (int)(inkParam.Transparency / 255D * 100);
  118. CPDFThicknessControl.Thickness = Convert.ToInt16(inkParam.Thickness);
  119. ColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(inkParam.InkColor));
  120. NoteTextBox.Text = inkParam.Content;
  121. }
  122. public CPDFFreehandData GetFreehandData()
  123. {
  124. CPDFFreehandData pdfFreehandData = new CPDFFreehandData();
  125. pdfFreehandData.AnnotationType = CPDFAnnotationType.Freehand;
  126. pdfFreehandData.BorderColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  127. pdfFreehandData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  128. pdfFreehandData.Thickness = CPDFThicknessControl.Thickness;
  129. pdfFreehandData.Note = NoteTextBox.Text;
  130. return pdfFreehandData;
  131. }
  132. public void SetEraseCheck(bool isCheck)
  133. {
  134. if(isCheck)
  135. {
  136. FreehandBtn.IsChecked = false;
  137. EraseBtn.IsChecked = true;
  138. FreehandPanel.Visibility = Visibility.Collapsed;
  139. ErasePanel.Visibility = Visibility.Visible;
  140. CPDFAnnotationPreviewerControl.Visibility = Visibility.Collapsed;
  141. EraseCirclePanel.Visibility = Visibility.Visible;
  142. }
  143. else
  144. {
  145. FreehandBtn.IsChecked = true;
  146. EraseBtn.IsChecked = false;
  147. FreehandPanel.Visibility = Visibility.Visible;
  148. ErasePanel.Visibility = Visibility.Collapsed;
  149. CPDFAnnotationPreviewerControl.Visibility = Visibility.Visible;
  150. EraseCirclePanel.Visibility=Visibility.Collapsed;
  151. }
  152. }
  153. internal void ClearAnnotAttribEvent()
  154. {
  155. cPDFAnnotation = null;
  156. }
  157. internal int GetEraseThickness()
  158. {
  159. return EraseThickness.Thickness;
  160. }
  161. private void FreehandBtn_Click(object sender, RoutedEventArgs e)
  162. {
  163. SetEraseCheck(false);
  164. EraseClickHandler?.Invoke(this, false);
  165. }
  166. private void EraseBtn_Click(object sender, RoutedEventArgs e)
  167. {
  168. SetEraseCheck(true);
  169. EraseClickHandler?.Invoke(this, true);
  170. }
  171. }
  172. }