PDFTextEditControl.xaml.cs 6.9 KB

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