CPDFFreehandUI.xaml.cs 6.0 KB

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