CPDFFreeTextUI.xaml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using compdfkit_tools.Common;
  2. using compdfkit_tools.Data;
  3. using ComPDFKitViewer;
  4. using ComPDFKitViewer.AnnotEvent;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace compdfkit_tools.PDFControlUI
  20. {
  21. /// <summary>
  22. /// CPDFFreeTextUI.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class CPDFFreeTextUI : UserControl
  25. {
  26. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  27. private AnnotAttribEvent annotAttribEvent;
  28. public CPDFFreeTextUI()
  29. {
  30. InitializeComponent();
  31. ColorPickerControl.ColorChanged += ColorPickerControl_ColorChanged;
  32. CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
  33. CPDFFontControl.FontFamilyChanged += CPDFFontControl_FontFamilyChanged;
  34. CPDFFontControl.FontStyleChanged += CPDFFontControl_FontStyleChanged;
  35. CPDFFontControl.FontAlignChanged += CPDFFontControl_FontAlignChanged;
  36. CPDFFontControl.FontSizeChanged += CPDFFontControl_FontSizeChanged;
  37. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  38. }
  39. private void CPDFFontControl_FontSizeChanged(object sender, EventArgs e)
  40. {
  41. if (annotAttribEvent == null)
  42. {
  43. PropertyChanged?.Invoke(this, GetFreeTextData());
  44. }
  45. else
  46. {
  47. annotAttribEvent.UpdateAttrib(AnnotAttrib.FontSize, CPDFFontControl.FontSizeValue);
  48. annotAttribEvent.UpdateAnnot();
  49. }
  50. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  51. }
  52. private void CPDFFontControl_FontAlignChanged(object sender, EventArgs e)
  53. {
  54. if (annotAttribEvent == null)
  55. {
  56. PropertyChanged?.Invoke(this, GetFreeTextData());
  57. }
  58. else
  59. {
  60. annotAttribEvent.UpdateAttrib(AnnotAttrib.TextAlign, CPDFFontControl.TextAlignment);
  61. annotAttribEvent.UpdateAnnot();
  62. }
  63. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  64. }
  65. private void CPDFFontControl_FontStyleChanged(object sender, EventArgs e)
  66. {
  67. if (annotAttribEvent == null)
  68. {
  69. PropertyChanged?.Invoke(this, GetFreeTextData());
  70. }
  71. else
  72. {
  73. annotAttribEvent.UpdateAttrib(AnnotAttrib.IsBold, CPDFFontControl.IsBold);
  74. annotAttribEvent.UpdateAttrib(AnnotAttrib.IsItalic, CPDFFontControl.IsItalic);
  75. annotAttribEvent.UpdateAnnot();
  76. }
  77. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  78. }
  79. private void CPDFFontControl_FontFamilyChanged(object sender, EventArgs e)
  80. {
  81. if (annotAttribEvent == null)
  82. {
  83. PropertyChanged?.Invoke(this, GetFreeTextData());
  84. }
  85. else
  86. {
  87. annotAttribEvent.UpdateAttrib(AnnotAttrib.FontName, CPDFFontControl.FontFamilyValue);
  88. annotAttribEvent.UpdateAnnot();
  89. }
  90. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  91. }
  92. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  93. {
  94. if (annotAttribEvent == null)
  95. {
  96. PropertyChanged?.Invoke(this, GetFreeTextData());
  97. }
  98. else
  99. {
  100. annotAttribEvent.UpdateAttrib(AnnotAttrib.Transparency, CPDFOpacityControl.OpacityValue / 100.0);
  101. annotAttribEvent.UpdateAnnot();
  102. }
  103. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  104. }
  105. private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
  106. {
  107. if (annotAttribEvent == null)
  108. {
  109. PropertyChanged?.Invoke(this, GetFreeTextData());
  110. }
  111. else
  112. {
  113. annotAttribEvent.UpdateAttrib(AnnotAttrib.FontColor, ((SolidColorBrush)ColorPickerControl.Brush).Color);
  114. annotAttribEvent.UpdateAnnot();
  115. }
  116. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  117. }
  118. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  119. {
  120. if (annotAttribEvent == null)
  121. {
  122. PropertyChanged?.Invoke(this, GetFreeTextData());
  123. }
  124. else
  125. {
  126. annotAttribEvent.UpdateAttrib(AnnotAttrib.NoteText, NoteTextBox.Text);
  127. annotAttribEvent.UpdateAnnot();
  128. }
  129. }
  130. public void SetPresentAnnotAttrib(AnnotAttribEvent annotAttribEvent)
  131. {
  132. this.annotAttribEvent = null;
  133. ColorPickerControl.Brush = new SolidColorBrush((Color)annotAttribEvent.Attribs[AnnotAttrib.FontColor]);
  134. CPDFOpacityControl.OpacityValue = (int)((double)annotAttribEvent.Attribs[AnnotAttrib.Transparency] * 100);
  135. CPDFFontControl.FontFamilyValue = (string)annotAttribEvent.Attribs[AnnotAttrib.FontName];
  136. CPDFFontControl.FontSizeValue = Convert.ToInt16(annotAttribEvent.Attribs[AnnotAttrib.FontSize]);
  137. CPDFFontControl.IsBold = (bool)annotAttribEvent.Attribs[AnnotAttrib.IsBold];
  138. CPDFFontControl.IsItalic = (bool)annotAttribEvent.Attribs[AnnotAttrib.IsItalic];
  139. CPDFFontControl.TextAlignment = (TextAlignment)annotAttribEvent.Attribs[AnnotAttrib.TextAlign];
  140. NoteTextBox.Text = (string)annotAttribEvent.Attribs[AnnotAttrib.NoteText];
  141. this.annotAttribEvent = annotAttribEvent;
  142. if (annotAttribEvent.Attribs != null && annotAttribEvent.Attribs.ContainsKey(AnnotAttrib.FontColor))
  143. {
  144. ColorPickerControl.SetCheckedForColor((Color)annotAttribEvent.Attribs[AnnotAttrib.FontColor]);
  145. }
  146. }
  147. public CPDFFreeTextData GetFreeTextData()
  148. {
  149. CPDFFreeTextData pdfFreeTextData = new CPDFFreeTextData();
  150. pdfFreeTextData.AnnotationType = CPDFAnnotationType.FreeText;
  151. pdfFreeTextData.BorderColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  152. pdfFreeTextData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  153. pdfFreeTextData.FontFamily = CPDFFontControl.FontFamilyValue;
  154. pdfFreeTextData.FontSize = CPDFFontControl.FontSizeValue;
  155. pdfFreeTextData.IsBold = CPDFFontControl.IsBold;
  156. pdfFreeTextData.IsItalic = CPDFFontControl.IsItalic;
  157. pdfFreeTextData.TextAlignment = CPDFFontControl.TextAlignment;
  158. pdfFreeTextData.Note = NoteTextBox.Text;
  159. return pdfFreeTextData;
  160. }
  161. }
  162. }