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