CPDFMarkupUI.xaml.cs 10 KB

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