CPDFFreeTextUI.xaml.cs 6.8 KB

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