TextFieldPropertyViewModel.cs 20 KB

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