TextFieldPropertyViewModel.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Office.Helper;
  6. using PDF_Office.Model;
  7. using PDF_Office.Model.From;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Media;
  18. namespace PDF_Office.ViewModels.Form
  19. {
  20. public class TextFieldPropertyViewModel : FormBaseVM, INavigationAware
  21. {
  22. #region 属性
  23. #region 一般
  24. private FormFieldType _formPos;
  25. public FormFieldType FormPos
  26. {
  27. get { return _formPos; }
  28. set { SetProperty(ref _formPos, value); ChangeFieldValue("FormPos"); }
  29. }
  30. #endregion
  31. #region 选项
  32. //默认值
  33. private string _defaultValue;
  34. public string DefaultValue
  35. {
  36. get { return _defaultValue; }
  37. set { SetProperty(ref _defaultValue, value); }
  38. }
  39. //多行
  40. private bool _isMultiline = false;
  41. public bool IsMultiLine
  42. {
  43. get { return _isMultiline; }
  44. set { SetProperty(ref _isMultiline, value); }
  45. }
  46. //滚动显示长文本
  47. private bool _isScrollText = false;
  48. public bool IsScrollText
  49. {
  50. get { return _isScrollText; }
  51. set { SetProperty(ref _isScrollText, value); }
  52. }
  53. #endregion
  54. #endregion
  55. #region Command变量
  56. public DelegateCommand<string> NameTextChangedCommand { get; set; }
  57. public DelegateCommand<string> ToolTipTextChangedCommand { get; set; }
  58. public DelegateCommand<object> IsReadOnlyCheckedCommand { get; set; }
  59. public DelegateCommand<object> RequiredFieldCheckedCommand { get; set; }
  60. //外观
  61. public DelegateCommand<object> ThicknessChangedCommand { get; set; }
  62. #endregion
  63. #region 变量
  64. public UpdateAttributeHelper AttribEvent;
  65. private CPDFViewer PDFViewer;
  66. private WidgetTextBoxArgs textBoxArgs;
  67. private bool IsCurrentWidget = false;
  68. private IDialogService dialogs;
  69. #endregion
  70. #region 初始化
  71. public TextFieldPropertyViewModel(IDialogService dialogService)
  72. {
  73. dialogs = dialogService;
  74. InitVariable();
  75. InitCommand();
  76. }
  77. private void InitVariable()
  78. {
  79. }
  80. private void InitCommand()
  81. {
  82. NameTextChangedCommand = new DelegateCommand<string>(NameTextChanged);
  83. ToolTipTextChangedCommand = new DelegateCommand<string>(ToolTipTextChanged);
  84. IsReadOnlyCheckedCommand = new DelegateCommand<object>(IsReadOnlyChecked);
  85. RequiredFieldCheckedCommand = new DelegateCommand<object>(RequiredFieldChecked);
  86. //外观
  87. ThicknessChangedCommand = new DelegateCommand<object>(ThicknessChanged);
  88. ChangeValueHandler -= ChangeValue;
  89. ChangeValueHandler += ChangeValue;
  90. }
  91. private void ThicknessChanged(object obj)
  92. {
  93. if(obj != null)
  94. {
  95. var str = (double)obj;
  96. }
  97. }
  98. #endregion
  99. #region 一般处理
  100. private void NameTextChanged(string obj)
  101. {
  102. if(string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  103. {
  104. NameStr = obj;
  105. }
  106. }
  107. private void ToolTipTextChanged(string obj)
  108. {
  109. if (string.IsNullOrEmpty(obj) == false)
  110. {
  111. ToolTipStr = obj;
  112. }
  113. }
  114. private void IsReadOnlyChecked(object obj)
  115. {
  116. if(obj is bool)
  117. {
  118. IsReadOnly = (bool)obj;
  119. }
  120. }
  121. private void RequiredFieldChecked(object obj)
  122. {
  123. if (obj is bool)
  124. {
  125. IsRequiredField = (bool)obj;
  126. }
  127. }
  128. #endregion
  129. #region 外观处理
  130. #endregion
  131. #region 选项处理
  132. #endregion
  133. #region Navegation
  134. public bool IsNavigationTarget(NavigationContext navigationContext)
  135. {
  136. return true;
  137. }
  138. public void OnNavigatedFrom(NavigationContext navigationContext)
  139. {
  140. isCreateWidget = false;
  141. IsCurrentWidget = false;
  142. textBoxArgs = null;
  143. }
  144. public void OnNavigatedTo(NavigationContext navigationContext)
  145. {
  146. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  147. navigationContext.Parameters.TryGetValue<UpdateAttributeHelper>(ParameterNames.AnnotEvent, out AttribEvent);
  148. navigationContext.Parameters.TryGetValue<WidgetTextBoxArgs>("WidgetArgs", out textBoxArgs);
  149. GetWidgeText();
  150. }
  151. bool isCreateWidget = false;
  152. private void GetWidgeText()
  153. {
  154. //新建的form表单
  155. if (textBoxArgs == null)
  156. {
  157. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  158. WidgetTextBoxArgs textArgs = new WidgetTextBoxArgs();
  159. textArgs.BgColor = Colors.Transparent;
  160. textArgs.FontSize = 12;
  161. textArgs.FontFamily = "Courier New";
  162. textArgs.FontColor = Colors.Black;
  163. textArgs.FieldName = "TextBox";
  164. textBoxArgs = textArgs;
  165. PDFViewer.SetToolParam(textBoxArgs);
  166. isCreateWidget = true;
  167. }
  168. else
  169. {
  170. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  171. isCreateWidget = false;
  172. }
  173. GetProperty();
  174. IsCurrentWidget = true;
  175. }
  176. private void GetProperty()
  177. {
  178. if(textBoxArgs != null)
  179. {
  180. NameStr = textBoxArgs.FieldName;
  181. IsRequiredField = textBoxArgs.IsRequired;
  182. IsMultiLine = textBoxArgs.IsMultiLine;
  183. IsLocked = textBoxArgs.Locked;
  184. ToolTipStr = textBoxArgs.Tooltip;
  185. }
  186. }
  187. //更改基类公共属性后,触发的事件
  188. private void ChangeValue(object sender, FormAttributeType e)
  189. {
  190. switch(e)
  191. {
  192. case FormAttributeType.Name:
  193. textBoxArgs.FieldName = NameStr;
  194. AttribEvent?.UpdateAttrib(AnnotAttrib.FieldName, NameStr);
  195. break;
  196. case FormAttributeType.ToolTip:
  197. textBoxArgs.Tooltip = ToolTipStr;
  198. AttribEvent?.UpdateAttrib(AnnotAttrib.Tooltip, ToolTipStr);
  199. break;
  200. case FormAttributeType.IsSolid:
  201. textBoxArgs.BorderStyle = IsSolid;
  202. AttribEvent?.UpdateAttrib(AnnotAttrib.LineStyle, IsSolid);
  203. break;
  204. case FormAttributeType.IsLocked:
  205. textBoxArgs.Locked = IsLocked;
  206. AttribEvent?.UpdateAttrib(AnnotAttrib.Locked, IsLocked);
  207. break;
  208. case FormAttributeType.HeightSize:
  209. // textBoxArgs.DefaultWidth = HeightSize;
  210. //AttribEvent?.UpdateAttrib(, BorderThiness);
  211. break;
  212. case FormAttributeType.BorderThiness:
  213. textBoxArgs.LineWidth = BorderThiness;
  214. AttribEvent?.UpdateAttrib(AnnotAttrib.Thickness, BorderThiness);
  215. break;
  216. case FormAttributeType.BorderColor:
  217. textBoxArgs.BgColor = BorderColor;
  218. AttribEvent?.UpdateAttrib(AnnotAttrib.Color, BorderColor);
  219. break;
  220. case FormAttributeType.ContentColor:
  221. textBoxArgs.FontColor = ContentColor;
  222. AttribEvent?.UpdateAttrib(AnnotAttrib.FontColor, ContentColor);
  223. break;
  224. case FormAttributeType.IsReadOnly:
  225. textBoxArgs.ReadOnly = IsReadOnly;
  226. AttribEvent?.UpdateAttrib(AnnotAttrib.ReadOnly, IsReadOnly);
  227. break;
  228. case FormAttributeType.WidthSize:
  229. textBoxArgs.DefaultWidth = WidthSize;
  230. break;
  231. case FormAttributeType.IsRequiredField:
  232. textBoxArgs.IsRequired = IsRequiredField;
  233. AttribEvent?.UpdateAttrib(AnnotAttrib.IsRequired, IsRequiredField);
  234. break;
  235. }
  236. AttribEvent?.UpdateAnnot();
  237. }
  238. private void ChangeFieldValue(string tag)
  239. {
  240. switch(tag)
  241. {
  242. case "FormPos":
  243. textBoxArgs.FormField = FormField.Visible;
  244. AttribEvent?.UpdateAttrib(AnnotAttrib.FormField,FormPos);
  245. break;
  246. }
  247. AttribEvent?.UpdateAnnot();
  248. }
  249. #endregion
  250. }
  251. }