CPDFMarkupUI.xaml.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.Tool;
  3. using Compdfkit_Tools.Common;
  4. using Compdfkit_Tools.Data;
  5. using Compdfkit_Tools.PDFControl;
  6. using ComPDFKitViewer;
  7. using System;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. namespace Compdfkit_Tools.PDFControlUI
  11. {
  12. public partial class CPDFMarkupUI : UserControl
  13. {
  14. private CPDFAnnotationType currentAnnotationType;
  15. private AnnotParam annotParam;
  16. private CPDFMarkupAnnotation markupAnnot;
  17. private PDFViewControl viewControl;
  18. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  19. public CPDFMarkupUI()
  20. {
  21. InitializeComponent();
  22. ColorPickerControl.ColorChanged -= ColorPickerControl_ColorChanged;
  23. CPDFOpacityControl.OpacityChanged -= CPDFOpacityControl_OpacityChanged;
  24. ColorPickerControl.ColorChanged += ColorPickerControl_ColorChanged;
  25. CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
  26. }
  27. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  28. {
  29. if (annotParam == null)
  30. {
  31. PropertyChanged?.Invoke(this, GetMarkupData());
  32. }
  33. else
  34. {
  35. if(markupAnnot!=null && markupAnnot.IsValid())
  36. {
  37. markupAnnot.SetTransparency((byte)(CPDFOpacityControl.OpacityValue / 100.0 * 255));
  38. if (viewControl != null && viewControl.PDFViewTool != null)
  39. {
  40. viewControl.UpdateAnnotFrame();
  41. }
  42. }
  43. }
  44. CPDFAnnotationPreviewerControl.DrawMarkUpPreview(GetMarkupData());
  45. }
  46. private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
  47. {
  48. if (annotParam == null)
  49. {
  50. PropertyChanged?.Invoke(this, GetMarkupData());
  51. }
  52. else
  53. {
  54. SolidColorBrush colorBrush = ColorPickerControl.Brush as SolidColorBrush;
  55. if (markupAnnot != null && markupAnnot.IsValid() && colorBrush!=null)
  56. {
  57. markupAnnot.SetColor(new byte[3]
  58. {
  59. colorBrush.Color.R,
  60. colorBrush.Color.G,
  61. colorBrush.Color.B
  62. });
  63. if (viewControl != null && viewControl.PDFViewTool != null)
  64. {
  65. viewControl.UpdateAnnotFrame();
  66. }
  67. }
  68. }
  69. CPDFAnnotationPreviewerControl.DrawMarkUpPreview(GetMarkupData());
  70. }
  71. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  72. {
  73. if (annotParam == null)
  74. {
  75. PropertyChanged?.Invoke(this, GetMarkupData());
  76. }
  77. else
  78. {
  79. if (markupAnnot != null && markupAnnot.IsValid())
  80. {
  81. markupAnnot.SetContent(NoteTextBox.Text);
  82. if (viewControl != null && viewControl.PDFViewTool != null)
  83. {
  84. viewControl.UpdateAnnotFrame();
  85. }
  86. }
  87. }
  88. }
  89. public CPDFMarkupData GetMarkupData()
  90. {
  91. CPDFMarkupData pdfMarkupData = new CPDFMarkupData();
  92. pdfMarkupData.AnnotationType = currentAnnotationType;
  93. pdfMarkupData.Color = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  94. pdfMarkupData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  95. pdfMarkupData.Note = NoteTextBox.Text;
  96. return pdfMarkupData;
  97. }
  98. public void SetPresentAnnotAttrib(AnnotParam param,CPDFMarkupAnnotation markup,PDFViewControl view)
  99. {
  100. this.annotParam = null;
  101. this.markupAnnot = null;
  102. this.viewControl = null;
  103. if(param!=null)
  104. {
  105. if(param is HighlightParam)
  106. {
  107. HighlightParam highlightParam = (HighlightParam)param;
  108. Color newColor = Color.FromRgb(
  109. highlightParam.HighlightColor[0],
  110. highlightParam.HighlightColor[1],
  111. highlightParam.HighlightColor[2]);
  112. ColorPickerControl.Brush = new SolidColorBrush(newColor);
  113. ColorPickerControl.SetCheckedForColor(newColor);
  114. }
  115. if(param is UnderlineParam)
  116. {
  117. UnderlineParam underlineParam = (UnderlineParam)param;
  118. Color newColor = Color.FromRgb(
  119. underlineParam.UnderlineColor[0],
  120. underlineParam.UnderlineColor[1],
  121. underlineParam.UnderlineColor[2]);
  122. ColorPickerControl.Brush = new SolidColorBrush(newColor);
  123. ColorPickerControl.SetCheckedForColor(newColor);
  124. }
  125. if (param is StrikeoutParam)
  126. {
  127. StrikeoutParam strikeoutParam = (StrikeoutParam)param;
  128. Color newColor = Color.FromRgb(
  129. strikeoutParam.StrikeoutColor[0],
  130. strikeoutParam.StrikeoutColor[1],
  131. strikeoutParam.StrikeoutColor[2]);
  132. ColorPickerControl.Brush = new SolidColorBrush(newColor);
  133. ColorPickerControl.SetCheckedForColor(newColor);
  134. }
  135. if(param is SquigglyParam)
  136. {
  137. SquigglyParam squigglyParam= (SquigglyParam)param;
  138. Color newColor = Color.FromRgb(
  139. squigglyParam.SquigglyColor[0],
  140. squigglyParam.SquigglyColor[1],
  141. squigglyParam.SquigglyColor[2]);
  142. ColorPickerControl.Brush = new SolidColorBrush(newColor);
  143. ColorPickerControl.SetCheckedForColor(newColor);
  144. }
  145. CPDFOpacityControl.OpacityValue = (int)(param.Transparency / 255D * 100);
  146. NoteTextBox.Text = param.Content;
  147. }
  148. this.annotParam = param;
  149. this.markupAnnot = markup;
  150. this.viewControl = view;
  151. CPDFAnnotationPreviewerControl.DrawMarkUpPreview(GetMarkupData());
  152. }
  153. public void InitWithAnnotationType(CPDFAnnotationType annotationType)
  154. {
  155. currentAnnotationType = annotationType;
  156. switch (annotationType)
  157. {
  158. case CPDFAnnotationType.Highlight:
  159. TitleTextBlock.Text = "Highlight";
  160. break;
  161. case CPDFAnnotationType.Underline:
  162. TitleTextBlock.Text = "Underline";
  163. break;
  164. case CPDFAnnotationType.Strikeout:
  165. TitleTextBlock.Text = "Strikethrough";
  166. break;
  167. case CPDFAnnotationType.Squiggly:
  168. TitleTextBlock.Text = "Squiggly";
  169. break;
  170. default:
  171. throw new ArgumentException("Not Excepted Argument");
  172. }
  173. CPDFAnnotationPreviewerControl.DrawMarkUpPreview(GetMarkupData());
  174. }
  175. }
  176. }