CPDFMarkupUI.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.Tool;
  3. using ComPDFKit.Controls.Common;
  4. using ComPDFKit.Controls.Data;
  5. using ComPDFKit.Controls.PDFControl;
  6. using System;
  7. using System.Linq;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using ComPDFKit.Controls.Helper;
  11. using ComPDFKit.Tool.Help;
  12. using ComPDFKit.Tool.UndoManger;
  13. using ComPDFKitViewer.Helper;
  14. namespace ComPDFKit.Controls.PDFControlUI
  15. {
  16. public partial class CPDFMarkupUI : UserControl
  17. {
  18. private CPDFAnnotationType currentAnnotationType;
  19. private AnnotParam annotParam;
  20. private CPDFMarkupAnnotation markupAnnot;
  21. private PDFViewControl viewControl;
  22. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  23. public CPDFMarkupUI()
  24. {
  25. InitializeComponent();
  26. ColorPickerControl.ColorChanged -= ColorPickerControl_ColorChanged;
  27. CPDFOpacityControl.OpacityChanged -= CPDFOpacityControl_OpacityChanged;
  28. ColorPickerControl.ColorChanged += ColorPickerControl_ColorChanged;
  29. CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
  30. }
  31. private AnnotHistory GetAnnotHistory()
  32. {
  33. if (markupAnnot != null && markupAnnot.IsValid())
  34. {
  35. switch (markupAnnot.Type)
  36. {
  37. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  38. return new HighlightAnnotHistory();
  39. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  40. return new UnderlineAnnotHistory();
  41. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  42. return new StrikeoutAnnotHistory();
  43. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  44. return new SquigglyAnnotHistory();
  45. }
  46. }
  47. return new AnnotHistory();
  48. }
  49. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  50. {
  51. if (annotParam == null)
  52. {
  53. PropertyChanged?.Invoke(this, GetMarkupData());
  54. }
  55. else
  56. {
  57. if(markupAnnot!=null && markupAnnot.IsValid())
  58. {
  59. byte opacity = (byte)(CPDFOpacityControl.OpacityValue / 100.0 * 255);
  60. if (viewControl != null && viewControl.PDFViewTool != null)
  61. {
  62. AnnotHistory history = GetAnnotHistory();
  63. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  64. history.Action = HistoryAction.Update;
  65. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, markupAnnot.Page.PageIndex, markupAnnot);
  66. markupAnnot.SetTransparency(opacity);
  67. markupAnnot.UpdateAp();
  68. viewControl.UpdateAnnotFrame();
  69. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, markupAnnot.Page.PageIndex, markupAnnot);
  70. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  71. }
  72. }
  73. }
  74. CPDFAnnotationPreviewerControl.DrawMarkUpPreview(GetMarkupData());
  75. }
  76. private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
  77. {
  78. if (annotParam == null)
  79. {
  80. PropertyChanged?.Invoke(this, GetMarkupData());
  81. }
  82. else
  83. {
  84. SolidColorBrush colorBrush = ColorPickerControl.Brush as SolidColorBrush;
  85. if (markupAnnot != null && markupAnnot.IsValid() && colorBrush!=null)
  86. {
  87. byte[] color = new byte[3]
  88. {
  89. colorBrush.Color.R,
  90. colorBrush.Color.G,
  91. colorBrush.Color.B
  92. };
  93. if (viewControl != null && !markupAnnot.Color.SequenceEqual(color))
  94. {
  95. AnnotHistory history = GetAnnotHistory();
  96. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  97. history.Action = HistoryAction.Update;
  98. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, markupAnnot.Page.PageIndex, markupAnnot);
  99. markupAnnot.SetColor(color);
  100. markupAnnot.UpdateAp();
  101. viewControl.UpdateAnnotFrame();
  102. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, markupAnnot.Page.PageIndex, markupAnnot);
  103. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  104. }
  105. }
  106. }
  107. CPDFAnnotationPreviewerControl.DrawMarkUpPreview(GetMarkupData());
  108. }
  109. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  110. {
  111. if (annotParam == null)
  112. {
  113. PropertyChanged?.Invoke(this, GetMarkupData());
  114. }
  115. else
  116. {
  117. if (markupAnnot != null && markupAnnot.IsValid())
  118. {
  119. if (viewControl != null && markupAnnot.GetContent() != NoteTextBox.Text)
  120. {
  121. AnnotHistory history = GetAnnotHistory();
  122. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  123. history.Action = HistoryAction.Update;
  124. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, markupAnnot.Page.PageIndex, markupAnnot);
  125. markupAnnot.SetContent(NoteTextBox.Text);
  126. viewControl.UpdateAnnotFrame();
  127. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, markupAnnot.Page.PageIndex, markupAnnot);
  128. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  129. }
  130. }
  131. }
  132. }
  133. public CPDFMarkupData GetMarkupData()
  134. {
  135. CPDFMarkupData pdfMarkupData = new CPDFMarkupData();
  136. pdfMarkupData.AnnotationType = currentAnnotationType;
  137. pdfMarkupData.Color = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  138. pdfMarkupData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  139. pdfMarkupData.Note = NoteTextBox.Text;
  140. return pdfMarkupData;
  141. }
  142. public void SetPresentAnnotAttrib(AnnotParam param,CPDFMarkupAnnotation markup,PDFViewControl view)
  143. {
  144. this.annotParam = null;
  145. this.markupAnnot = null;
  146. this.viewControl = null;
  147. if(param!=null)
  148. {
  149. if(param is HighlightParam)
  150. {
  151. HighlightParam highlightParam = (HighlightParam)param;
  152. Color newColor = Color.FromRgb(
  153. highlightParam.HighlightColor[0],
  154. highlightParam.HighlightColor[1],
  155. highlightParam.HighlightColor[2]);
  156. ColorPickerControl.Brush = new SolidColorBrush(newColor);
  157. ColorPickerControl.SetCheckedForColor(newColor);
  158. }
  159. if(param is UnderlineParam)
  160. {
  161. UnderlineParam underlineParam = (UnderlineParam)param;
  162. Color newColor = Color.FromRgb(
  163. underlineParam.UnderlineColor[0],
  164. underlineParam.UnderlineColor[1],
  165. underlineParam.UnderlineColor[2]);
  166. ColorPickerControl.Brush = new SolidColorBrush(newColor);
  167. ColorPickerControl.SetCheckedForColor(newColor);
  168. }
  169. if (param is StrikeoutParam)
  170. {
  171. StrikeoutParam strikeoutParam = (StrikeoutParam)param;
  172. Color newColor = Color.FromRgb(
  173. strikeoutParam.StrikeoutColor[0],
  174. strikeoutParam.StrikeoutColor[1],
  175. strikeoutParam.StrikeoutColor[2]);
  176. ColorPickerControl.Brush = new SolidColorBrush(newColor);
  177. ColorPickerControl.SetCheckedForColor(newColor);
  178. }
  179. if(param is SquigglyParam)
  180. {
  181. SquigglyParam squigglyParam= (SquigglyParam)param;
  182. Color newColor = Color.FromRgb(
  183. squigglyParam.SquigglyColor[0],
  184. squigglyParam.SquigglyColor[1],
  185. squigglyParam.SquigglyColor[2]);
  186. ColorPickerControl.Brush = new SolidColorBrush(newColor);
  187. ColorPickerControl.SetCheckedForColor(newColor);
  188. }
  189. CPDFOpacityControl.OpacityValue = (int)(param.Transparency / 255D * 100);
  190. NoteTextBox.Text = param.Content;
  191. }
  192. this.annotParam = param;
  193. this.markupAnnot = markup;
  194. this.viewControl = view;
  195. CPDFAnnotationPreviewerControl.DrawMarkUpPreview(GetMarkupData());
  196. }
  197. public void InitWithAnnotationType(CPDFAnnotationType annotationType)
  198. {
  199. currentAnnotationType = annotationType;
  200. switch (annotationType)
  201. {
  202. case CPDFAnnotationType.Highlight:
  203. TitleTextBlock.Text = LanguageHelper.PropertyPanelManager.GetString("Title_Highlight");
  204. break;
  205. case CPDFAnnotationType.Underline:
  206. TitleTextBlock.Text = LanguageHelper.PropertyPanelManager.GetString("Title_Underline");
  207. break;
  208. case CPDFAnnotationType.Strikeout:
  209. TitleTextBlock.Text = LanguageHelper.PropertyPanelManager.GetString("Title_Strikeout");
  210. break;
  211. case CPDFAnnotationType.Squiggly:
  212. TitleTextBlock.Text = LanguageHelper.PropertyPanelManager.GetString("Title_Squiggly");
  213. break;
  214. default:
  215. throw new ArgumentException("Not Excepted Argument");
  216. }
  217. CPDFAnnotationPreviewerControl.DrawMarkUpPreview(GetMarkupData());
  218. }
  219. }
  220. }