CPDFFreeTextUI.xaml.cs 11 KB

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