FreetextAnnotPropertyViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using PDF_Office.CustomControl.CompositeControl;
  4. using PDF_Office.Model;
  5. using PDF_Office.Model.PropertyPanel.AnnotPanel;
  6. using PDF_Office.ViewModels.Tools;
  7. using PDFSettings;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Media;
  19. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  20. {
  21. public class FreetextAnnotPropertyViewModel : BindableBase, INavigationAware
  22. {
  23. private FontBoardVm _fontVm = new FontBoardVm(true);
  24. public FontBoardVm FontVm
  25. {
  26. get { return _fontVm; }
  27. set { SetProperty(ref _fontVm, value); }
  28. }
  29. private AnnotBasePropertyVM _basicVm = new AnnotBasePropertyVM();
  30. public AnnotBasePropertyVM BasicVm
  31. {
  32. get { return _basicVm; }
  33. set => SetProperty(ref _basicVm, value);
  34. }
  35. private bool _isFillFreeText = false;
  36. public bool IsFillFreeTextAnnot
  37. {
  38. get { return _isFillFreeText; }
  39. set => SetProperty(ref _isFillFreeText, value);
  40. }
  41. private bool _isCheckedTime = false;
  42. public bool IsCheckedTime
  43. {
  44. get { return _isCheckedTime; }
  45. set => SetProperty(ref _isCheckedTime, value);
  46. }
  47. private ComboDataItem _currrentDateFormat = new ComboDataItem("M/d", "format");
  48. public ComboDataItem CurrrentDateFormat
  49. {
  50. get { return _currrentDateFormat; }
  51. set => SetProperty(ref _currrentDateFormat, value);
  52. }
  53. public List<ComboDataItem> DateFormatItems { get; private set; }
  54. public AnnotAttribEvent AnnotEvent { get; set; }
  55. private FreeTextAnnotArgs Annot;
  56. private AnnotPropertyPanel PropertyPanel;
  57. public DelegateCommand SelectedPresetFontCommand { get; set; }
  58. public DelegateCommand FontFamilyChangedCommand { get; set; }
  59. public DelegateCommand FontStyleWeightChangedCommand { get; set; }
  60. public DelegateCommand<object> SelectedFillOpacityCommand { get; set; }
  61. public DelegateCommand<object> SelectedColorCommand { get; set; }
  62. public DelegateCommand<object> SelectedFillColorCommand { get; set; }
  63. public DelegateCommand FontSizeChangedCommand { get; set; }
  64. public DelegateCommand<object> TextAlignCheckedCommand { get; set; }
  65. public DelegateCommand<object> LineModeCheckedCommand { get; set; }
  66. public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }
  67. public DelegateCommand CustomFontStyleCommand { get; set; }
  68. public DelegateCommand ReDefineFontStyleCommand { get; set; }
  69. public DelegateCommand RestoreDefaultStyleCommand { get; set; }
  70. public DelegateCommand DateFormatChangedCommand { get; set; }
  71. public DelegateCommand ShowTimeCheckedChnagedCommand { get; set; }
  72. public event EventHandler<object> LoadPropertyHandler;
  73. public FreetextAnnotPropertyViewModel()
  74. {
  75. //文本设置
  76. //预设文本样式
  77. SelectedPresetFontCommand = new DelegateCommand(SelectedPresetFont);
  78. //文本字重、字体
  79. FontStyleWeightChangedCommand = new DelegateCommand(FontStyleWeightChanged);
  80. //字体
  81. FontFamilyChangedCommand = new DelegateCommand(FontFamilyChanged);
  82. //自定义
  83. CustomFontStyleCommand = new DelegateCommand(CustomFontStyle);
  84. //重新定义
  85. ReDefineFontStyleCommand = new DelegateCommand(ReDefineFontStyle);
  86. //恢复默认
  87. RestoreDefaultStyleCommand = new DelegateCommand(RestoreDefaultStyle);
  88. //颜色
  89. SelectedColorCommand = new DelegateCommand<object>(SelectedColor_Command);
  90. //填充颜色
  91. SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColorChange);
  92. //填充颜色不透明度
  93. SelectedFillOpacityCommand = new DelegateCommand<object>(SelectedFillOpacity);
  94. //大小
  95. FontSizeChangedCommand = new DelegateCommand(FontSizeChanged);
  96. //内容对齐
  97. TextAlignCheckedCommand = new DelegateCommand<object>(TextAlignChecked);
  98. //填写与签名:日期
  99. DateFormatChangedCommand = new DelegateCommand(DateFormatChanged);
  100. ShowTimeCheckedChnagedCommand = new DelegateCommand(ShowTimeCheckedChnaged);
  101. //MVP不上
  102. LineModeCheckedCommand = new DelegateCommand<object>(LineMode_Checked);
  103. SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
  104. InitVariable();
  105. }
  106. private void InitVariable()
  107. {
  108. DateFormatItems = new List<ComboDataItem>();
  109. DateFormatItems = TextFont.GetDateFormats();
  110. UpdateDateFormatLists();
  111. }
  112. private void UpdateDateFormatLists()
  113. {
  114. string strTime = " HH:mm:ss";
  115. foreach (var item in DateFormatItems)
  116. {
  117. if (IsCheckedTime)
  118. strTime = item.ValueStr + " HH:mm:ss";
  119. else
  120. strTime = item.ValueStr;
  121. string stadate = System.DateTime.Now.ToString(strTime);
  122. item.SetContent(stadate);
  123. if (CurrrentDateFormat != null && item.ValueStr == CurrrentDateFormat.ValueStr)
  124. CurrrentDateFormat.SetContent(stadate);
  125. }
  126. CurrrentDateFormat = new ComboDataItem(CurrrentDateFormat.ValueStr, CurrrentDateFormat.Content);
  127. }
  128. private void TextAlignChecked(object obj)
  129. {
  130. if ((string)obj != null)
  131. {
  132. switch ((string)obj)
  133. {
  134. case "AlignLeft":
  135. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Left);
  136. break;
  137. case "AlignCenter":
  138. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Center);
  139. break;
  140. case "AlignRight":
  141. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Right);
  142. break;
  143. case "Justify":
  144. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Justify);
  145. break;
  146. }
  147. }
  148. }
  149. private void LineMode_Checked(object obj)
  150. {
  151. if(obj != null)
  152. {
  153. var tag = ((string)obj);
  154. switch (tag)
  155. {
  156. case "Dashed":
  157. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash);
  158. break;
  159. case "Solid":
  160. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  161. break;
  162. }
  163. AnnotEvent?.UpdateAnnot();
  164. }
  165. }
  166. //从预设样式设置文本
  167. private void SelectedPresetFont()
  168. {
  169. if (FontVm.CurrentPresetFont != null)
  170. {
  171. var itemComboxItem = FontVm.CurrentPresetFont;
  172. var item = FontVm.PresetFontList.FirstOrDefault(temp => temp.mTag == itemComboxItem.ValueStr);
  173. if (item != null)
  174. {
  175. FontVm.GetCurrentFontSize(item.mFontSize);
  176. FontVm.GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
  177. FontVm.GetFontWeights_Style(item.mFontStyle, item.mFontWeight);
  178. Dictionary<AnnotAttrib, object> AnnotAttribDir = new Dictionary<AnnotAttrib, object>();
  179. AnnotAttribDir.Add(AnnotAttrib.FontSize, item.mFontSize);
  180. AnnotAttribDir.Add(AnnotAttrib.FontFamily, item.mFontFamily);
  181. AnnotAttribDir.Add(AnnotAttrib.FontStyle, item.mFontStyle);
  182. AnnotAttribDir.Add(AnnotAttrib.FontWeight, item.mFontWeight);
  183. PropertyPanel.UpdateAnnotAllAttribs(AnnotAttribDir);
  184. }
  185. }
  186. }
  187. //设置文本字重、样式
  188. private void FontStyleWeightChanged()
  189. {
  190. if(FontVm.CurrrentFontWeightStyle != null)
  191. {
  192. FontVm.UpdateFontWeight_Style();
  193. Dictionary<AnnotAttrib, object> AnnotAttribDir = new Dictionary<AnnotAttrib, object>();
  194. AnnotAttribDir.Add(AnnotAttrib.FontStyle, FontVm.FontStyleItem);
  195. AnnotAttribDir.Add(AnnotAttrib.FontWeight, FontVm.FontWeightItem);
  196. PropertyPanel.UpdateAnnotAllAttribs(AnnotAttribDir);
  197. }
  198. }
  199. private void SelectedFillOpacity(object obj)
  200. {
  201. if (obj != null)
  202. {
  203. BasicVm.FillOpacity = (double)obj;
  204. BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
  205. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  206. }
  207. }
  208. private void SelectedOpacityValue(object obj)
  209. {
  210. if (obj != null && obj is double)
  211. {
  212. BasicVm.FillOpacity = (double)obj;
  213. BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
  214. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  215. }
  216. }
  217. //设置字体大小
  218. private void FontSizeChanged()
  219. {
  220. if (FontVm.CurrentFontSize != null)
  221. {
  222. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontSize, FontVm.CurrentFontSize.Value);
  223. }
  224. }
  225. //设置字体样式
  226. private void FontFamilyChanged()
  227. {
  228. if (string.IsNullOrEmpty(FontVm.CurrentFontFamily.ValueStr) == false)
  229. {
  230. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontFamily, new FontFamily(FontVm.CurrentFontFamily.ValueStr));
  231. }
  232. }
  233. private void SelectedFillColorChange(object obj)
  234. {
  235. if (obj != null)
  236. {
  237. var colorValue = (Color)obj;
  238. if (colorValue != null)
  239. {
  240. BasicVm.FillColor = new SolidColorBrush(colorValue);
  241. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FillColor, colorValue);
  242. // BasicVm.FillColor.Opacity = BasicVmFillOpacity;
  243. }
  244. }
  245. }
  246. private void SelectedColor_Command(object obj)
  247. {
  248. if (obj != null)
  249. {
  250. var colorValue = (Color)obj;
  251. if (colorValue != null)
  252. {
  253. FontVm.FontColor = new SolidColorBrush(colorValue);
  254. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontColor, colorValue);
  255. }
  256. }
  257. }
  258. private void CustomFontStyle()
  259. {
  260. if (FontVm.CurrentPresetFont != null)
  261. {
  262. ContextMenu menu;
  263. if (FontVm.CurrentPresetFont.ValueStr == "custom")
  264. {
  265. menu = SelectAnnotContextMenu(false);
  266. }
  267. else
  268. {
  269. menu = SelectAnnotContextMenu(true);
  270. }
  271. if (menu != null)
  272. {
  273. menu.IsOpen = true;
  274. }
  275. }
  276. }
  277. private ContextMenu SelectAnnotContextMenu(bool isEnable)
  278. {
  279. var popMenu = App.Current.FindResource("CustomFontStyleFlyoutMenu") as ContextMenu;
  280. if (popMenu != null && popMenu.Items.Count == 2)
  281. {
  282. //用所选部分重新定义
  283. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  284. menuItem.IsEnabled = isEnable;
  285. menuItem.Command = ReDefineFontStyleCommand;
  286. //恢复默认预设样式
  287. menuItem = popMenu.Items[1] as MenuItem;
  288. menuItem.IsEnabled = isEnable;
  289. menuItem.Command = RestoreDefaultStyleCommand;
  290. }
  291. return popMenu;
  292. }
  293. public List<PresetFontItem> FontStyleList = new List<PresetFontItem>();
  294. private void ReDefineFontStyle()
  295. {
  296. var item = FontStyleList.FirstOrDefault(temp => temp.mTag == FontVm.CurrentPresetFont.ValueStr);
  297. if (FontVm.CurrentFontFamily.ValueStr == "Bold")
  298. {
  299. item.mFontStyle = FontStyles.Normal;
  300. item.mFontWeight = FontWeights.Bold;
  301. }
  302. else if (FontVm.CurrentFontFamily.ValueStr == "Regular")
  303. {
  304. item.mFontStyle = FontStyles.Normal;
  305. item.mFontWeight = FontWeights.Normal;
  306. }
  307. else
  308. {
  309. item.mFontStyle = FontStyles.Italic;
  310. item.mFontWeight = FontWeights.Bold;
  311. }
  312. item.mFontSize = (int)FontVm.CurrentFontSize.Value;
  313. }
  314. private void RestoreDefaultStyle()
  315. {
  316. // var item = FontStyleList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  317. }
  318. private void DateFormatChanged()
  319. {
  320. if(CurrrentDateFormat != null)
  321. {
  322. if (IsCheckedTime)
  323. UpdateDateFormatLists();
  324. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.NoteText, CurrrentDateFormat.Content);
  325. }
  326. }
  327. private void ShowTimeCheckedChnaged()
  328. {
  329. if (CurrrentDateFormat != null)
  330. {
  331. UpdateDateFormatLists();
  332. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.NoteText, CurrrentDateFormat.Content);
  333. }
  334. }
  335. public bool IsNavigationTarget(NavigationContext navigationContext)
  336. {
  337. return true;
  338. }
  339. public void OnNavigatedFrom(NavigationContext navigationContext)
  340. {
  341. BasicVm.IsMultiSelected = false;
  342. // IsCheckedTime = false;
  343. }
  344. public void OnNavigatedTo(NavigationContext navigationContext)
  345. {
  346. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  347. if (PropertyPanel != null)
  348. {
  349. AnnotEvent = PropertyPanel.AnnotEvent;
  350. Annot = PropertyPanel.annot as FreeTextAnnotArgs;
  351. LoadPropertyHandler?.Invoke(this, Annot);
  352. BasicVm.AnnotTypeTitle = "文字";
  353. BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected;
  354. IsFillFreeTextAnnot = PropertyPanel.IsTextFill;
  355. if (BasicVm.IsMultiSelected)
  356. {
  357. IsAttributeEquals();
  358. BasicVm.SetStrDashStyle("None");
  359. }
  360. else
  361. {
  362. GetAnnotProperty();
  363. }
  364. IsFillFreeTextAnnot = PropertyPanel.IsTextFill;
  365. }
  366. }
  367. private List<FreeTextAnnotArgs> ConvertLists()
  368. {
  369. List<FreeTextAnnotArgs> FreeTextLists = new List<FreeTextAnnotArgs>();
  370. foreach (var item in PropertyPanel.annotlists)
  371. {
  372. var itemFreeText = item as FreeTextAnnotArgs;
  373. if (itemFreeText != null)
  374. {
  375. FreeTextLists.Add(itemFreeText);
  376. }
  377. }
  378. if (FreeTextLists.Count != PropertyPanel.annotlists.Count)
  379. return null;
  380. else
  381. return FreeTextLists;
  382. }
  383. private void IsAttributeEquals()
  384. {
  385. var list = ConvertLists();
  386. if (list != null)
  387. {
  388. var temp = list[0];
  389. Dictionary<string, bool> isNoEqualsDir = new Dictionary<string, bool>();
  390. isNoEqualsDir.Add("FontFamily", false);
  391. isNoEqualsDir.Add("FontSize", false);
  392. isNoEqualsDir.Add("FontStyleFontWeight", false);
  393. isNoEqualsDir.Add("FontColor", false);
  394. isNoEqualsDir.Add("FillColor", false);
  395. foreach (var item in list)
  396. {
  397. if (item == list[0])
  398. continue;
  399. if (temp.FontFamily != item.FontFamily)
  400. {
  401. isNoEqualsDir["FontFamily"] = true;
  402. FontVm.CurrentFontFamily = null;
  403. }
  404. if (temp.FontSize != item.FontSize)
  405. {
  406. isNoEqualsDir["FontSize"] = true;
  407. FontVm.CurrentFontSize = null;
  408. }
  409. if (temp.FontStyle != item.FontStyle || temp.FontWeight != item.FontWeight)
  410. {
  411. isNoEqualsDir["FontStyleFontWeight"] = true;
  412. FontVm.CurrrentFontWeightStyle = null;
  413. }
  414. if (isNoEqualsDir["FontFamily"] == true || isNoEqualsDir["FontSize"] == true || isNoEqualsDir["FontStyleFontWeight"] == true)
  415. {
  416. FontVm.CurrentPresetFont = null;
  417. }
  418. if (isNoEqualsDir["FontColor"] == false)
  419. {
  420. if (temp.FontColor.A != item.FontColor.A || temp.FontColor.R != item.FontColor.R || temp.FontColor.G != item.FontColor.G || temp.FontColor.B != item.FontColor.B)
  421. {
  422. FontVm.FontColor = new SolidColorBrush(Colors.Transparent);
  423. isNoEqualsDir["FontColor"] = true;
  424. }
  425. }
  426. if (isNoEqualsDir["FillColor"] == false)
  427. {
  428. if (temp.BgColor.A != item.BgColor.A || temp.BgColor.R != item.BgColor.R || temp.BgColor.G != item.BgColor.G || temp.BgColor.B != item.BgColor.B)
  429. {
  430. BasicVm.FillColor = new SolidColorBrush(Colors.Transparent);
  431. isNoEqualsDir["FillColor"] = true;
  432. }
  433. }
  434. }
  435. ////以下是多选注释的属性相等时
  436. if (isNoEqualsDir["FontFamily"] == false)
  437. {
  438. FontVm.GetCurrentFontFamily(temp.FontFamily.ToString(), temp.FontFamily.ToString());
  439. }
  440. if (isNoEqualsDir["FontSize"] == false)
  441. {
  442. FontVm.GetCurrentFontSize((int)temp.FontSize);
  443. }
  444. if (isNoEqualsDir["FontStyleFontWeight"] == false)
  445. {
  446. FontVm.GetFontWeights_Style(temp.FontStyle, temp.FontWeight);
  447. }
  448. if (isNoEqualsDir["FontColor"] == false)
  449. {
  450. FontVm.FontColor = new SolidColorBrush(temp.FontColor);
  451. }
  452. if (isNoEqualsDir["FillColor"] == false)
  453. {
  454. FontVm.FontColor = new SolidColorBrush(temp.BgColor);
  455. }
  456. }
  457. }
  458. private void GetAnnotProperty()
  459. {
  460. BasicVm.FillColor = new SolidColorBrush(Annot.BgColor);
  461. BasicVm.FillOpacity = Annot.Transparency;
  462. FontVm.FontColor = new SolidColorBrush(Annot.FontColor);
  463. FontVm.GetCurrentFontSize((int)Annot.FontSize);
  464. FontVm.GetCurrentFontFamily(Annot.FontFamily.ToString(), Annot.FontFamily.ToString());
  465. FontVm.GetFontWeights_Style(Annot.FontStyle, Annot.FontWeight);
  466. GetTimeFormat();
  467. string str = "";
  468. if (Annot.Align == TextAlignment.Left)
  469. str = "AlignLeft";
  470. else if (Annot.Align == TextAlignment.Center)
  471. str = "AlignCenter";
  472. else if (Annot.Align == TextAlignment.Right)
  473. str = "AlignRight";
  474. else if(Annot.Align == TextAlignment.Justify)
  475. str = "AlignJustify";
  476. else
  477. str = "None";
  478. BasicVm.SetStrDashStyle(str);
  479. }
  480. private void GetTimeFormat()
  481. {
  482. if (IsFillFreeTextAnnot == false)
  483. return;
  484. if(string.IsNullOrEmpty(Annot.TextContent))
  485. {
  486. if (CurrrentDateFormat != null)
  487. {
  488. if (IsCheckedTime)
  489. UpdateDateFormatLists();
  490. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.NoteText, CurrrentDateFormat.Content);
  491. }
  492. return;
  493. }
  494. DateTime dtDate;
  495. bool isTime = false;
  496. foreach (var item in DateFormatItems)
  497. {
  498. var strFormat = "";
  499. var strArr = Annot.TextContent.Split(' ');
  500. if (strArr != null && strArr.Count() == 2)
  501. {
  502. strFormat = item.ValueStr + " HH:mm:ss";
  503. }
  504. else
  505. {
  506. strFormat = item.ValueStr;
  507. }
  508. if (DateTime.TryParseExact(Annot.TextContent, strFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal, out dtDate))
  509. {
  510. if (strArr != null && strArr.Count() == 2)
  511. {
  512. isTime = true;
  513. }
  514. foreach (var item2 in DateFormatItems)
  515. {
  516. string stadate = System.DateTime.Now.ToString(strFormat);
  517. item.SetContent(stadate);
  518. }
  519. CurrrentDateFormat = new ComboDataItem(item.ValueStr, item.Content);
  520. break;
  521. }
  522. }
  523. IsCheckedTime = isTime;
  524. }
  525. }
  526. }