CPDFFreehandUI.xaml.cs 7.0 KB

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