CPDFFreeTextUI.xaml.cs 10 KB

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