CPDFFreeTextUI.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.Tool;
  3. using Compdfkit_Tools.Common;
  4. using Compdfkit_Tools.Data;
  5. using Compdfkit_Tools.PDFControl;
  6. using ComPDFKitViewer;
  7. using System;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Forms;
  11. using System.Windows.Media;
  12. using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  13. namespace Compdfkit_Tools.PDFControlUI
  14. {
  15. public partial class CPDFFreeTextUI : System.Windows.Controls.UserControl
  16. {
  17. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  18. private CPDFFreeTextAnnotation textAnnot;
  19. private FreeTextParam textParam;
  20. private PDFViewControl viewControl;
  21. public CPDFFreeTextUI()
  22. {
  23. InitializeComponent();
  24. ColorPickerControl.ColorChanged -= ColorPickerControl_ColorChanged;
  25. CPDFOpacityControl.OpacityChanged -= CPDFOpacityControl_OpacityChanged;
  26. CPDFFontControl.FontFamilyChanged -= CPDFFontControl_FontFamilyChanged;
  27. CPDFFontControl.FontStyleChanged -= CPDFFontControl_FontStyleChanged;
  28. CPDFFontControl.FontAlignChanged -= CPDFFontControl_FontAlignChanged;
  29. CPDFFontControl.FontSizeChanged -= CPDFFontControl_FontSizeChanged;
  30. ColorPickerControl.ColorChanged += ColorPickerControl_ColorChanged;
  31. CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
  32. CPDFFontControl.FontFamilyChanged += CPDFFontControl_FontFamilyChanged;
  33. CPDFFontControl.FontStyleChanged += CPDFFontControl_FontStyleChanged;
  34. CPDFFontControl.FontAlignChanged += CPDFFontControl_FontAlignChanged;
  35. CPDFFontControl.FontSizeChanged += CPDFFontControl_FontSizeChanged;
  36. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  37. }
  38. private void CPDFFontControl_FontSizeChanged(object sender, EventArgs e)
  39. {
  40. if (textAnnot == null)
  41. {
  42. PropertyChanged?.Invoke(this, GetFreeTextData());
  43. }
  44. else
  45. {
  46. if(textAnnot!=null &&textAnnot.IsValid())
  47. {
  48. CTextAttribute textAttr = textAnnot.FreeTextDa;
  49. textAttr.FontSize = CPDFFontControl.FontSizeValue;
  50. textAnnot.SetFreetextDa(textAttr);
  51. textAnnot.UpdateAp();
  52. if (viewControl != null && viewControl.PDFViewTool != null)
  53. {
  54. viewControl.UpdateAnnotFrame();
  55. }
  56. }
  57. }
  58. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  59. }
  60. private void CPDFFontControl_FontAlignChanged(object sender, EventArgs e)
  61. {
  62. if (textAnnot == null)
  63. {
  64. PropertyChanged?.Invoke(this, GetFreeTextData());
  65. }
  66. else
  67. {
  68. if (textAnnot != null && textAnnot.IsValid())
  69. {
  70. switch(CPDFFontControl.TextAlignment)
  71. {
  72. case TextAlignment.Left:
  73. textAnnot.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
  74. break;
  75. case TextAlignment.Center:
  76. textAnnot.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_CENTER);
  77. break;
  78. case TextAlignment.Right:
  79. textAnnot.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT);
  80. break;
  81. default:
  82. break;
  83. }
  84. textAnnot.UpdateAp();
  85. if (viewControl != null && viewControl.PDFViewTool != null)
  86. {
  87. viewControl.UpdateAnnotFrame();
  88. }
  89. }
  90. }
  91. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  92. }
  93. private void CPDFFontControl_FontStyleChanged(object sender, EventArgs e)
  94. {
  95. if (textAnnot == null)
  96. {
  97. PropertyChanged?.Invoke(this, GetFreeTextData());
  98. }
  99. else
  100. {
  101. if (textAnnot != null && textAnnot.IsValid())
  102. {
  103. CTextAttribute textAttr = textAnnot.FreeTextDa;
  104. FontType fontType = GetFontType(textAttr.FontName);
  105. textAttr.FontName = ObtainFontName(fontType, CPDFFontControl.IsBold, CPDFFontControl.IsItalic);
  106. textAnnot.SetFreetextDa(textAttr);
  107. textAnnot.UpdateAp();
  108. if (viewControl != null && viewControl.PDFViewTool != null)
  109. {
  110. viewControl.UpdateAnnotFrame();
  111. }
  112. }
  113. }
  114. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  115. }
  116. private void CPDFFontControl_FontFamilyChanged(object sender, EventArgs e)
  117. {
  118. if (textAnnot == null)
  119. {
  120. PropertyChanged?.Invoke(this, GetFreeTextData());
  121. }
  122. else
  123. {
  124. CTextAttribute textAttr = textAnnot.FreeTextDa;
  125. bool isBold = IsBold(textAttr.FontName);
  126. bool isItalic = IsItalic(textAttr.FontName);
  127. FontType fontType = GetFontType(CPDFFontControl.FontFamilyValue);
  128. textAttr.FontName = ObtainFontName(fontType, isBold, isItalic);
  129. textAnnot.SetFreetextDa(textAttr);
  130. textAnnot.UpdateAp();
  131. if (viewControl != null && viewControl.PDFViewTool != null)
  132. {
  133. viewControl.UpdateAnnotFrame() ;
  134. }
  135. }
  136. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  137. }
  138. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  139. {
  140. if (textAnnot == null)
  141. {
  142. PropertyChanged?.Invoke(this, GetFreeTextData());
  143. }
  144. else
  145. {
  146. textAnnot.SetTransparency((byte)(CPDFOpacityControl.OpacityValue / 100.0*255));
  147. textAnnot.UpdateAp();
  148. if (viewControl != null && viewControl.PDFViewTool != null)
  149. {
  150. viewControl.UpdateAnnotFrame();
  151. }
  152. }
  153. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  154. }
  155. private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
  156. {
  157. if (textAnnot == null)
  158. {
  159. PropertyChanged?.Invoke(this, GetFreeTextData());
  160. }
  161. else
  162. {
  163. Color fontColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  164. CTextAttribute textAttr = textAnnot.FreeTextDa;
  165. textAttr.FontColor = new byte[3]
  166. {
  167. fontColor.R,
  168. fontColor.G,
  169. fontColor.B,
  170. };
  171. textAnnot.SetFreetextDa(textAttr);
  172. textAnnot.UpdateAp();
  173. if (viewControl != null && viewControl.PDFViewTool != null)
  174. {
  175. viewControl.UpdateAnnotFrame() ;
  176. }
  177. }
  178. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  179. }
  180. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  181. {
  182. if (textAnnot == null)
  183. {
  184. PropertyChanged?.Invoke(this, GetFreeTextData());
  185. }
  186. else
  187. {
  188. textAnnot.SetContent(NoteTextBox.Text);
  189. textAnnot.UpdateAp();
  190. if (viewControl != null && viewControl.PDFViewTool != null)
  191. {
  192. viewControl.UpdateAnnotFrame();
  193. }
  194. }
  195. }
  196. public void SetPresentAnnotAttrib(FreeTextParam textParam,CPDFFreeTextAnnotation annot,PDFViewControl view)
  197. {
  198. this.textAnnot = null;
  199. if(textParam!=null)
  200. {
  201. ColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
  202. textParam.FontColor[0],
  203. textParam.FontColor[1],
  204. textParam.FontColor[2]));
  205. CPDFOpacityControl.OpacityValue = (int)(textParam.Transparency / 255D * 100);
  206. CPDFFontControl.FontFamilyValue = textParam.FontName;
  207. CPDFFontControl.FontSizeValue = (int)textParam.FontSize;
  208. CPDFFontControl.IsBold = textParam.IsBold;
  209. CPDFFontControl.IsItalic = textParam.IsItalic;
  210. switch(textParam.Alignment)
  211. {
  212. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  213. CPDFFontControl.TextAlignment =TextAlignment.Left;
  214. break;
  215. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  216. CPDFFontControl.TextAlignment=TextAlignment.Center;
  217. break;
  218. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  219. CPDFFontControl.TextAlignment = TextAlignment.Right;
  220. break;
  221. default:
  222. break;
  223. }
  224. NoteTextBox.Text = textParam.Content;
  225. ColorPickerControl.SetCheckedForColor(Color.FromRgb(
  226. textParam.FontColor[0],
  227. textParam.FontColor[1],
  228. textParam.FontColor[2]));
  229. }
  230. this.textAnnot = annot;
  231. this.textParam = textParam;
  232. this.viewControl = view;
  233. }
  234. public CPDFFreeTextData GetFreeTextData()
  235. {
  236. CPDFFreeTextData pdfFreeTextData = new CPDFFreeTextData();
  237. pdfFreeTextData.AnnotationType = CPDFAnnotationType.FreeText;
  238. pdfFreeTextData.BorderColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  239. pdfFreeTextData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  240. pdfFreeTextData.FontFamily = CPDFFontControl.FontFamilyValue;
  241. pdfFreeTextData.FontSize = CPDFFontControl.FontSizeValue;
  242. pdfFreeTextData.IsBold = CPDFFontControl.IsBold;
  243. pdfFreeTextData.IsItalic = CPDFFontControl.IsItalic;
  244. pdfFreeTextData.TextAlignment = CPDFFontControl.TextAlignment;
  245. pdfFreeTextData.Note = NoteTextBox.Text;
  246. return pdfFreeTextData;
  247. }
  248. }
  249. }