CPDFFreehandUI.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.Tool;
  3. using ComPDFKit.Tool.Help;
  4. using ComPDFKit.Controls.Data;
  5. using ComPDFKit.Controls.PDFControl;
  6. using System;
  7. using System.Linq;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media;
  11. using ComPDFKit.Tool.UndoManger;
  12. using ComPDFKitViewer.Helper;
  13. namespace ComPDFKit.Controls.PDFControlUI
  14. {
  15. public partial class CPDFFreehandUI : UserControl
  16. {
  17. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  18. public event EventHandler<bool> EraseClickHandler;
  19. public event EventHandler<double> EraseChangeHandler;
  20. private InkParam inkParam = null;
  21. private CPDFInkAnnotation cPDFAnnotation = null;
  22. private PDFViewControl pdfViewerControl = null;
  23. public CPDFFreehandUI()
  24. {
  25. InitializeComponent();
  26. ColorPickerControl.ColorChanged -= ColorPickerControl_ColorChanged;
  27. CPDFOpacityControl.OpacityChanged -= CPDFOpacityControl_OpacityChanged;
  28. CPDFThicknessControl.ThicknessChanged -= CPDFThicknessControl_ThicknessChanged;
  29. ColorPickerControl.ColorChanged += ColorPickerControl_ColorChanged;
  30. CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
  31. CPDFThicknessControl.ThicknessChanged += CPDFThicknessControl_ThicknessChanged;
  32. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  33. EraseThickness.ThicknessChanged -= EraseThickness_ThicknessChanged;
  34. EraseThickness.ThicknessChanged += EraseThickness_ThicknessChanged;
  35. }
  36. private void EraseThickness_ThicknessChanged(object sender, EventArgs e)
  37. {
  38. EraseChangeHandler?.Invoke(this, EraseThickness.Thickness);
  39. EraseCircle.Width = EraseThickness.Thickness * 6;
  40. EraseCircle.Height = EraseThickness.Thickness * 6;
  41. }
  42. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  43. {
  44. if (cPDFAnnotation == null)
  45. {
  46. PropertyChanged?.Invoke(this, GetFreehandData());
  47. }
  48. else
  49. {
  50. double transparent = CPDFOpacityControl.OpacityValue / 100.0;
  51. if(transparent<=1)
  52. {
  53. transparent = transparent * 255;
  54. }
  55. if (inkParam.Transparency != (byte)transparent && pdfViewerControl != null)
  56. {
  57. InkAnnotHistory history = new InkAnnotHistory();
  58. history.PDFDoc = pdfViewerControl.GetCPDFViewer().GetDocument();
  59. history.Action = HistoryAction.Update;
  60. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, cPDFAnnotation.Page.PageIndex, cPDFAnnotation);
  61. cPDFAnnotation.SetTransparency((byte)transparent);
  62. cPDFAnnotation.UpdateAp();
  63. pdfViewerControl.UpdateAnnotFrame();
  64. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, cPDFAnnotation.Page.PageIndex, cPDFAnnotation);
  65. pdfViewerControl.GetCPDFViewer().UndoManager.AddHistory(history);
  66. }
  67. }
  68. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  69. }
  70. private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
  71. {
  72. if (cPDFAnnotation == null)
  73. {
  74. PropertyChanged?.Invoke(this, GetFreehandData());
  75. }
  76. else
  77. {
  78. if (Math.Abs(inkParam.Thickness - CPDFThicknessControl.Thickness) > 0.01 && pdfViewerControl != null)
  79. {
  80. InkAnnotHistory history = new InkAnnotHistory();
  81. history.PDFDoc = pdfViewerControl.GetCPDFViewer().GetDocument();
  82. history.Action = HistoryAction.Update;
  83. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, cPDFAnnotation.Page.PageIndex, cPDFAnnotation);
  84. cPDFAnnotation.SetThickness(CPDFThicknessControl.Thickness);
  85. cPDFAnnotation.UpdateAp();
  86. pdfViewerControl.UpdateAnnotFrame();
  87. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, cPDFAnnotation.Page.PageIndex, cPDFAnnotation);
  88. pdfViewerControl.GetCPDFViewer().UndoManager.AddHistory(history);
  89. }
  90. }
  91. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  92. }
  93. private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
  94. {
  95. if (cPDFAnnotation == null)
  96. {
  97. PropertyChanged?.Invoke(this, GetFreehandData());
  98. }
  99. else
  100. {
  101. byte[] color = new byte[3];
  102. color[0] = ((SolidColorBrush)ColorPickerControl.Brush).Color.R;
  103. color[1] = ((SolidColorBrush)ColorPickerControl.Brush).Color.G;
  104. color[2] = ((SolidColorBrush)ColorPickerControl.Brush).Color.B;
  105. if (!cPDFAnnotation.InkColor.SequenceEqual(color) && pdfViewerControl != null)
  106. {
  107. InkAnnotHistory history = new InkAnnotHistory();
  108. history.PDFDoc = pdfViewerControl.GetCPDFViewer().GetDocument();
  109. history.Action = HistoryAction.Update;
  110. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, cPDFAnnotation.Page.PageIndex, cPDFAnnotation);
  111. cPDFAnnotation.SetInkColor(color);
  112. cPDFAnnotation.UpdateAp();
  113. pdfViewerControl.UpdateAnnotFrame();
  114. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, cPDFAnnotation.Page.PageIndex, cPDFAnnotation);
  115. pdfViewerControl.GetCPDFViewer().UndoManager.AddHistory(history);
  116. }
  117. }
  118. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  119. }
  120. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  121. {
  122. if (cPDFAnnotation == null)
  123. {
  124. PropertyChanged?.Invoke(this, GetFreehandData());
  125. }
  126. else
  127. {
  128. if (inkParam.Content != NoteTextBox.Text && pdfViewerControl != null)
  129. {
  130. InkAnnotHistory history = new InkAnnotHistory();
  131. history.PDFDoc = pdfViewerControl.GetCPDFViewer().GetDocument();
  132. history.Action = HistoryAction.Update;
  133. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, cPDFAnnotation.Page.PageIndex, cPDFAnnotation);
  134. cPDFAnnotation.SetContent(NoteTextBox.Text);
  135. pdfViewerControl.UpdateAnnotFrame();
  136. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, cPDFAnnotation.Page.PageIndex, cPDFAnnotation);
  137. pdfViewerControl.GetCPDFViewer().UndoManager.AddHistory(history);
  138. }
  139. }
  140. }
  141. public void SetPresentAnnotAttrib(AnnotParam annotParam, CPDFAnnotation annotation, PDFViewControl cPDFViewer)
  142. {
  143. inkParam = (InkParam)annotParam;
  144. cPDFAnnotation = (CPDFInkAnnotation)annotation;
  145. pdfViewerControl = cPDFViewer;
  146. if (inkParam==null)
  147. {
  148. return;
  149. }
  150. ColorPickerControl.Brush = new SolidColorBrush(ParamConverter.ConverterByteForColor(inkParam.InkColor));
  151. CPDFOpacityControl.OpacityValue = (int)(inkParam.Transparency / 255D * 100);
  152. CPDFThicknessControl.Thickness = Convert.ToInt16(inkParam.Thickness);
  153. ColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(inkParam.InkColor));
  154. NoteTextBox.Text = inkParam.Content;
  155. }
  156. public CPDFFreehandData GetFreehandData()
  157. {
  158. CPDFFreehandData pdfFreehandData = new CPDFFreehandData();
  159. pdfFreehandData.AnnotationType = CPDFAnnotationType.Freehand;
  160. pdfFreehandData.BorderColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  161. pdfFreehandData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  162. pdfFreehandData.Thickness = CPDFThicknessControl.Thickness;
  163. pdfFreehandData.Note = NoteTextBox.Text;
  164. return pdfFreehandData;
  165. }
  166. public void SetEraseCheck(bool isCheck)
  167. {
  168. if(isCheck)
  169. {
  170. FreehandBtn.IsChecked = false;
  171. EraseBtn.IsChecked = true;
  172. FreehandPanel.Visibility = Visibility.Collapsed;
  173. ErasePanel.Visibility = Visibility.Visible;
  174. CPDFAnnotationPreviewerControl.Visibility = Visibility.Collapsed;
  175. EraseCirclePanel.Visibility = Visibility.Visible;
  176. }
  177. else
  178. {
  179. FreehandBtn.IsChecked = true;
  180. EraseBtn.IsChecked = false;
  181. FreehandPanel.Visibility = Visibility.Visible;
  182. ErasePanel.Visibility = Visibility.Collapsed;
  183. CPDFAnnotationPreviewerControl.Visibility = Visibility.Visible;
  184. EraseCirclePanel.Visibility=Visibility.Collapsed;
  185. }
  186. }
  187. internal void ClearAnnotAttribEvent()
  188. {
  189. cPDFAnnotation = null;
  190. }
  191. internal int GetEraseThickness()
  192. {
  193. return EraseThickness.Thickness;
  194. }
  195. private void FreehandBtn_Click(object sender, RoutedEventArgs e)
  196. {
  197. SetEraseCheck(false);
  198. EraseClickHandler?.Invoke(this, false);
  199. }
  200. private void EraseBtn_Click(object sender, RoutedEventArgs e)
  201. {
  202. SetEraseCheck(true);
  203. EraseClickHandler?.Invoke(this, true);
  204. }
  205. }
  206. }