TextFieldPropertyViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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;
  18. using System.Windows.Controls;
  19. using System.Windows.Media;
  20. namespace PDF_Office.ViewModels.Form
  21. {
  22. public class TextFieldPropertyViewModel : FormBaseVM, INavigationAware
  23. {
  24. #region 属性
  25. #region 一般
  26. private FormFieldType _formPos;
  27. public FormFieldType FormPos
  28. {
  29. get { return _formPos; }
  30. set { SetProperty(ref _formPos, value); ChangeFieldValue("FormPos"); }
  31. }
  32. #endregion
  33. #region 选项
  34. //默认值
  35. private string _defaultValue;
  36. public string DefaultValue
  37. {
  38. get { return _defaultValue; }
  39. set { SetProperty(ref _defaultValue, value); }
  40. }
  41. //多行
  42. private bool _isMultiline = false;
  43. public bool IsMultiLine
  44. {
  45. get { return _isMultiline; }
  46. set { SetProperty(ref _isMultiline, value); ChangeFieldValue("IsMultiLine"); }
  47. }
  48. //滚动显示长文本
  49. private bool _isScrollText = false;
  50. public bool IsScrollText
  51. {
  52. get { return _isScrollText; }
  53. set { SetProperty(ref _isScrollText, value); ChangeFieldValue("IsScrollText"); }
  54. }
  55. #endregion
  56. private FontFamily _fontFamily;
  57. public FontFamily FontFamilyItem
  58. {
  59. get { return _fontFamily; }
  60. set { SetProperty(ref _fontFamily, value); ChangeFieldValue("FontFamilyItem"); }
  61. }
  62. private FontStyle _fontStyle;
  63. public FontStyle FontStyleItem
  64. {
  65. get { return _fontStyle; }
  66. set { SetProperty(ref _fontStyle, value); ChangeFieldValue("FontStyleItem"); }
  67. }
  68. private FontWeight _fontWeight;
  69. public FontWeight FontWeightItem
  70. {
  71. get { return _fontWeight; }
  72. set { SetProperty(ref _fontWeight, value); ChangeFieldValue("FontWeightItem"); }
  73. }
  74. private C_TEXT_ALIGNMENT _textAlignment;
  75. public C_TEXT_ALIGNMENT TextAlignmentItem
  76. {
  77. get { return _textAlignment; }
  78. set { SetProperty(ref _textAlignment, value); ChangeFieldValue("TextAlignmentItem"); }
  79. }
  80. private string _content;
  81. public string FormContent
  82. {
  83. get { return _content; }
  84. set { SetProperty(ref _content, value); ChangeFieldValue("FormContent"); }
  85. }
  86. #endregion
  87. #region Command变量
  88. public DelegateCommand<string> NameTextChangedCommand { get; set; }
  89. public DelegateCommand<string> ToolTipTextChangedCommand { get; set; }
  90. public DelegateCommand<object> IsReadOnlyCheckedCommand { get; set; }
  91. public DelegateCommand<object> RequiredFieldCheckedCommand { get; set; }
  92. //外观
  93. public DelegateCommand<object> ThicknessChangedCommand { get; set; }
  94. public DelegateCommand<object> ResetColorCommand { get; set; }
  95. public DelegateCommand<object> ResetColorCheckedBtnCommand { get; set; }
  96. public DelegateCommand<object> FontFamilyChangedCommand { get; set; }
  97. public DelegateCommand<object> FontStyleChangedCommand { get; set; }
  98. public DelegateCommand<object> AlignmentChangedCommand { get; set; }
  99. public DelegateCommand<string> FormContentTextChangedCommand { get; set; }
  100. public DelegateCommand<object> IsMultiLineCheckedCommand { get; set; }
  101. public DelegateCommand<object> IsScrollToDisplayCheckedCommand { get; set; }
  102. public DelegateCommand<object> LineStyleCommand { get; set; }
  103. #endregion
  104. #region 变量
  105. public UpdateAttributeHelper AttribEvent;
  106. private CPDFViewer PDFViewer;
  107. private WidgetTextBoxArgs textBoxArgs;
  108. private bool IsCurrentWidget = false;
  109. private IDialogService dialogs;
  110. #endregion
  111. #region 初始化
  112. public TextFieldPropertyViewModel(IDialogService dialogService)
  113. {
  114. dialogs = dialogService;
  115. InitVariable();
  116. InitCommand();
  117. }
  118. private void InitVariable()
  119. {
  120. ResetColorOne = new ResetColor() {
  121. BorderColor = new SolidColorBrush(Colors.Transparent),
  122. FontColor = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0x00)),
  123. FillColor = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0x00))
  124. };
  125. ResetColorTwo = new ResetColor()
  126. {
  127. BorderColor = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0x00)),
  128. FontColor = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0x00)),
  129. FillColor = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0x00))
  130. };
  131. ResetColorThree = new ResetColor()
  132. {
  133. BorderColor = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0x00)),
  134. FontColor = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0x00)),
  135. FillColor = new SolidColorBrush(Color.FromArgb(0xFF, 0xBD, 0xDF, 0xFD))
  136. };
  137. ResetColorForth = new ResetColor()
  138. {
  139. BorderColor = new SolidColorBrush(Color.FromArgb(0xFF, 0xff, 0x00, 0x00)),
  140. FontColor = new SolidColorBrush(Color.FromArgb(0xFF, 0xff, 0x00, 0x00)),
  141. FillColor = new SolidColorBrush(Colors.Transparent)
  142. };
  143. }
  144. private void InitCommand()
  145. {
  146. NameTextChangedCommand = new DelegateCommand<string>(NameTextChanged);
  147. ToolTipTextChangedCommand = new DelegateCommand<string>(ToolTipTextChanged);
  148. IsReadOnlyCheckedCommand = new DelegateCommand<object>(IsReadOnlyChecked);
  149. RequiredFieldCheckedCommand = new DelegateCommand<object>(RequiredFieldChecked);
  150. //外观
  151. ThicknessChangedCommand = new DelegateCommand<object>(ThicknessChanged);
  152. ResetColorCommand = new DelegateCommand<object>(ResetColorEvent);
  153. ResetColorCheckedBtnCommand = new DelegateCommand<object>(ResetColorCheckedBtn);
  154. FontFamilyChangedCommand = new DelegateCommand<object>(FontFamilyChanged);
  155. FontStyleChangedCommand = new DelegateCommand<object>(FontStyleChanged);
  156. AlignmentChangedCommand = new DelegateCommand<object>(AlignmentChanged);
  157. FormContentTextChangedCommand = new DelegateCommand<string>(FormContentTextChanged);
  158. IsMultiLineCheckedCommand = new DelegateCommand<object>(IsMultiLineChecked);
  159. IsScrollToDisplayCheckedCommand = new DelegateCommand<object>(IsScrollToDisplayChecked);
  160. LineStyleCommand = new DelegateCommand<object>(LineStyleBtnEvent);
  161. ChangeValueHandler -= ChangeValue;
  162. ChangeValueHandler += ChangeValue;
  163. }
  164. private void LineStyleBtnEvent(object obj)
  165. {
  166. if(obj != null)
  167. {
  168. switch((string)obj)
  169. {
  170. case "Solid":
  171. IsSolid = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_SOLID;
  172. break;
  173. case "Dotted":
  174. IsSolid = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_DASHDED;
  175. break;
  176. }
  177. }
  178. }
  179. private void IsScrollToDisplayChecked(object obj)
  180. {
  181. if(obj != null)
  182. {
  183. IsScrollText = (bool)obj;
  184. }
  185. }
  186. private void IsMultiLineChecked(object obj)
  187. {
  188. if (obj != null)
  189. {
  190. IsMultiLine = (bool)obj;
  191. }
  192. }
  193. private void FormContentTextChanged(string obj)
  194. {
  195. if(obj != null)
  196. {
  197. FormContent = obj;
  198. }
  199. }
  200. private void AlignmentChanged(object obj)
  201. {
  202. if (obj != null)
  203. {
  204. var combo = (ComboBoxItem)obj;
  205. if (combo != null)
  206. {
  207. switch (combo.Content)
  208. {
  209. case "Left":
  210. TextAlignmentItem = C_TEXT_ALIGNMENT.ALIGNMENT_LEFT;
  211. break;
  212. case "Center":
  213. TextAlignmentItem = C_TEXT_ALIGNMENT.ALIGNMENT_CENTER;
  214. break;
  215. case "Right":
  216. TextAlignmentItem = C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT;
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. private void FontStyleChanged(object obj)
  223. {
  224. if(obj != null)
  225. {
  226. var combo = (ComboBoxItem)obj;
  227. if(combo != null)
  228. {
  229. switch(combo.Content)
  230. {
  231. case "Regular":
  232. FontStyleItem = FontStyles.Normal;
  233. FontWeightItem = FontWeights.Normal;
  234. break;
  235. case "Bold":
  236. FontStyleItem = FontStyles.Normal;
  237. FontWeightItem = FontWeights.Bold;
  238. break;
  239. case "Italic":
  240. FontStyleItem = FontStyles.Italic;
  241. FontWeightItem = FontWeights.Normal;
  242. break;
  243. case "Bold Italic":
  244. FontStyleItem = FontStyles.Italic;
  245. FontWeightItem = FontWeights.Bold;
  246. break;
  247. }
  248. }
  249. }
  250. }
  251. private void FontFamilyChanged(object obj)
  252. {
  253. if(obj!= null)
  254. {
  255. switch((int)obj)
  256. {
  257. case 0:
  258. FontFamilyItem = new FontFamily("Courier");
  259. break;
  260. case 1:
  261. FontFamilyItem = new FontFamily("Helvetica");
  262. break;
  263. case 2:
  264. FontFamilyItem = new FontFamily("Times Roman");
  265. break;
  266. }
  267. }
  268. }
  269. private void ResetColorCheckedBtn(object obj)
  270. {
  271. if(obj != null)
  272. {
  273. var str = obj as string;
  274. if(str != null)
  275. {
  276. switch(str)
  277. {
  278. case "One":
  279. BorderColor = ResetColorOne.BorderColor.Color;
  280. ContentColor = ResetColorOne.FontColor.Color;
  281. break;
  282. case "Two":
  283. BorderColor = ResetColorTwo.BorderColor.Color;
  284. ContentColor = ResetColorTwo.FontColor.Color;
  285. break;
  286. case "Three":
  287. BorderColor = ResetColorThree.BorderColor.Color;
  288. ContentColor = ResetColorThree.FontColor.Color;
  289. break;
  290. case "Forth":
  291. BorderColor = ResetColorForth.BorderColor.Color;
  292. ContentColor = ResetColorForth.FontColor.Color;
  293. break;
  294. }
  295. }
  296. }
  297. }
  298. private void ResetColorEvent(object obj)
  299. {
  300. //if(obj != null && (ResetColor)obj != null)
  301. // {
  302. // var resetcolor = obj as ResetColor;
  303. // }
  304. bool result = true;
  305. DialogParameters value = new DialogParameters();
  306. value.Add(ParameterNames.PDFViewer, PDFViewer);
  307. dialogs.ShowDialog(DialogNames.EditPresetColorsDialog, value, e =>
  308. {
  309. if (e.Result != ButtonResult.OK)
  310. {
  311. result = false;
  312. }
  313. EditPresetColorsDialogViewModel DialogVM = e.Parameters.GetValue<EditPresetColorsDialogViewModel>(ParameterNames.DataModel);
  314. if (DialogVM != null)
  315. {
  316. // CreateSignature(DialogVM);
  317. }
  318. });
  319. if (!result)
  320. {
  321. return;
  322. }
  323. }
  324. private void ThicknessChanged(object obj)
  325. {
  326. if(obj != null)
  327. {
  328. BorderThiness = (double)obj;
  329. }
  330. }
  331. #endregion
  332. #region 一般处理
  333. private void NameTextChanged(string obj)
  334. {
  335. if(string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  336. {
  337. NameStr = obj;
  338. }
  339. }
  340. private void ToolTipTextChanged(string obj)
  341. {
  342. if (string.IsNullOrEmpty(obj) == false)
  343. {
  344. ToolTipStr = obj;
  345. }
  346. }
  347. private void IsReadOnlyChecked(object obj)
  348. {
  349. if(obj is bool)
  350. {
  351. IsReadOnly = (bool)obj;
  352. }
  353. }
  354. private void RequiredFieldChecked(object obj)
  355. {
  356. if (obj is bool)
  357. {
  358. IsRequiredField = (bool)obj;
  359. }
  360. }
  361. #endregion
  362. #region 外观处理
  363. #endregion
  364. #region 选项处理
  365. #endregion
  366. #region Navegation
  367. public bool IsNavigationTarget(NavigationContext navigationContext)
  368. {
  369. return true;
  370. }
  371. public void OnNavigatedFrom(NavigationContext navigationContext)
  372. {
  373. isCreateWidget = false;
  374. IsCurrentWidget = false;
  375. textBoxArgs = null;
  376. }
  377. public void OnNavigatedTo(NavigationContext navigationContext)
  378. {
  379. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  380. navigationContext.Parameters.TryGetValue<UpdateAttributeHelper>(ParameterNames.AnnotEvent, out AttribEvent);
  381. navigationContext.Parameters.TryGetValue<WidgetTextBoxArgs>("WidgetArgs", out textBoxArgs);
  382. GetWidgeText();
  383. }
  384. bool isCreateWidget = false;
  385. private void GetWidgeText()
  386. {
  387. //新建的form表单
  388. if (textBoxArgs == null)
  389. {
  390. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  391. WidgetTextBoxArgs textArgs = new WidgetTextBoxArgs();
  392. textArgs.BgColor = Colors.Transparent;
  393. textArgs.FontSize = 12;
  394. textArgs.FontFamily = "Courier New";
  395. textArgs.FontColor = Colors.Black;
  396. textArgs.FieldName = "TextBox";
  397. textBoxArgs = textArgs;
  398. PDFViewer.SetToolParam(textBoxArgs);
  399. isCreateWidget = true;
  400. }
  401. else
  402. {
  403. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  404. isCreateWidget = false;
  405. }
  406. GetProperty();
  407. IsCurrentWidget = true;
  408. }
  409. private void GetProperty()
  410. {
  411. if(textBoxArgs != null)
  412. {
  413. NameStr = textBoxArgs.FieldName;
  414. IsRequiredField = textBoxArgs.IsRequired;
  415. IsMultiLine = textBoxArgs.IsMultiLine;
  416. IsLocked = textBoxArgs.Locked;
  417. ToolTipStr = textBoxArgs.Tooltip;
  418. }
  419. }
  420. //更改基类公共属性后,触发的事件
  421. private void ChangeValue(object sender, FormAttributeType e)
  422. {
  423. switch(e)
  424. {
  425. case FormAttributeType.Name:
  426. textBoxArgs.FieldName = NameStr;
  427. AttribEvent?.UpdateAttrib(AnnotAttrib.FieldName, NameStr);
  428. break;
  429. case FormAttributeType.ToolTip:
  430. textBoxArgs.Tooltip = ToolTipStr;
  431. AttribEvent?.UpdateAttrib(AnnotAttrib.Tooltip, ToolTipStr);
  432. break;
  433. case FormAttributeType.IsSolid:
  434. textBoxArgs.BorderStyle = IsSolid;
  435. AttribEvent?.UpdateAttrib(AnnotAttrib.LineStyle, IsSolid);
  436. break;
  437. case FormAttributeType.IsLocked:
  438. textBoxArgs.Locked = IsLocked;
  439. AttribEvent?.UpdateAttrib(AnnotAttrib.Locked, IsLocked);
  440. break;
  441. case FormAttributeType.HeightSize:
  442. textBoxArgs.Height = HeightSize;
  443. //AttribEvent?.UpdateAttrib(AnnotAttrib.w, HeightSize);
  444. break;
  445. case FormAttributeType.BorderThiness:
  446. textBoxArgs.LineWidth = BorderThiness;
  447. AttribEvent?.UpdateAttrib(AnnotAttrib.Thickness, BorderThiness);
  448. break;
  449. case FormAttributeType.BorderColor:
  450. textBoxArgs.BgColor = BorderColor;
  451. AttribEvent?.UpdateAttrib(AnnotAttrib.Color, BorderColor);
  452. break;
  453. case FormAttributeType.ContentColor:
  454. textBoxArgs.FontColor = ContentColor;
  455. AttribEvent?.UpdateAttrib(AnnotAttrib.FontColor, ContentColor);
  456. break;
  457. case FormAttributeType.FillColor:
  458. textBoxArgs.BgColor = FillColor;
  459. AttribEvent?.UpdateAttrib(AnnotAttrib.FillColor, FillColor);
  460. break;
  461. case FormAttributeType.IsReadOnly:
  462. textBoxArgs.ReadOnly = IsReadOnly;
  463. AttribEvent?.UpdateAttrib(AnnotAttrib.ReadOnly, IsReadOnly);
  464. break;
  465. case FormAttributeType.WidthSize:
  466. textBoxArgs.Width = WidthSize;
  467. break;
  468. case FormAttributeType.IsRequiredField:
  469. textBoxArgs.IsRequired = IsRequiredField;
  470. AttribEvent?.UpdateAttrib(AnnotAttrib.IsRequired, IsRequiredField);
  471. break;
  472. }
  473. AttribEvent?.UpdateAnnot();
  474. }
  475. private void ChangeFieldValue(string tag)
  476. {
  477. switch(tag)
  478. {
  479. case "FormPos":
  480. textBoxArgs.FormField = FormField.Visible;
  481. AttribEvent?.UpdateAttrib(AnnotAttrib.FormField,FormPos);
  482. break;
  483. case "FontFamilyItem":
  484. textBoxArgs.FontFamily = FontFamilyItem.Source;
  485. AttribEvent?.UpdateAttrib(AnnotAttrib.FontFamily, FontFamilyItem.Source);
  486. break;
  487. case "FontStyleItem":
  488. textBoxArgs.FontStyle = FontStyleItem;
  489. AttribEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyleItem);
  490. break;
  491. case "FontWeightItem":
  492. textBoxArgs.FontWeight = FontWeightItem;
  493. AttribEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeightItem);
  494. break;
  495. case "TextAlignmentItem":
  496. textBoxArgs.Alignment = TextAlignmentItem;
  497. AttribEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignmentItem);
  498. break;
  499. case "FormContent":
  500. textBoxArgs.Content = FormContent;
  501. AttribEvent?.UpdateAttrib(AnnotAttrib.Text, FormContent);
  502. break;
  503. case "IsScrollText":
  504. textBoxArgs.ScrollFlag = IsScrollText;
  505. AttribEvent?.UpdateAttrib(AnnotAttrib.ScrollFlag, IsScrollText);
  506. break;
  507. case "IsMultiLine":
  508. textBoxArgs.IsMultiLine = IsMultiLine;
  509. AttribEvent?.UpdateAttrib(AnnotAttrib.IsMutilLine, IsMultiLine);
  510. break;
  511. }
  512. AttribEvent?.UpdateAnnot();
  513. }
  514. #endregion
  515. }
  516. }