CPDFFreehandUI.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using Compdfkit_Tools.Data;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using ComPDFKitViewer.PdfViewer;
  19. namespace Compdfkit_Tools.PDFControlUI
  20. {
  21. /// <summary>
  22. /// CPDFFreeHandUI.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class CPDFFreehandUI : UserControl
  25. {
  26. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  27. public event EventHandler<bool> EraseClickHandler;
  28. public event EventHandler<double> EraseChangeHandler;
  29. private AnnotAttribEvent annotAttribEvent;
  30. public CPDFFreehandUI()
  31. {
  32. InitializeComponent();
  33. ColorPickerControl.ColorChanged += ColorPickerControl_ColorChanged;
  34. CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
  35. CPDFThicknessControl.ThicknessChanged += CPDFThicknessControl_ThicknessChanged;
  36. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  37. EraseThickness.ThicknessChanged += EraseThickness_ThicknessChanged;
  38. }
  39. private void EraseThickness_ThicknessChanged(object sender, EventArgs e)
  40. {
  41. EraseChangeHandler?.Invoke(this, EraseThickness.Thickness);
  42. EraseCircle.Width = EraseThickness.Thickness * 6;
  43. EraseCircle.Height = EraseThickness.Thickness * 6;
  44. }
  45. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  46. {
  47. if (annotAttribEvent == null)
  48. {
  49. PropertyChanged?.Invoke(this, GetFreehandData());
  50. }
  51. else
  52. {
  53. annotAttribEvent.UpdateAttrib(AnnotAttrib.Transparency, CPDFOpacityControl.OpacityValue / 100.0);
  54. annotAttribEvent.UpdateAnnot();
  55. }
  56. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  57. }
  58. private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
  59. {
  60. if (annotAttribEvent == null)
  61. {
  62. PropertyChanged?.Invoke(this, GetFreehandData());
  63. }
  64. else
  65. {
  66. annotAttribEvent.UpdateAttrib(AnnotAttrib.Thickness, CPDFThicknessControl.Thickness);
  67. annotAttribEvent.UpdateAnnot();
  68. }
  69. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  70. }
  71. private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
  72. {
  73. if (annotAttribEvent == null)
  74. {
  75. PropertyChanged?.Invoke(this, GetFreehandData());
  76. }
  77. else
  78. {
  79. annotAttribEvent.UpdateAttrib(AnnotAttrib.Color, ((SolidColorBrush)ColorPickerControl.Brush).Color);
  80. annotAttribEvent.UpdateAnnot();
  81. }
  82. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  83. }
  84. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  85. {
  86. if (annotAttribEvent == null)
  87. {
  88. PropertyChanged?.Invoke(this, GetFreehandData());
  89. }
  90. else
  91. {
  92. annotAttribEvent.UpdateAttrib(AnnotAttrib.NoteText, NoteTextBox.Text);
  93. annotAttribEvent.UpdateAnnot();
  94. }
  95. }
  96. public void SetPresentAnnotAttrib(AnnotAttribEvent annotAttribEvent)
  97. {
  98. this.annotAttribEvent = null;
  99. ColorPickerControl.Brush = new SolidColorBrush((Color)annotAttribEvent.Attribs[AnnotAttrib.Color]);
  100. CPDFOpacityControl.OpacityValue = (int)((double)annotAttribEvent.Attribs[AnnotAttrib.Transparency] * 100);
  101. CPDFThicknessControl.Thickness =Convert.ToInt16(annotAttribEvent.Attribs[AnnotAttrib.Thickness]);
  102. if(annotAttribEvent.Attribs.ContainsKey(AnnotAttrib.Color))
  103. {
  104. ColorPickerControl.SetCheckedForColor((Color)annotAttribEvent.Attribs[AnnotAttrib.Color]);
  105. }
  106. NoteTextBox.Text = (string)annotAttribEvent.Attribs[AnnotAttrib.NoteText];
  107. this.annotAttribEvent = annotAttribEvent;
  108. }
  109. public CPDFFreehandData GetFreehandData()
  110. {
  111. CPDFFreehandData pdfFreehandData = new CPDFFreehandData();
  112. pdfFreehandData.AnnotationType = CPDFAnnotationType.Freehand;
  113. pdfFreehandData.BorderColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  114. pdfFreehandData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  115. pdfFreehandData.Thickness = CPDFThicknessControl.Thickness;
  116. pdfFreehandData.Note = NoteTextBox.Text;
  117. return pdfFreehandData;
  118. }
  119. public void SetEraseCheck(bool isCheck)
  120. {
  121. if(isCheck)
  122. {
  123. FreehandBtn.IsChecked = false;
  124. EraseBtn.IsChecked = true;
  125. FreehandPanel.Visibility = Visibility.Collapsed;
  126. ErasePanel.Visibility = Visibility.Visible;
  127. CPDFAnnotationPreviewerControl.Visibility = Visibility.Collapsed;
  128. EraseCirclePanel.Visibility = Visibility.Visible;
  129. }
  130. else
  131. {
  132. FreehandBtn.IsChecked = true;
  133. EraseBtn.IsChecked = false;
  134. FreehandPanel.Visibility = Visibility.Visible;
  135. ErasePanel.Visibility = Visibility.Collapsed;
  136. CPDFAnnotationPreviewerControl.Visibility = Visibility.Visible;
  137. EraseCirclePanel.Visibility=Visibility.Collapsed;
  138. }
  139. }
  140. internal void ClearAnnotAttribEvent()
  141. {
  142. annotAttribEvent = null;
  143. }
  144. internal int GetEraseThickness()
  145. {
  146. return EraseThickness.Thickness;
  147. }
  148. private void FreehandBtn_Click(object sender, RoutedEventArgs e)
  149. {
  150. SetEraseCheck(false);
  151. EraseClickHandler?.Invoke(this, false);
  152. }
  153. private void EraseBtn_Click(object sender, RoutedEventArgs e)
  154. {
  155. SetEraseCheck(true);
  156. EraseClickHandler?.Invoke(this, true);
  157. }
  158. }
  159. }