PDFTextEditControl.xaml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. EditEvent = null;
  39. if (newEvent!=null && newEvent.EditType==CPDFEditType.EditText)
  40. {
  41. if (newEvent.SystemFontNameList.Contains(newEvent.FontName) == false && string.IsNullOrEmpty(newEvent.FontName)==false)
  42. {
  43. newEvent.SystemFontNameList.Add(newEvent.FontName);
  44. }
  45. TextStyleUI.SetFontNames(newEvent.SystemFontNameList);
  46. TextStyleUI.SelectFontName(newEvent.FontName);
  47. TextStyleUI.SetFontStyle(newEvent.IsBold,newEvent.IsItalic);
  48. TextStyleUI.SetFontSize(newEvent.FontSize);
  49. OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Ceiling(newEvent.Transparency * 100/255D)));
  50. TextAlignUI.SetFontAlign(newEvent.TextAlign);
  51. FontColorUI.SetCheckedForColor(newEvent.FontColor);
  52. }
  53. EditEvent = newEvent;
  54. }
  55. private void Slider_DragCompleted(object sender, DragCompletedEventArgs e)
  56. {
  57. Slider slider = sender as Slider;
  58. if (slider != null)
  59. {
  60. slider.Tag = "true";
  61. }
  62. if (EditEvent != null)
  63. {
  64. EditEvent.FontSize = slider.Value;
  65. EditEvent.UpdatePDFEditByEventArgs();
  66. }
  67. }
  68. private void SliderOpacity_DragCompleted(object sender, DragCompletedEventArgs e)
  69. {
  70. Slider slider = sender as Slider;
  71. if (slider != null)
  72. {
  73. slider.Tag = "true";
  74. }
  75. if (EditEvent != null)
  76. {
  77. EditEvent.Transparency = (int)(FontOpacitySlider.Value * 255);
  78. EditEvent.UpdatePDFEditByEventArgs();
  79. }
  80. }
  81. private void SliderOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  82. {
  83. Slider slider = sender as Slider;
  84. if(OpacityTextBox!=null && FontOpacitySlider!=null)
  85. {
  86. OpacityTextBox.Text = string.Format("{0}%", (int)(FontOpacitySlider.Value * 100));
  87. }
  88. if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
  89. {
  90. return;
  91. }
  92. if (EditEvent != null)
  93. {
  94. EditEvent.Transparency = (int)(FontOpacitySlider.Value * 255);
  95. EditEvent.UpdatePDFEditByEventArgs();
  96. }
  97. }
  98. private void Slider_DragStarted(object sender, DragStartedEventArgs e)
  99. {
  100. Slider slider = sender as Slider;
  101. if (slider != null)
  102. {
  103. slider.Tag = "false";
  104. }
  105. }
  106. private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  107. {
  108. Slider slider = sender as Slider;
  109. if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
  110. {
  111. return;
  112. }
  113. if (EditEvent != null)
  114. {
  115. EditEvent.FontSize = slider.Value;
  116. EditEvent.UpdatePDFEditByEventArgs();
  117. }
  118. }
  119. private void PDFTextEditControl_Loaded(object sender, RoutedEventArgs e)
  120. {
  121. TextStyleUI.TextFontChanged += TextStyleUI_TextFontChanged;
  122. TextStyleUI.TextBoldChanged += TextStyleUI_TextBoldChanged;
  123. TextStyleUI.TextItalicChanged += TextStyleUI_TextItalicChanged;
  124. TextStyleUI.TextSizeChanged += TextStyleUI_TextSizeChanged;
  125. TextAlignUI.TextAlignChanged += TextAlignUI_TextAlignChanged;
  126. FontColorUI.ColorChanged += FontColorUI_ColorChanged;
  127. }
  128. private void TextStyleUI_TextSizeChanged(object sender, double e)
  129. {
  130. if (EditEvent != null)
  131. {
  132. EditEvent.FontSize = e;
  133. EditEvent.UpdatePDFEditByEventArgs();
  134. }
  135. }
  136. private void FontColorUI_ColorChanged(object sender, EventArgs e)
  137. {
  138. if (EditEvent != null)
  139. {
  140. SolidColorBrush newBrush = FontColorUI.Brush as SolidColorBrush;
  141. if (newBrush != null)
  142. {
  143. EditEvent.FontColor = newBrush.Color;
  144. EditEvent.UpdatePDFEditByEventArgs();
  145. }
  146. }
  147. }
  148. private void TextAlignUI_TextAlignChanged(object sender,TextAlignType e)
  149. {
  150. if (EditEvent != null)
  151. {
  152. EditEvent.TextAlign = e;
  153. EditEvent.UpdatePDFEditByEventArgs();
  154. }
  155. }
  156. private void TextStyleUI_TextItalicChanged(object sender, bool e)
  157. {
  158. if(EditEvent!=null)
  159. {
  160. EditEvent.IsItalic = e;
  161. EditEvent.UpdatePDFEditByEventArgs();
  162. }
  163. }
  164. private void TextStyleUI_TextBoldChanged(object sender, bool e)
  165. {
  166. if (EditEvent != null)
  167. {
  168. EditEvent.IsBold = e;
  169. EditEvent.UpdatePDFEditByEventArgs();
  170. }
  171. }
  172. private void TextStyleUI_TextFontChanged(object sender, string e)
  173. {
  174. if (EditEvent != null)
  175. {
  176. EditEvent.FontName = e;
  177. EditEvent.UpdatePDFEditByEventArgs();
  178. }
  179. }
  180. private void OpacityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  181. {
  182. ComboBoxItem selectItem = OpacityComboBox.SelectedItem as ComboBoxItem;
  183. if (selectItem != null && selectItem.Content != null)
  184. {
  185. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double newOpacity))
  186. {
  187. OpacityTextBox.Text = selectItem.Content.ToString();
  188. FontOpacitySlider.Value = newOpacity/100.0;
  189. }
  190. }
  191. }
  192. }
  193. }