CPDFMarkupUI.xaml.cs 7.2 KB

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