PDFTextEditControl.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using ComPDFKit.PDFPage;
  2. using ComPDFKit.PDFPage.Edit;
  3. using ComPDFKitViewer;
  4. using ComPDFKitViewer.PdfViewer;
  5. using System;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Controls.Primitives;
  9. using System.Windows.Media;
  10. namespace Compdfkit_Tools.Edit
  11. {
  12. public partial class PDFTextEditControl : UserControl
  13. {
  14. public CPDFViewer PDFView { get;private set; }
  15. public PDFEditEvent EditEvent { get; set; }
  16. public PDFTextEditControl()
  17. {
  18. InitializeComponent();
  19. Loaded += PDFTextEditControl_Loaded;
  20. }
  21. public void InitWithPDFViewer(CPDFViewer newPDFView)
  22. {
  23. PDFView = newPDFView;
  24. }
  25. public void SetPDFTextEditData(PDFEditEvent newEvent)
  26. {
  27. EditEvent = null;
  28. if (newEvent!=null && newEvent.EditType==CPDFEditType.EditText)
  29. {
  30. if (newEvent.SystemFontNameList.Contains(newEvent.FontName) == false && string.IsNullOrEmpty(newEvent.FontName)==false)
  31. {
  32. newEvent.SystemFontNameList.Add(newEvent.FontName);
  33. }
  34. TextStyleUI.SetFontNames(newEvent.SystemFontNameList);
  35. TextStyleUI.SelectFontName(newEvent.FontName);
  36. TextStyleUI.SetFontStyle(newEvent.IsBold,newEvent.IsItalic);
  37. TextStyleUI.SetFontSize(newEvent.FontSize);
  38. OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Ceiling(newEvent.Transparency * 100/255D)));
  39. TextAlignUI.SetFontAlign(newEvent.TextAlign);
  40. FontColorUI.SetCheckedForColor(newEvent.FontColor);
  41. }
  42. EditEvent = newEvent;
  43. }
  44. private void Slider_DragCompleted(object sender, DragCompletedEventArgs e)
  45. {
  46. Slider slider = sender as Slider;
  47. if (slider != null)
  48. {
  49. slider.Tag = "true";
  50. }
  51. if (EditEvent != null)
  52. {
  53. EditEvent.FontSize = slider.Value;
  54. EditEvent.UpdatePDFEditByEventArgs();
  55. }
  56. }
  57. private void SliderOpacity_DragCompleted(object sender, DragCompletedEventArgs e)
  58. {
  59. Slider slider = sender as Slider;
  60. if (slider != null)
  61. {
  62. slider.Tag = "true";
  63. }
  64. if (EditEvent != null)
  65. {
  66. EditEvent.Transparency = (int)(FontOpacitySlider.Value * 255);
  67. EditEvent.UpdatePDFEditByEventArgs();
  68. }
  69. }
  70. private void SliderOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  71. {
  72. Slider slider = sender as Slider;
  73. if(OpacityTextBox!=null && FontOpacitySlider!=null)
  74. {
  75. OpacityTextBox.Text = string.Format("{0}%", (int)(FontOpacitySlider.Value * 100));
  76. }
  77. if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
  78. {
  79. return;
  80. }
  81. if (EditEvent != null)
  82. {
  83. EditEvent.Transparency = (int)(FontOpacitySlider.Value * 255);
  84. EditEvent.UpdatePDFEditByEventArgs();
  85. }
  86. }
  87. private void Slider_DragStarted(object sender, DragStartedEventArgs e)
  88. {
  89. Slider slider = sender as Slider;
  90. if (slider != null)
  91. {
  92. slider.Tag = "false";
  93. }
  94. }
  95. private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  96. {
  97. Slider slider = sender as Slider;
  98. if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
  99. {
  100. return;
  101. }
  102. if (EditEvent != null)
  103. {
  104. EditEvent.FontSize = slider.Value;
  105. EditEvent.UpdatePDFEditByEventArgs();
  106. }
  107. }
  108. private void PDFTextEditControl_Loaded(object sender, RoutedEventArgs e)
  109. {
  110. TextStyleUI.TextFontChanged += TextStyleUI_TextFontChanged;
  111. TextStyleUI.TextBoldChanged += TextStyleUI_TextBoldChanged;
  112. TextStyleUI.TextItalicChanged += TextStyleUI_TextItalicChanged;
  113. TextStyleUI.TextSizeChanged += TextStyleUI_TextSizeChanged;
  114. TextAlignUI.TextAlignChanged += TextAlignUI_TextAlignChanged;
  115. FontColorUI.ColorChanged += FontColorUI_ColorChanged;
  116. }
  117. private void TextStyleUI_TextSizeChanged(object sender, double e)
  118. {
  119. if (EditEvent != null)
  120. {
  121. EditEvent.FontSize = e;
  122. EditEvent.UpdatePDFEditByEventArgs();
  123. }
  124. }
  125. private void FontColorUI_ColorChanged(object sender, EventArgs e)
  126. {
  127. if (EditEvent != null)
  128. {
  129. SolidColorBrush newBrush = FontColorUI.Brush as SolidColorBrush;
  130. if (newBrush != null)
  131. {
  132. EditEvent.FontColor = newBrush.Color;
  133. EditEvent.UpdatePDFEditByEventArgs();
  134. }
  135. }
  136. }
  137. private void TextAlignUI_TextAlignChanged(object sender,TextAlignType e)
  138. {
  139. if (EditEvent != null)
  140. {
  141. EditEvent.TextAlign = e;
  142. EditEvent.UpdatePDFEditByEventArgs();
  143. }
  144. }
  145. private void TextStyleUI_TextItalicChanged(object sender, bool e)
  146. {
  147. if(EditEvent!=null)
  148. {
  149. EditEvent.IsItalic = e;
  150. EditEvent.UpdatePDFEditByEventArgs();
  151. }
  152. }
  153. private void TextStyleUI_TextBoldChanged(object sender, bool e)
  154. {
  155. if (EditEvent != null)
  156. {
  157. EditEvent.IsBold = e;
  158. EditEvent.UpdatePDFEditByEventArgs();
  159. }
  160. }
  161. private void TextStyleUI_TextFontChanged(object sender, string e)
  162. {
  163. if (EditEvent != null)
  164. {
  165. EditEvent.FontName = e;
  166. EditEvent.UpdatePDFEditByEventArgs();
  167. }
  168. }
  169. private void OpacityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  170. {
  171. ComboBoxItem selectItem = OpacityComboBox.SelectedItem as ComboBoxItem;
  172. if (selectItem != null && selectItem.Content != null)
  173. {
  174. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double newOpacity))
  175. {
  176. OpacityTextBox.Text = selectItem.Content.ToString();
  177. FontOpacitySlider.Value = newOpacity/100.0;
  178. }
  179. }
  180. }
  181. }
  182. }