CPDFFreehandUI.xaml.cs 10 KB

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