CPDFFreeTextUI.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.Tool;
  3. using ComPDFKit.Controls.Common;
  4. using ComPDFKit.Controls.Data;
  5. using ComPDFKit.Controls.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.Controls.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. FreeTextAnnotHistory history = new FreeTextAnnotHistory();
  76. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  77. history.Action = HistoryAction.Update;
  78. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  79. switch(CPDFFontControl.TextAlignment)
  80. {
  81. case TextAlignment.Left:
  82. textAnnot.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
  83. break;
  84. case TextAlignment.Center:
  85. textAnnot.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_CENTER);
  86. break;
  87. case TextAlignment.Right:
  88. textAnnot.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT);
  89. break;
  90. default:
  91. break;
  92. }
  93. textAnnot.UpdateAp();
  94. if (viewControl != null && viewControl.PDFViewTool != null)
  95. {
  96. viewControl.UpdateAnnotFrame();
  97. }
  98. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  99. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  100. }
  101. }
  102. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  103. }
  104. private void CPDFFontControl_FontStyleChanged(object sender, EventArgs e)
  105. {
  106. if (textAnnot == null)
  107. {
  108. PropertyChanged?.Invoke(this, GetFreeTextData());
  109. }
  110. else
  111. {
  112. if (textAnnot != null && textAnnot.IsValid())
  113. {
  114. FreeTextAnnotHistory history = new FreeTextAnnotHistory();
  115. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  116. history.Action = HistoryAction.Update;
  117. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  118. CTextAttribute textAttr = textAnnot.FreeTextDa;
  119. string psName=String.Empty;
  120. CPDFFont.GetPostScriptName(CPDFFontControl.FontFamilyValue, CPDFFontControl.FontStyleValue, ref psName);
  121. textAttr.FontName = psName;
  122. textAnnot.SetFreetextDa(textAttr);
  123. textAnnot.UpdateAp();
  124. if (viewControl != null && viewControl.PDFViewTool != null)
  125. {
  126. viewControl.UpdateAnnotFrame();
  127. }
  128. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  129. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  130. }
  131. }
  132. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  133. }
  134. private void CPDFFontControl_FontFamilyChanged(object sender, EventArgs e)
  135. {
  136. if (textAnnot == null)
  137. {
  138. PropertyChanged?.Invoke(this, GetFreeTextData());
  139. }
  140. else
  141. {
  142. FreeTextAnnotHistory history = new FreeTextAnnotHistory();
  143. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  144. history.Action = HistoryAction.Update;
  145. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  146. CTextAttribute textAttr = textAnnot.FreeTextDa;
  147. bool isBold = IsBold(textAttr.FontName);
  148. bool isItalic = IsItalic(textAttr.FontName);
  149. string psName=String.Empty;
  150. CPDFFont.GetPostScriptName(CPDFFontControl.FontFamilyValue, CPDFFontControl.FontStyleValue, ref psName);
  151. textAttr.FontName = psName;
  152. textAnnot.SetFreetextDa(textAttr);
  153. textAnnot.UpdateAp();
  154. if (viewControl != null && viewControl.PDFViewTool != null)
  155. {
  156. viewControl.UpdateAnnotFrame() ;
  157. }
  158. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  159. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  160. }
  161. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  162. }
  163. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  164. {
  165. if (textAnnot == null)
  166. {
  167. PropertyChanged?.Invoke(this, GetFreeTextData());
  168. }
  169. else
  170. {
  171. if (viewControl != null && textParam.Transparency != CPDFOpacityControl.OpacityValue)
  172. {
  173. FreeTextAnnotHistory history = new FreeTextAnnotHistory();
  174. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  175. history.Action = HistoryAction.Update;
  176. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  177. textAnnot.SetTransparency((byte)(CPDFOpacityControl.OpacityValue / 100.0*255));
  178. textAnnot.UpdateAp();
  179. viewControl.UpdateAnnotFrame();
  180. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  181. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  182. }
  183. }
  184. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  185. }
  186. private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
  187. {
  188. if (textAnnot == null)
  189. {
  190. PropertyChanged?.Invoke(this, GetFreeTextData());
  191. }
  192. else
  193. {
  194. Color fontColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  195. CTextAttribute textAttr = new CTextAttribute
  196. {
  197. FontName = textAnnot.FreeTextDa.FontName,
  198. FontSize = textAnnot.FreeTextDa.FontSize,
  199. FontColor = new byte[3]
  200. {
  201. fontColor.R,
  202. fontColor.G,
  203. fontColor.B,
  204. }
  205. };
  206. if (viewControl != null && !textAnnot.FreeTextDa.FontColor.SequenceEqual(textAttr.FontColor))
  207. {
  208. FreeTextAnnotHistory history = new FreeTextAnnotHistory();
  209. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  210. history.Action = HistoryAction.Update;
  211. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  212. textAnnot.SetFreetextDa(textAttr);
  213. textAnnot.UpdateAp();
  214. viewControl.UpdateAnnotFrame() ;
  215. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  216. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  217. }
  218. }
  219. CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
  220. }
  221. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  222. {
  223. if (textAnnot == null)
  224. {
  225. PropertyChanged?.Invoke(this, GetFreeTextData());
  226. }
  227. else
  228. {
  229. if (viewControl != null && textParam.Content != NoteTextBox.Text)
  230. {
  231. FreeTextAnnotHistory history = new FreeTextAnnotHistory();
  232. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  233. history.Action = HistoryAction.Update;
  234. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  235. viewControl.UpdateAnnotFrame();
  236. textAnnot.SetContent(NoteTextBox.Text);
  237. textAnnot.UpdateAp();
  238. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
  239. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  240. }
  241. }
  242. }
  243. public void SetPresentAnnotAttrib(FreeTextParam textParam,CPDFFreeTextAnnotation annot,PDFViewControl view)
  244. {
  245. this.textAnnot = null;
  246. if(textParam!=null)
  247. {
  248. ColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
  249. textParam.FontColor[0],
  250. textParam.FontColor[1],
  251. textParam.FontColor[2]));
  252. CPDFOpacityControl.OpacityValue = (int)(textParam.Transparency / 255D * 100);
  253. CPDFFontControl.PostScriptName = textParam.FontName;
  254. CPDFFontControl.FontSizeValue = (int)textParam.FontSize;
  255. switch(textParam.Alignment)
  256. {
  257. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  258. CPDFFontControl.TextAlignment =TextAlignment.Left;
  259. break;
  260. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  261. CPDFFontControl.TextAlignment=TextAlignment.Center;
  262. break;
  263. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  264. CPDFFontControl.TextAlignment = TextAlignment.Right;
  265. break;
  266. default:
  267. break;
  268. }
  269. NoteTextBox.Text = textParam.Content;
  270. ColorPickerControl.SetCheckedForColor(Color.FromRgb(
  271. textParam.FontColor[0],
  272. textParam.FontColor[1],
  273. textParam.FontColor[2]));
  274. }
  275. this.textAnnot = annot;
  276. this.textParam = textParam;
  277. this.viewControl = view;
  278. }
  279. public CPDFFreeTextData GetFreeTextData()
  280. {
  281. CPDFFreeTextData pdfFreeTextData = new CPDFFreeTextData();
  282. pdfFreeTextData.AnnotationType = CPDFAnnotationType.FreeText;
  283. pdfFreeTextData.BorderColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  284. pdfFreeTextData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  285. pdfFreeTextData.FontFamily = CPDFFontControl.PostScriptName;
  286. pdfFreeTextData.FontSize = CPDFFontControl.FontSizeValue;
  287. pdfFreeTextData.TextAlignment = CPDFFontControl.TextAlignment;
  288. pdfFreeTextData.Note = NoteTextBox.Text;
  289. return pdfFreeTextData;
  290. }
  291. }
  292. }