CPDFFreeTextUI.xaml.cs 13 KB

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