CPDFFreeTextUI.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. }
  26. private void CPDFFontControl_FontSizeChanged(object sender, EventArgs e)
  27. {
  28. if (annotAttribEvent == null)
  29. {
  30. PropertyChanged?.Invoke(this, GetFreeTextData());
  31. }
  32. else
  33. {
  34. annotAttribEvent.UpdateAttrib(AnnotAttrib.FontSize, CPDFFontControl.FontSizeValue);
  35. annotAttribEvent.UpdateAnnot();
  36. }
  37. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  38. }
  39. private void CPDFFontControl_FontAlignChanged(object sender, EventArgs e)
  40. {
  41. if (annotAttribEvent == null)
  42. {
  43. PropertyChanged?.Invoke(this, GetFreeTextData());
  44. }
  45. else
  46. {
  47. annotAttribEvent.UpdateAttrib(AnnotAttrib.TextAlign, CPDFFontControl.TextAlignment);
  48. annotAttribEvent.UpdateAnnot();
  49. }
  50. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  51. }
  52. private void CPDFFontControl_FontStyleChanged(object sender, EventArgs e)
  53. {
  54. if (annotAttribEvent == null)
  55. {
  56. PropertyChanged?.Invoke(this, GetFreeTextData());
  57. }
  58. else
  59. {
  60. annotAttribEvent.UpdateAttrib(AnnotAttrib.IsBold, CPDFFontControl.IsBold);
  61. annotAttribEvent.UpdateAttrib(AnnotAttrib.IsItalic, CPDFFontControl.IsItalic);
  62. annotAttribEvent.UpdateAnnot();
  63. }
  64. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  65. }
  66. private void CPDFFontControl_FontFamilyChanged(object sender, EventArgs e)
  67. {
  68. if (annotAttribEvent == null)
  69. {
  70. PropertyChanged?.Invoke(this, GetFreeTextData());
  71. }
  72. else
  73. {
  74. annotAttribEvent.UpdateAttrib(AnnotAttrib.FontName, CPDFFontControl.FontFamilyValue);
  75. annotAttribEvent.UpdateAnnot();
  76. }
  77. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  78. }
  79. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  80. {
  81. if (annotAttribEvent == null)
  82. {
  83. PropertyChanged?.Invoke(this, GetFreeTextData());
  84. }
  85. else
  86. {
  87. annotAttribEvent.UpdateAttrib(AnnotAttrib.Transparency, CPDFOpacityControl.OpacityValue / 100.0);
  88. annotAttribEvent.UpdateAnnot();
  89. }
  90. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  91. }
  92. private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
  93. {
  94. if (annotAttribEvent == null)
  95. {
  96. PropertyChanged?.Invoke(this, GetFreeTextData());
  97. }
  98. else
  99. {
  100. annotAttribEvent.UpdateAttrib(AnnotAttrib.FontColor, ((SolidColorBrush)ColorPickerControl.Brush).Color);
  101. annotAttribEvent.UpdateAnnot();
  102. }
  103. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  104. }
  105. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  106. {
  107. if (annotAttribEvent == null)
  108. {
  109. PropertyChanged?.Invoke(this, GetFreeTextData());
  110. }
  111. else
  112. {
  113. annotAttribEvent.UpdateAttrib(AnnotAttrib.NoteText, NoteTextBox.Text);
  114. annotAttribEvent.UpdateAnnot();
  115. }
  116. }
  117. public void SetPresentAnnotAttrib(AnnotAttribEvent annotAttribEvent)
  118. {
  119. this.annotAttribEvent = null;
  120. ColorPickerControl.Brush = new SolidColorBrush((Color)annotAttribEvent.Attribs[AnnotAttrib.FontColor]);
  121. CPDFOpacityControl.OpacityValue = (int)((double)annotAttribEvent.Attribs[AnnotAttrib.Transparency] * 100);
  122. CPDFFontControl.FontFamilyValue = (string)annotAttribEvent.Attribs[AnnotAttrib.FontName];
  123. CPDFFontControl.FontSizeValue = Convert.ToInt16(annotAttribEvent.Attribs[AnnotAttrib.FontSize]);
  124. CPDFFontControl.IsBold = (bool)annotAttribEvent.Attribs[AnnotAttrib.IsBold];
  125. CPDFFontControl.IsItalic = (bool)annotAttribEvent.Attribs[AnnotAttrib.IsItalic];
  126. CPDFFontControl.TextAlignment = (TextAlignment)annotAttribEvent.Attribs[AnnotAttrib.TextAlign];
  127. NoteTextBox.Text = (string)annotAttribEvent.Attribs[AnnotAttrib.NoteText];
  128. this.annotAttribEvent = annotAttribEvent;
  129. if (annotAttribEvent.Attribs != null && annotAttribEvent.Attribs.ContainsKey(AnnotAttrib.FontColor))
  130. {
  131. ColorPickerControl.SetCheckedForColor((Color)annotAttribEvent.Attribs[AnnotAttrib.FontColor]);
  132. }
  133. }
  134. public CPDFFreeTextData GetFreeTextData()
  135. {
  136. CPDFFreeTextData pdfFreeTextData = new CPDFFreeTextData();
  137. pdfFreeTextData.AnnotationType = CPDFAnnotationType.FreeText;
  138. pdfFreeTextData.BorderColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  139. pdfFreeTextData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  140. pdfFreeTextData.FontFamily = CPDFFontControl.FontFamilyValue;
  141. pdfFreeTextData.FontSize = CPDFFontControl.FontSizeValue;
  142. pdfFreeTextData.IsBold = CPDFFontControl.IsBold;
  143. pdfFreeTextData.IsItalic = CPDFFontControl.IsItalic;
  144. pdfFreeTextData.TextAlignment = CPDFFontControl.TextAlignment;
  145. pdfFreeTextData.Note = NoteTextBox.Text;
  146. return pdfFreeTextData;
  147. }
  148. }
  149. }