CPDFFreeTextUI.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. using ComPDFKit.PDFDocument;
  18. namespace Compdfkit_Tools.PDFControlUI
  19. {
  20. public partial class CPDFFreeTextUI : System.Windows.Controls.UserControl
  21. {
  22. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  23. private CPDFFreeTextAnnotation textAnnot;
  24. private FreeTextParam textParam;
  25. private PDFViewControl viewControl;
  26. public CPDFFreeTextUI()
  27. {
  28. InitializeComponent();
  29. ColorPickerControl.ColorChanged -= ColorPickerControl_ColorChanged;
  30. CPDFOpacityControl.OpacityChanged -= CPDFOpacityControl_OpacityChanged;
  31. CPDFFontControl.FontFamilyChanged -= CPDFFontControl_FontFamilyChanged;
  32. CPDFFontControl.FontStyleChanged -= CPDFFontControl_FontStyleChanged;
  33. CPDFFontControl.FontAlignChanged -= CPDFFontControl_FontAlignChanged;
  34. CPDFFontControl.FontSizeChanged -= CPDFFontControl_FontSizeChanged;
  35. ColorPickerControl.ColorChanged += ColorPickerControl_ColorChanged;
  36. CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
  37. CPDFFontControl.FontFamilyChanged += CPDFFontControl_FontFamilyChanged;
  38. CPDFFontControl.FontStyleChanged += CPDFFontControl_FontStyleChanged;
  39. CPDFFontControl.FontAlignChanged += CPDFFontControl_FontAlignChanged;
  40. CPDFFontControl.FontSizeChanged += CPDFFontControl_FontSizeChanged;
  41. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  42. }
  43. private void CPDFFontControl_FontSizeChanged(object sender, EventArgs e)
  44. {
  45. if (textAnnot == null)
  46. {
  47. PropertyChanged?.Invoke(this, GetFreeTextData());
  48. }
  49. else
  50. {
  51. if(textAnnot!=null &&textAnnot.IsValid())
  52. {
  53. CTextAttribute textAttr = textAnnot.FreeTextDa;
  54. textAttr.FontSize = CPDFFontControl.FontSizeValue;
  55. textAnnot.SetFreetextDa(textAttr);
  56. textAnnot.UpdateAp();
  57. if (viewControl != null && viewControl.PDFViewTool != null)
  58. {
  59. viewControl.UpdateAnnotFrame();
  60. }
  61. }
  62. }
  63. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  64. }
  65. private void CPDFFontControl_FontAlignChanged(object sender, EventArgs e)
  66. {
  67. if (textAnnot == null)
  68. {
  69. PropertyChanged?.Invoke(this, GetFreeTextData());
  70. }
  71. else
  72. {
  73. if (textAnnot != null && textAnnot.IsValid())
  74. {
  75. switch(CPDFFontControl.TextAlignment)
  76. {
  77. case TextAlignment.Left:
  78. textAnnot.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
  79. break;
  80. case TextAlignment.Center:
  81. textAnnot.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_CENTER);
  82. break;
  83. case TextAlignment.Right:
  84. textAnnot.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT);
  85. break;
  86. default:
  87. break;
  88. }
  89. textAnnot.UpdateAp();
  90. if (viewControl != null && viewControl.PDFViewTool != null)
  91. {
  92. viewControl.UpdateAnnotFrame();
  93. }
  94. }
  95. }
  96. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  97. }
  98. private void CPDFFontControl_FontStyleChanged(object sender, EventArgs e)
  99. {
  100. if (textAnnot == null)
  101. {
  102. PropertyChanged?.Invoke(this, GetFreeTextData());
  103. }
  104. else
  105. {
  106. if (textAnnot != null && textAnnot.IsValid())
  107. {
  108. CTextAttribute textAttr = textAnnot.FreeTextDa;
  109. string postScriptName = string.Empty;
  110. textAttr.FontName = CPDFFontControl.PostScriptName;
  111. textAnnot.SetFreetextDa(textAttr);
  112. textAnnot.UpdateAp();
  113. if (viewControl != null && viewControl.PDFViewTool != null)
  114. {
  115. viewControl.UpdateAnnotFrame();
  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. viewControl.UpdateAnnotFrame() ;
  139. }
  140. }
  141. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  142. }
  143. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  144. {
  145. if (textAnnot == null)
  146. {
  147. PropertyChanged?.Invoke(this, GetFreeTextData());
  148. }
  149. else
  150. {
  151. if (viewControl != null && textParam.Transparency != CPDFOpacityControl.OpacityValue)
  152. {
  153. FreeTextAnnotHistory history = new FreeTextAnnotHistory();
  154. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  155. history.Action = HistoryAction.Update;
  156. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  157. textAnnot.SetTransparency((byte)(CPDFOpacityControl.OpacityValue / 100.0*255));
  158. textAnnot.UpdateAp();
  159. viewControl.UpdateAnnotFrame();
  160. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  161. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  162. }
  163. }
  164. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  165. }
  166. private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
  167. {
  168. if (textAnnot == null)
  169. {
  170. PropertyChanged?.Invoke(this, GetFreeTextData());
  171. }
  172. else
  173. {
  174. Color fontColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  175. CTextAttribute textAttr = textAnnot.FreeTextDa;
  176. textAttr.FontColor = new byte[3]
  177. {
  178. fontColor.R,
  179. fontColor.G,
  180. fontColor.B,
  181. };
  182. if (viewControl != null && !textAnnot.FreeTextDa.FontColor.SequenceEqual(textAttr.FontColor))
  183. {
  184. FreeTextAnnotHistory history = new FreeTextAnnotHistory();
  185. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  186. history.Action = HistoryAction.Update;
  187. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  188. textAnnot.SetFreetextDa(textAttr);
  189. textAnnot.UpdateAp();
  190. viewControl.UpdateAnnotFrame() ;
  191. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  192. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  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. if (viewControl != null && textParam.Content != NoteTextBox.Text)
  206. {
  207. FreeTextAnnotHistory history = new FreeTextAnnotHistory();
  208. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  209. history.Action = HistoryAction.Update;
  210. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  211. viewControl.UpdateAnnotFrame();
  212. textAnnot.SetContent(NoteTextBox.Text);
  213. textAnnot.UpdateAp();
  214. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  215. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  216. }
  217. }
  218. }
  219. public void SetPresentAnnotAttrib(FreeTextParam textParam,CPDFFreeTextAnnotation annot,PDFViewControl view)
  220. {
  221. this.textAnnot = null;
  222. if(textParam!=null)
  223. {
  224. ColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
  225. textParam.FontColor[0],
  226. textParam.FontColor[1],
  227. textParam.FontColor[2]));
  228. CPDFOpacityControl.OpacityValue = (int)(textParam.Transparency / 255D * 100);
  229. CPDFFontControl.PostScriptName = textParam.FontName;
  230. CPDFFontControl.FontSizeValue = (int)textParam.FontSize;
  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.PostScriptName;
  262. pdfFreeTextData.FontSize = CPDFFontControl.FontSizeValue;
  263. pdfFreeTextData.TextAlignment = CPDFFontControl.TextAlignment;
  264. pdfFreeTextData.Note = NoteTextBox.Text;
  265. return pdfFreeTextData;
  266. }
  267. }
  268. }