TextFieldPropertyViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Master.CustomControl.CompositeControl;
  6. using PDF_Master.Helper;
  7. using PDF_Master.Model;
  8. using PDF_Master.Model.From;
  9. using Prism.Commands;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using Prism.Services.Dialogs;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Media;
  21. namespace PDF_Master.ViewModels.Form
  22. {
  23. public class TextFieldPropertyViewModel : FormBaseVM, INavigationAware
  24. {
  25. #region Command变量
  26. public DelegateCommand<string> FieldNameTextChangedCommand { get; set; }
  27. public DelegateCommand<string> ToolTipTextChangedCommand { get; set; }
  28. //外观
  29. public DelegateCommand<object> ResetColorCommand { get; set; }
  30. public DelegateCommand<object> ResetColorCheckedBtnCommand { get; set; }
  31. public DelegateCommand<string> FormContentTextChangedCommand { get; set; }
  32. public DelegateCommand<object> LineStyleCommand { get; set; }
  33. #endregion
  34. #region 变量
  35. private CPDFViewer PDFViewer;
  36. private WidgetTextBoxArgs textBoxArgs;
  37. private IDialogService dialogs;
  38. public event EventHandler<int> SelectResetColorBtnHandler;
  39. public List<ComboDataItem> FontFamilyItems { get; private set; }
  40. public List<ComboDataItem> FontStyleItems { get; private set; }
  41. public List<ComboDataItem> AglinmentItems { get; private set; }
  42. #endregion
  43. #region 初始化
  44. public TextFieldPropertyViewModel(IDialogService dialogService)
  45. {
  46. dialogs = dialogService;
  47. InitVariable();
  48. InitCommand();
  49. }
  50. private void InitVariable()
  51. {
  52. InitAllResetColor();
  53. InitFontFamilyComboBox();
  54. InitFontStyleComboBox();
  55. InitAglinmentItemsComboBox();
  56. }
  57. private void InitAllResetColor()
  58. {
  59. ResetColorOne = InitResetColor(Colors.Transparent, Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Colors.Transparent);
  60. ResetColorTwo = InitResetColor(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Colors.Transparent);
  61. ResetColorThree = InitResetColor(Colors.Transparent, Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0xBD, 0xDF, 0xFD));
  62. ResetColorForth = InitResetColor(Color.FromArgb(0xFF, 0xff, 0x00, 0x00), Color.FromArgb(0xFF, 0xff, 0x00, 0x00), Colors.Transparent);
  63. }
  64. private void InitFontFamilyComboBox()
  65. {
  66. FontFamilyItems = new List<ComboDataItem>();
  67. ComboDataItem item = new ComboDataItem("Courier", "Courier New");
  68. FontFamilyItems.Add(item);
  69. item = new ComboDataItem("Helvetica", "Helvetica");
  70. FontFamilyItems.Add(item);
  71. item = new ComboDataItem("Times-Roman", "Times New Roman");
  72. FontFamilyItems.Add(item);
  73. }
  74. private void InitFontStyleComboBox()
  75. {
  76. FontStyleItems = new List<ComboDataItem>();
  77. ComboDataItem item = new ComboDataItem("Regular", "Regular");
  78. FontStyleItems.Add(item);
  79. item = new ComboDataItem("Bold", "Bold");
  80. FontStyleItems.Add(item);
  81. item = new ComboDataItem("Italic", "Italic");
  82. FontStyleItems.Add(item);
  83. item = new ComboDataItem("Bold Italic", "Bold Italic");
  84. FontStyleItems.Add(item);
  85. }
  86. private void InitAglinmentItemsComboBox()
  87. {
  88. AglinmentItems = new List<ComboDataItem>();
  89. ComboDataItem item = new ComboDataItem("Left", "Left");
  90. AglinmentItems.Add(item);
  91. item = new ComboDataItem("Center", "Center");
  92. AglinmentItems.Add(item);
  93. item = new ComboDataItem("Right", "Right");
  94. AglinmentItems.Add(item);
  95. }
  96. private void InitCommand()
  97. {
  98. //一般
  99. FieldNameTextChangedCommand = new DelegateCommand<string>(FieldNameTextChanged);
  100. ToolTipTextChangedCommand = new DelegateCommand<string>(ToolTipTextChanged);
  101. //外观
  102. ResetColorCheckedBtnCommand = new DelegateCommand<object>(ResetColorCheckedBtn);
  103. ResetColorCommand = new DelegateCommand<object>(ResetColorEvent);
  104. LineStyleCommand = new DelegateCommand<object>(LineStyleBtnEvent);
  105. //选项
  106. FormContentTextChangedCommand = new DelegateCommand<string>(FormContentTextChanged);
  107. }
  108. #endregion
  109. #region 事件
  110. private void FieldNameTextChanged(string obj)
  111. {
  112. if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  113. {
  114. FieldName = obj;
  115. }
  116. }
  117. private void ToolTipTextChanged(string obj)
  118. {
  119. if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  120. {
  121. ToolTipStr = obj;
  122. }
  123. }
  124. private void LineStyleBtnEvent(object obj)
  125. {
  126. if (obj != null)
  127. {
  128. switch ((string)obj)
  129. {
  130. case "Solid":
  131. BorderStyle = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_SOLID;
  132. break;
  133. case "Dotted":
  134. BorderStyle = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_DASHDED;
  135. break;
  136. }
  137. }
  138. }
  139. private void FormContentTextChanged(string obj)
  140. {
  141. if (obj != null && IsCurrentWidget == true)
  142. {
  143. FormContent = obj;
  144. }
  145. }
  146. private void ResetColorCheckedBtn(object obj)
  147. {
  148. if (obj != null)
  149. {
  150. var str = obj as string;
  151. if (str != null)
  152. {
  153. switch (str)
  154. {
  155. case "One":
  156. BorderColor = ResetColorOne.BorderColor.Color;
  157. ContentColor = ResetColorOne.FontColor.Color;
  158. FillColor = ResetColorOne.FillColor.Color;
  159. break;
  160. case "Two":
  161. BorderColor = ResetColorTwo.BorderColor.Color;
  162. ContentColor = ResetColorTwo.FontColor.Color;
  163. FillColor = ResetColorTwo.FillColor.Color;
  164. break;
  165. case "Three":
  166. BorderColor = ResetColorThree.BorderColor.Color;
  167. ContentColor = ResetColorThree.FontColor.Color;
  168. FillColor = ResetColorThree.FillColor.Color;
  169. break;
  170. case "Forth":
  171. BorderColor = ResetColorForth.BorderColor.Color;
  172. ContentColor = ResetColorForth.FontColor.Color;
  173. FillColor = ResetColorForth.FillColor.Color;
  174. break;
  175. }
  176. }
  177. }
  178. }
  179. private void ResetColorEvent(object obj)
  180. {
  181. bool result = true;
  182. DialogParameters value = new DialogParameters();
  183. value.Add(ParameterNames.PDFViewer, PDFViewer);
  184. dialogs.ShowDialog(DialogNames.EditPresetColorsDialog, value, e =>
  185. {
  186. if (e.Result != ButtonResult.OK)
  187. {
  188. result = false;
  189. }
  190. EditPresetColorsDialogViewModel DialogVM = e.Parameters.GetValue<EditPresetColorsDialogViewModel>(ParameterNames.DataModel);
  191. if (DialogVM != null)
  192. {
  193. }
  194. });
  195. if (!result)
  196. {
  197. return;
  198. }
  199. }
  200. #endregion
  201. #region 外部XAML触发事件
  202. private void UpdataSelectResetColorBtn()
  203. {
  204. int result = 0;
  205. if (UpdataSelectResetColor(ResetColorOne))
  206. {
  207. result = 1;
  208. }
  209. else if (UpdataSelectResetColor(ResetColorTwo))
  210. {
  211. result = 2;
  212. }
  213. else if (UpdataSelectResetColor(ResetColorThree))
  214. {
  215. result = 3;
  216. }
  217. else if (UpdataSelectResetColor(ResetColorForth))
  218. {
  219. result = 4;
  220. }
  221. SelectResetColorBtnHandler?.Invoke(null, result);
  222. }
  223. private bool UpdataSelectResetColor(ResetColor reset)
  224. {
  225. if (reset.FillColor.Color == FillColor &&
  226. reset.FontColor.Color == ContentColor &&
  227. reset.BorderColor.Color == BorderColor
  228. )
  229. {
  230. return true;
  231. }
  232. return false;
  233. }
  234. #endregion
  235. #region Navegation
  236. public bool IsNavigationTarget(NavigationContext navigationContext)
  237. {
  238. return true;
  239. }
  240. public void OnNavigatedFrom(NavigationContext navigationContext)
  241. {
  242. isCreateWidget = false;
  243. IsCurrentWidget = false;
  244. textBoxArgs = null;
  245. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  246. }
  247. public void OnNavigatedTo(NavigationContext navigationContext)
  248. {
  249. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  250. navigationContext.Parameters.TryGetValue<UpdateAttributeHelper>(ParameterNames.AnnotEvent, out AttribEvent);
  251. navigationContext.Parameters.TryGetValue<WidgetTextBoxArgs>("WidgetArgs", out textBoxArgs);
  252. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  253. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  254. GetWidgeText();
  255. UpdataSelectResetColorBtn();
  256. }
  257. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  258. {
  259. if (e != null && e.Count > 0)
  260. {
  261. var widgeArgs = e[e.Count - 1].EditAnnotArgs as WidgetArgs;
  262. if (widgeArgs != null)
  263. {
  264. AnnotEditEvent editEvent = e[e.Count - 1];
  265. if (editEvent.EditAction == ActionType.Modify)
  266. {
  267. SetSizeNoUpdateValue(widgeArgs.Width, widgeArgs.Height);
  268. }
  269. }
  270. }
  271. }
  272. private void GetWidgeText()
  273. {
  274. //新建的form表单
  275. if (textBoxArgs == null)
  276. {
  277. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  278. WidgetTextBoxArgs textArgs = new WidgetTextBoxArgs();
  279. textArgs.BgColor = Colors.LightGray;
  280. textArgs.Width = 150;
  281. textArgs.Height = 22;
  282. textBoxArgs = textArgs;
  283. textArgs.FontSize = 12;
  284. textArgs.FontName = "Helvatica";
  285. textArgs.LineWidth = 2;
  286. textArgs.FontColor = Colors.Black;
  287. textArgs.LineColor = Colors.Black;
  288. // textArgs.
  289. PDFViewer.SetToolParam(textBoxArgs);
  290. isCreateWidget = true;
  291. }
  292. else
  293. {
  294. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  295. isCreateWidget = false;
  296. }
  297. GetProperty();
  298. IsCurrentWidget = true;
  299. }
  300. private void GetProperty()
  301. {
  302. if (textBoxArgs != null)
  303. {
  304. IsLocked = textBoxArgs.Locked;
  305. FieldName = textBoxArgs.FieldName;
  306. ToolTipStr = textBoxArgs.Tooltip;
  307. IsReadOnly = textBoxArgs.ReadOnly;
  308. IsRequiredField = textBoxArgs.IsRequired;
  309. FillColor = textBoxArgs.BgColor;
  310. ContentColor = textBoxArgs.FontColor;
  311. BorderColor = textBoxArgs.LineColor;
  312. BorderThiness = textBoxArgs.LineWidth;
  313. BorderStyle = textBoxArgs.BorderStyle;
  314. FormField = (FormFieldType)textBoxArgs.FormField;
  315. string fontWeightStyleStr = "";
  316. if (textBoxArgs.IsItalic ==false)
  317. {
  318. if (textBoxArgs.IsBold == false)
  319. fontWeightStyleStr = "Regular";
  320. else
  321. fontWeightStyleStr = "Bold";
  322. }
  323. else
  324. {
  325. if (textBoxArgs.IsBold == false)
  326. fontWeightStyleStr = "Italic";
  327. else
  328. fontWeightStyleStr = "Bold Italic";
  329. }
  330. FontWeightStyleItem = new ComboDataItem(fontWeightStyleStr);
  331. FontFamilyData = new ComboDataItem(textBoxArgs.FontName);
  332. //避免BorderStyle跟上一个值相同,而没触发更改IsSolid属性
  333. if (BorderStyle == C_BORDER_STYLE.BS_SOLID)
  334. IsSolid = true;
  335. else
  336. IsSolid = false;
  337. FontSizeData = new ComboDataItem(textBoxArgs.FontSize);
  338. if (isCreateWidget == false)
  339. {
  340. HeightSize = textBoxArgs.Height;
  341. WidthSize = textBoxArgs.Width;
  342. }
  343. string alignmentStr = "";
  344. if (textBoxArgs.Alignment == C_TEXT_ALIGNMENT.ALIGNMENT_LEFT)
  345. alignmentStr = "left";
  346. else if (textBoxArgs.Alignment == C_TEXT_ALIGNMENT.ALIGNMENT_CENTER)
  347. alignmentStr = "Center";
  348. else
  349. alignmentStr = "Right";
  350. TextAlignmentData = new ComboDataItem(alignmentStr, alignmentStr);
  351. FormContent = textBoxArgs.Text;
  352. IsMultiLine = textBoxArgs.IsMultiLine;
  353. IsScrollText = textBoxArgs.ScrollFlag;
  354. }
  355. }
  356. #endregion
  357. }
  358. }