TextEditPropertyViewModel.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Microsoft.Win32;
  5. using PDF_Master.CustomControl.CompositeControl;
  6. using PDF_Master.Helper;
  7. using PDF_Master.Model;
  8. using PDF_Master.Model.AnnotPanel;
  9. using PDF_Master.Model.PropertyPanel.AnnotPanel;
  10. using PDF_Master.ViewModels.Tools.AnnotManager;
  11. using PDFSettings;
  12. using Prism.Commands;
  13. using Prism.Mvvm;
  14. using Prism.Regions;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Input;
  23. using System.Windows.Media;
  24. namespace PDF_Master.ViewModels.PropertyPanel.PDFEdit
  25. {
  26. public class TextEditPropertyViewModel : PDFEditVM, INavigationAware
  27. {
  28. #region 属性
  29. public event EventHandler ClearCheckedAglin;
  30. public TextEditPropertyViewModel()
  31. {
  32. InitVariable();
  33. InitCommand();
  34. }
  35. private void InitVariable()
  36. {
  37. InitBaseVariable();
  38. }
  39. private bool isSelectedEmpty;
  40. public bool IsSelectedEmpty
  41. {
  42. get { return isSelectedEmpty; }
  43. set { SetProperty(ref isSelectedEmpty, value); }
  44. }
  45. private string title;
  46. public string Title
  47. {
  48. get { return title; }
  49. set
  50. {
  51. SetProperty(ref title, value);
  52. }
  53. }
  54. private FontBoardVm _fontVm = new FontBoardVm(true);
  55. public FontBoardVm FontVm
  56. {
  57. get { return _fontVm; }
  58. set { SetProperty(ref _fontVm, value); }
  59. }
  60. private ComPDFKit.PDFPage.Edit.TextAlignType _textAlign;
  61. public ComPDFKit.PDFPage.Edit.TextAlignType TextAlign
  62. {
  63. get { return _textAlign; }
  64. set { _textAlign = value; }
  65. }
  66. #endregion
  67. #region Command
  68. public DelegateCommand AddTextCommand { get; set; }
  69. public DelegateCommand AddImgCommand { get; set; }
  70. public DelegateCommand<object> SelectedColorCommand { get; set; }
  71. public DelegateCommand<object> SelectedFontStyleCommand { get; set; }
  72. public DelegateCommand<object> FontSizeChangedCommand { get; set; }
  73. public DelegateCommand<object> TextAlignCheckedCommand { get; set; }
  74. //进入属性面板编辑
  75. public DelegateCommand EditTextModeCommand { get; set; }
  76. public DelegateCommand FontFamilyChangedCommand { get; set; }
  77. public DelegateCommand CustomFontStyleCommand { get; set; }
  78. public DelegateCommand FontStyleWeightChangedCommand { get; set; }
  79. public DelegateCommand SelectedPresetFontCommand { get; set; }
  80. //重定义
  81. public DelegateCommand ReDefineFontStyleCommand { get; set; }
  82. public DelegateCommand RestoreDefaultStyleCommand { get; set; }
  83. public DelegateCommand<object> LayoutAlignCheckedCommand { get; set; }
  84. #endregion
  85. #region 初始化
  86. private void InitCommand()
  87. {
  88. //预设文本样式
  89. SelectedPresetFontCommand = new DelegateCommand(SelectedPresetFont);
  90. //字体
  91. FontFamilyChangedCommand = new DelegateCommand(FontFamilyChanged);
  92. //文本字重、字体
  93. FontStyleWeightChangedCommand = new DelegateCommand(FontStyleWeightChanged);
  94. //添加文本
  95. AddTextCommand = new DelegateCommand(AddText);
  96. //添加图片
  97. AddImgCommand = new DelegateCommand(AddImg);
  98. //选择颜色
  99. SelectedColorCommand = new DelegateCommand<object>(SelectedColor);
  100. //选择字体样式
  101. SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
  102. //大小
  103. FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged);
  104. //内容对齐
  105. TextAlignCheckedCommand = new DelegateCommand<object>(TextAlignChecked);
  106. //编辑模式
  107. EditTextModeCommand = new DelegateCommand(EditTextMode);
  108. //自定义
  109. CustomFontStyleCommand = new DelegateCommand(CustomFontStyle);
  110. //重新定义
  111. ReDefineFontStyleCommand = new DelegateCommand(ReDefineFontStyle);
  112. //恢复默认
  113. RestoreDefaultStyleCommand = new DelegateCommand(RestoreDefaultStyle);
  114. //文本对齐
  115. LayoutAlignCheckedCommand = new DelegateCommand<object>(LayoutAlignChecked);
  116. }
  117. #endregion
  118. #region 文本处理逻辑
  119. private void AddText()
  120. {
  121. //设置对应创建模式
  122. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  123. }
  124. private void AddImg()
  125. {
  126. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  127. }
  128. private void EditTextMode()
  129. {
  130. }
  131. /// <summary>
  132. /// 从预设样式设置文本
  133. /// </summary>
  134. private void SelectedPresetFont()
  135. {
  136. if (CurrentPresetFont != null)
  137. {
  138. var itemComboxItem = CurrentPresetFont;
  139. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == itemComboxItem.ValueStr);
  140. if (item != null)
  141. {
  142. if (item.mTag != "Custom")
  143. {
  144. GetCurrentFontSize(item.mFontSize);
  145. if (item.mFontFamily != null)
  146. {
  147. GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
  148. }
  149. GetFontWeights_Style(item.mFontStyle, item.mFontWeight);
  150. //CurrentFontSize = new ComboDataItem(TextEditEvent.FontSize);
  151. //CurrentFontFamily = new ComboDataItem(TextEditEvent.FontName, TextEditEvent.FontName);
  152. //FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
  153. //FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
  154. }
  155. }
  156. }
  157. }
  158. /// <summary>
  159. /// 自定义文字样式
  160. /// </summary>
  161. private void CustomFontStyle()
  162. {
  163. if (CurrentPresetFont != null)
  164. {
  165. ContextMenu menu;
  166. if (CurrentPresetFont.ValueStr == "Custom")
  167. {
  168. menu = SelectAnnotContextMenu(false);
  169. }
  170. else
  171. {
  172. menu = SelectAnnotContextMenu(false);
  173. var defaultlists = TextFont.GetPresetFontStyle();
  174. var defaulItem = defaultlists.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  175. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  176. if (currentItem.mTag != "Custom")
  177. {
  178. if (FontWeightItem != defaulItem.mFontWeight ||
  179. FontStyleItem != defaulItem.mFontStyle ||
  180. CurrentFontSize.Value != defaulItem.mFontSize ||
  181. CurrentFontFamily.ValueStr != defaulItem.mFontFamily.Source)
  182. {
  183. menu = SelectAnnotContextMenu(true);
  184. }
  185. }
  186. }
  187. if (menu != null)
  188. {
  189. menu.IsOpen = true;
  190. }
  191. }
  192. }
  193. //设置字体样式
  194. private void FontFamilyChanged()
  195. {
  196. if (CurrentPresetFont != null)
  197. {
  198. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  199. if (currentItem.mTag != "Custom")
  200. {
  201. foreach (var item in PresetFontList)
  202. {
  203. if (item.mTag == currentItem.mTag)
  204. {
  205. if (CurrentFontFamily.ValueStr != item.mFontFamily.Source)
  206. {
  207. string txt = string.Format($" * {currentItem.mTagContent}");
  208. if (Title == txt)
  209. {
  210. Title = "";
  211. }
  212. Title = txt;
  213. break;
  214. }
  215. else
  216. {
  217. if (FontWeightItem == item.mFontWeight && FontStyleItem == item.mFontStyle && CurrentFontSize.Value == item.mFontSize && CurrentFontFamily.ValueStr == item.mFontFamily.Source)
  218. {
  219. string txt = currentItem.mTagContent;
  220. if (Title == txt)
  221. {
  222. Title = "";
  223. }
  224. Title = txt;
  225. }
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232. /// <summary>
  233. /// 设置文本字重、样式
  234. /// </summary>
  235. private void FontStyleWeightChanged()
  236. {
  237. if (CurrrentFontWeightStyle != null)
  238. {
  239. UpdateFontWeight_Style();
  240. //FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
  241. //FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
  242. if (CurrentPresetFont != null)
  243. {
  244. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  245. if (currentItem.mTag != "Custom")
  246. {
  247. foreach (var item in PresetFontList)
  248. {
  249. if (item.mTag == currentItem.mTag)
  250. {
  251. if (FontWeightItem != item.mFontWeight || FontStyleItem != item.mFontStyle)
  252. {
  253. string txt = string.Format($" * {currentItem.mTagContent}");
  254. if (Title == txt)
  255. {
  256. Title = "";
  257. }
  258. Title = txt;
  259. break;
  260. }
  261. else
  262. {
  263. if (FontWeightItem == item.mFontWeight && FontStyleItem == item.mFontStyle && CurrentFontSize.Value == item.mFontSize && CurrentFontFamily.ValueStr == item.mFontFamily.Source)
  264. {
  265. string txt = currentItem.mTagContent;
  266. if (Title == txt)
  267. {
  268. Title = "";
  269. }
  270. Title = txt;
  271. }
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. }
  279. /// <summary>
  280. /// 用所选部分重新定义
  281. /// </summary>
  282. private void ReDefineFontStyle()
  283. {
  284. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  285. if (item == null) return;
  286. item.mFontFamily = new FontFamily(CurrentFontFamily.ValueStr);
  287. if (CurrrentFontWeightStyle.ValueStr == "Bold")
  288. {
  289. item.mFontStyle = FontStyles.Normal;
  290. item.mFontWeight = FontWeights.Bold;
  291. }
  292. else if (CurrrentFontWeightStyle.ValueStr == "Regular")
  293. {
  294. item.mFontStyle = FontStyles.Normal;
  295. item.mFontWeight = FontWeights.Normal;
  296. }
  297. else if (CurrrentFontWeightStyle.ValueStr == "Italic")
  298. {
  299. item.mFontStyle = FontStyles.Italic;
  300. item.mFontWeight = FontWeights.Normal;
  301. }
  302. else
  303. {
  304. item.mFontStyle = FontStyles.Italic;
  305. item.mFontWeight = FontWeights.Bold;
  306. }
  307. item.mFontSize = (int)CurrentFontSize.Value;
  308. CurrentPresetFont = new ComboDataItem(item.mTag, item.mTagContent);
  309. SelectedPresetFont();
  310. //更改后 保存到本地缓存
  311. TextFont.SavePresetFontList(PresetFontList);
  312. }
  313. //重置定义
  314. private void RestoreDefaultStyle()
  315. {
  316. var defaultlists = TextFont.GetPresetFontStyle();
  317. if (CurrentPresetFont.ValueStr != "Custom")
  318. {
  319. var defaulItem = defaultlists.FirstOrDefault(temp => temp.mTag ==CurrentPresetFont.ValueStr);
  320. if (defaulItem != null)
  321. {
  322. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  323. if (currentItem != null)
  324. {
  325. currentItem.mTagContent = defaulItem.mTagContent;
  326. currentItem.mFontStyle = defaulItem.mFontStyle;
  327. currentItem.mFontWeight = defaulItem.mFontWeight;
  328. currentItem.mFontFamily = defaulItem.mFontFamily;
  329. currentItem.mFontSize = defaulItem.mFontSize;
  330. GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
  331. GetCurrentFontSize(currentItem.mFontSize);
  332. GetFontWeights_Style(currentItem.mFontStyle, currentItem.mFontWeight);
  333. CurrentPresetFont = new ComboDataItem(defaulItem.mTag, defaulItem.mTagContent);
  334. SelectedPresetFont();
  335. TextFont.SavePresetFontList(PresetFontList);
  336. }
  337. }
  338. }
  339. }
  340. //设置多选对齐方式
  341. private void LayoutAlignChecked(object obj)
  342. {
  343. if (obj != null)
  344. {
  345. if (obj != null)
  346. {
  347. switch ((string)obj)
  348. {
  349. case "AlignLeft":
  350. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  351. break;
  352. case "AlignHorizonCenter":
  353. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  354. break;
  355. case "AlignRight":
  356. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  357. break;
  358. case "DistributeHorizontal":
  359. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  360. break;
  361. case "AlignTop":
  362. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  363. break;
  364. case "AlignVerticalCenter":
  365. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  366. break;
  367. case "AlignBottom":
  368. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  369. break;
  370. case "DistributeVertical":
  371. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  372. break;
  373. }
  374. }
  375. }
  376. }
  377. //设置文本框内对齐方式
  378. private void TextAlignChecked(object obj)
  379. {
  380. if ((string)obj != null && TextEditEvent != null)
  381. {
  382. switch ((string)obj)
  383. {
  384. case "AlignLeft":
  385. GetAnnotAlign(TextAlignment.Left);
  386. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignLeft;
  387. break;
  388. case "AlignCenter":
  389. GetAnnotAlign(TextAlignment.Center);
  390. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignMiddle;
  391. break;
  392. case "AlignRight":
  393. GetAnnotAlign(TextAlignment.Right);
  394. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignRight;
  395. break;
  396. case "Align":
  397. GetAnnotAlign(TextAlignment.Justify);
  398. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignNone;
  399. break;
  400. }
  401. TextAlign = TextEditEvent.TextAlign;
  402. TextEditEvent.UpdatePDFEditByEventArgs();
  403. }
  404. }
  405. //设置文本内容对齐属性面板显示
  406. private void GetAnnotAlign(TextAlignment align)
  407. {
  408. switch (align)
  409. {
  410. case TextAlignment.Left:
  411. StrTextAlign = "Left";
  412. break;
  413. case TextAlignment.Center:
  414. StrTextAlign = "Center";
  415. break;
  416. case TextAlignment.Right:
  417. StrTextAlign = "Right";
  418. break;
  419. case TextAlignment.Justify:
  420. StrTextAlign = "Justify";
  421. break;
  422. default:
  423. StrTextAlign = "None";
  424. break;
  425. }
  426. }
  427. //设置文本颜色
  428. private void SelectedColor(object obj)
  429. {
  430. if (obj != null)
  431. {
  432. var colorValue = (Color)obj;
  433. if (colorValue != null)
  434. {
  435. SelectColor = new SolidColorBrush(colorValue);
  436. }
  437. }
  438. }
  439. //设置文本样式
  440. private void SelectedFontStyle(object obj)
  441. {
  442. if (obj != null && (PresetFontItem)obj != null)
  443. {
  444. var item = (PresetFontItem)obj;
  445. }
  446. }
  447. //设置字体大小
  448. private void FontSizeChanged(object obj)
  449. {
  450. //if (CurrentFontSize != null)
  451. //{
  452. // CurrentFontSize = new ComboDataItem(TextEditEvent.FontSize);
  453. //}
  454. if (CurrentPresetFont != null)
  455. {
  456. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  457. if (currentItem.mTag != "Custom")
  458. {
  459. foreach (var item in PresetFontList)
  460. {
  461. if (item.mTag == currentItem.mTag)
  462. {
  463. if (CurrentFontSize.Value != item.mFontSize)
  464. {
  465. string txt = string.Format($" * {currentItem.mTagContent}");
  466. if (Title == txt)
  467. {
  468. Title = "";
  469. }
  470. Title = txt;
  471. break;
  472. }
  473. else
  474. {
  475. if (FontWeightItem == item.mFontWeight && FontStyleItem == item.mFontStyle && CurrentFontSize.Value == item.mFontSize && CurrentFontFamily.ValueStr == item.mFontFamily.Source)
  476. {
  477. string txt = currentItem.mTagContent;
  478. if (Title == txt)
  479. {
  480. Title = "";
  481. }
  482. Title = txt;
  483. }
  484. }
  485. }
  486. }
  487. }
  488. }
  489. }
  490. #endregion
  491. #region 右键菜单
  492. //点击空白处时
  493. private ContextMenu EmptyStateMenu(object sender)
  494. {
  495. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  496. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  497. //粘贴
  498. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  499. //添加文本
  500. customMenu.SetMenuBinding(1, AddTextCommand);
  501. //添加图像
  502. customMenu.SetMenuBinding(2, AddImgCommand);
  503. return popMenu;
  504. }
  505. #endregion
  506. #region 编辑PDF内容触发的事件
  507. /// <summary>
  508. /// 右键菜单的事件
  509. /// </summary>
  510. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  511. {
  512. //if (e == null)
  513. // return;
  514. //switch (e.CommandType)
  515. //{
  516. // case CommandType.Context:
  517. // if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  518. // {
  519. // e.PopupMenu = EmptyStateMenu(sender);
  520. // }
  521. // break;
  522. // default:
  523. // e.DoCommand();
  524. // break;
  525. //}
  526. //if (e.PopupMenu != null)
  527. //{
  528. // e.Handle = true;
  529. //}
  530. }
  531. /// <summary>
  532. /// 更多菜单
  533. /// </summary>
  534. /// <param name="isEnable"></param>
  535. /// <returns></returns>
  536. private ContextMenu SelectAnnotContextMenu(bool isEnable)
  537. {
  538. var popMenu = App.Current.FindResource("CustomFontStyleFlyoutMenu") as ContextMenu;
  539. if (popMenu != null && popMenu.Items.Count == 2)
  540. {
  541. //用所选部分重新定义
  542. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  543. //恢复默认预设样式
  544. menuItem = popMenu.Items[0] as MenuItem;
  545. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  546. menuItem.IsEnabled = false;
  547. if (currentItem.mTag != "Custom")
  548. {
  549. if (FontWeightItem != currentItem.mFontWeight ||
  550. FontStyleItem != currentItem.mFontStyle ||
  551. CurrentFontSize.Value != currentItem.mFontSize ||
  552. CurrentFontFamily.ValueStr != currentItem.mFontFamily.Source)
  553. {
  554. menuItem.IsEnabled = true;
  555. menuItem.Command = ReDefineFontStyleCommand;
  556. }
  557. }
  558. //恢复默认预设样式
  559. menuItem = popMenu.Items[1] as MenuItem;
  560. menuItem.IsEnabled = isEnable;
  561. menuItem.Command = RestoreDefaultStyleCommand;
  562. }
  563. return popMenu;
  564. }
  565. #endregion
  566. protected List<PDFEditEvent> TextEditEventList;
  567. public void OnNavigatedTo(NavigationContext navigationContext)
  568. {
  569. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  570. navigationContext.Parameters.TryGetValue<List<PDFEditEvent>>(ParameterNames.AnnotEvent, out TextEditEventList);
  571. if (PDFViewer != null)
  572. {
  573. if (TextEditEventList != null && TextEditEventList.Count > 0)
  574. {
  575. TextEditEvent = TextEditEventList[0];
  576. if(TextEditEvent.FontName!=""&& TextEditEvent.FontName !=null)
  577. {
  578. GetPDFEdit();
  579. }
  580. if (TextEditEventList.Count > 1)
  581. IsSelectedEmpty = true;
  582. else
  583. IsSelectedEmpty = false;
  584. if (TextEditEventList.Count == 2)
  585. {
  586. IsLayoutAlign = true;
  587. IsLayoutAvgAlign = false;
  588. }
  589. else if (TextEditEventList.Count > 2)
  590. {
  591. IsLayoutAlign = true;
  592. IsLayoutAvgAlign = true;
  593. }
  594. else
  595. {
  596. IsLayoutAlign = false;
  597. IsLayoutAvgAlign = false;
  598. }
  599. }
  600. else if(TextEditEventList==null|| TextEditEvent.FontName == null|| TextEditEvent.FontName == "")
  601. {
  602. var defaultlists = TextFont.GetPresetFontStyle();
  603. //var defaulItem = defaultlists.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  604. //if (defaulItem != null)
  605. //{
  606. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == "Custom");
  607. if (currentItem != null)
  608. {
  609. //currentItem.mTagContent = defaulItem.mTagContent;
  610. //currentItem.mFontStyle = defaulItem.mFontStyle;
  611. //currentItem.mFontWeight = defaulItem.mFontWeight;
  612. //currentItem.mFontFamily = defaulItem.mFontFamily;
  613. //currentItem.mFontSize = defaulItem.mFontSize;
  614. GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
  615. GetCurrentFontSize(currentItem.mFontSize);
  616. GetFontWeights_Style(currentItem.mFontStyle, currentItem.mFontWeight);
  617. SelectedPresetFont();
  618. SelectColor = new SolidColorBrush(Colors.Black);
  619. CurrentFontSize = new ComboDataItem(currentItem.mFontSize);
  620. CurrentPresetFont = new ComboDataItem(currentItem.mTag, currentItem.mTagContent);
  621. CurrentFontFamily = new ComboDataItem(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
  622. FontStyleItem = FontStyles.Normal;
  623. FontWeightItem = FontWeights.Normal;
  624. //}
  625. }
  626. }
  627. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  628. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  629. ChangedValue -= FontMode_ChangedValue;
  630. ChangedValue += FontMode_ChangedValue;
  631. IsCanSave = true;
  632. }
  633. }
  634. //文本内容改变触发
  635. private void FontMode_ChangedValue(object sender, FontSetModeType e)
  636. {
  637. if (sender != null && TextEditEvent != null)
  638. {
  639. switch (e)
  640. {
  641. case FontSetModeType.PresetFontStyes:
  642. if (PresetFontList != null && sender is string == true)
  643. {
  644. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == (string)sender);
  645. if (item != null && TextEditEvent != null)
  646. {
  647. if (TextEditEventList.Count == 1 && IsSelectedEmpty == false)
  648. {
  649. TextEditEvent.FontName = item.mFontFamily != null ? item.mFontFamily.Source : string.Empty;
  650. TextEditEvent.FontSize = item.mFontSize;
  651. TextEditEvent.IsBold = item.mFontWeight == FontWeights.Bold;
  652. TextEditEvent.IsItalic = item.mFontStyle == FontStyles.Italic;
  653. TextEditEvent.UpdatePDFEditByEventArgs();
  654. }
  655. else
  656. {
  657. foreach (var itemlist in TextEditEventList)
  658. {
  659. itemlist.FontName = item.mFontFamily != null ? item.mFontFamily.Source : string.Empty;
  660. itemlist.FontSize = item.mFontSize;
  661. itemlist.IsBold = item.mFontWeight == FontWeights.Bold;
  662. itemlist.IsItalic = item.mFontStyle == FontStyles.Italic;
  663. itemlist.UpdatePDFEditByEventArgs();
  664. }
  665. }
  666. }
  667. }
  668. break;
  669. case FontSetModeType.FontFamilys:
  670. if (sender is string == true)
  671. {
  672. TextEditEvent.FontName = (string)sender;
  673. TextEditEvent.UpdatePDFEditByEventArgs();
  674. }
  675. break;
  676. case FontSetModeType.FontSizes:
  677. if (sender is double == true && (double)sender > 0 && TextEditEvent.FontSize > 0)
  678. {
  679. TextEditEvent.FontSize = (double)sender;
  680. TextEditEvent.UpdatePDFEditByEventArgs();
  681. }
  682. break;
  683. case FontSetModeType.FontWeight_Style:
  684. UpdateFontWeight_Style();
  685. TextEditEvent.IsBold = FontWeightItem == FontWeights.Bold;
  686. TextEditEvent.IsItalic = FontStyleItem == FontStyles.Italic;
  687. TextEditEvent.UpdatePDFEditByEventArgs();
  688. break;
  689. case FontSetModeType.FontColor:
  690. if (sender is Color == true)
  691. {
  692. var setColor = (Color)sender;
  693. var eventColor = TextEditEvent.FontColor;
  694. bool isok = eventColor.A != setColor.A || eventColor.B != setColor.B ||
  695. eventColor.G != setColor.G || eventColor.R != setColor.R;
  696. if (isok)
  697. {
  698. TextEditEvent.FontColor = setColor;
  699. TextEditEvent.UpdatePDFEditByEventArgs();
  700. }
  701. }
  702. break;
  703. case FontSetModeType.TextAlignment:
  704. break;
  705. }
  706. }
  707. }
  708. //获取文本参数
  709. private void GetPDFEdit()
  710. {
  711. SelectColor = new SolidColorBrush(TextEditEvent.FontColor);
  712. CurrentFontSize =new ComboDataItem(TextEditEvent.FontSize);
  713. CurrentFontFamily = new ComboDataItem(TextEditEvent.FontName, TextEditEvent.FontName);
  714. FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
  715. FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
  716. GetFontWeights_Style(FontStyleItem, FontWeightItem);
  717. //判断样式列表中是否存在对应样式
  718. bool isExist = false;
  719. foreach (var item in PresetFontList)
  720. {
  721. if (TextEditEvent.FontSize == item.mFontSize && TextEditEvent.IsBold == (item.mFontWeight == FontWeights.Bold) && TextEditEvent.IsItalic == (item.mFontStyle == FontStyles.Italic)
  722. && (TextEditEvent.FontName == item.mFontFamily.Source || TextEditEvent.FontName == "Arial" && item.mFontFamily.Source == "Helvetica")
  723. )
  724. {
  725. if (item.mTag != "Custom")
  726. {
  727. CurrentPresetFont = new ComboDataItem(item.mTag, item.mTagContent);
  728. isExist = true;
  729. }
  730. break;
  731. }
  732. }
  733. if (isExist == false)
  734. {
  735. FontVm.CurrentPresetFont = new ComboDataItem("Custom", "Custom");
  736. CurrentPresetFont = new ComboDataItem("Custom", "Custom");
  737. GetCurrentFontSize((int)TextEditEvent.FontSize);
  738. if (TextEditEvent.FontName == ""|| TextEditEvent.FontName == null)
  739. {
  740. GetCurrentFontFamily("Helvetica", "Helvetica");
  741. }
  742. else
  743. {
  744. GetCurrentFontFamily(TextEditEvent.FontName, TextEditEvent.FontName);
  745. }
  746. GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal);
  747. }
  748. else
  749. {
  750. if (CurrentPresetFont != null)
  751. {
  752. var itemComboxItem = CurrentPresetFont;
  753. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == itemComboxItem.ValueStr);
  754. if (item != null)
  755. {
  756. if (item.mTag != "Custom")
  757. {
  758. GetCurrentFontSize(item.mFontSize);
  759. if (item.mFontFamily != null)
  760. {
  761. GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
  762. }
  763. GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal);
  764. }
  765. }
  766. }
  767. }
  768. //初始化传入的对齐方式
  769. switch (TextEditEvent.TextAlign.ToString())
  770. {
  771. case "AlignLeft":
  772. GetAnnotAlign(TextAlignment.Left);
  773. break;
  774. case "AlignCenter":
  775. GetAnnotAlign(TextAlignment.Center);
  776. break;
  777. case "AlignRight":
  778. GetAnnotAlign(TextAlignment.Right);
  779. break;
  780. case "Align":
  781. GetAnnotAlign(TextAlignment.Justify);
  782. break;
  783. }
  784. TextAlign = TextEditEvent.TextAlign;
  785. TextEditEvent.UpdatePDFEditByEventArgs();
  786. }
  787. public bool IsNavigationTarget(NavigationContext navigationContext) { return true; }
  788. public void OnNavigatedFrom(NavigationContext navigationContext)
  789. {
  790. IsCanSave = false;
  791. TextEditEvent = null;
  792. IsSelectedEmpty = false;
  793. ClearCheckedAglin?.Invoke(null, null);
  794. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  795. ChangedValue -= FontMode_ChangedValue;
  796. }
  797. }
  798. }