TextEditPropertyViewModel.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. //重置定义
  312. private void RestoreDefaultStyle()
  313. {
  314. var defaultlists = TextFont.GetPresetFontStyle();
  315. if (CurrentPresetFont.ValueStr != "Custom")
  316. {
  317. var defaulItem = defaultlists.FirstOrDefault(temp => temp.mTag ==CurrentPresetFont.ValueStr);
  318. if (defaulItem != null)
  319. {
  320. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  321. if (currentItem != null)
  322. {
  323. currentItem.mTagContent = defaulItem.mTagContent;
  324. currentItem.mFontStyle = defaulItem.mFontStyle;
  325. currentItem.mFontWeight = defaulItem.mFontWeight;
  326. currentItem.mFontFamily = defaulItem.mFontFamily;
  327. currentItem.mFontSize = defaulItem.mFontSize;
  328. GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
  329. GetCurrentFontSize(currentItem.mFontSize);
  330. GetFontWeights_Style(currentItem.mFontStyle, currentItem.mFontWeight);
  331. CurrentPresetFont = new ComboDataItem(defaulItem.mTag, defaulItem.mTagContent);
  332. SelectedPresetFont();
  333. }
  334. }
  335. }
  336. }
  337. //设置多选对齐方式
  338. private void LayoutAlignChecked(object obj)
  339. {
  340. if (obj != null)
  341. {
  342. if (obj != null)
  343. {
  344. switch ((string)obj)
  345. {
  346. case "AlignLeft":
  347. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  348. break;
  349. case "AlignHorizonCenter":
  350. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  351. break;
  352. case "AlignRight":
  353. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  354. break;
  355. case "DistributeHorizontal":
  356. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  357. break;
  358. case "AlignTop":
  359. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  360. break;
  361. case "AlignVerticalCenter":
  362. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  363. break;
  364. case "AlignBottom":
  365. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  366. break;
  367. case "DistributeVertical":
  368. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  369. break;
  370. }
  371. }
  372. }
  373. }
  374. //设置文本框内对齐方式
  375. private void TextAlignChecked(object obj)
  376. {
  377. if ((string)obj != null && TextEditEvent != null)
  378. {
  379. switch ((string)obj)
  380. {
  381. case "AlignLeft":
  382. GetAnnotAlign(TextAlignment.Left);
  383. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignLeft;
  384. break;
  385. case "AlignCenter":
  386. GetAnnotAlign(TextAlignment.Center);
  387. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignMiddle;
  388. break;
  389. case "AlignRight":
  390. GetAnnotAlign(TextAlignment.Right);
  391. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignRight;
  392. break;
  393. case "Align":
  394. GetAnnotAlign(TextAlignment.Justify);
  395. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignNone;
  396. break;
  397. }
  398. TextAlign = TextEditEvent.TextAlign;
  399. TextEditEvent.UpdatePDFEditByEventArgs();
  400. }
  401. }
  402. //设置文本内容对齐属性面板显示
  403. private void GetAnnotAlign(TextAlignment align)
  404. {
  405. switch (align)
  406. {
  407. case TextAlignment.Left:
  408. StrTextAlign = "Left";
  409. break;
  410. case TextAlignment.Center:
  411. StrTextAlign = "Center";
  412. break;
  413. case TextAlignment.Right:
  414. StrTextAlign = "Right";
  415. break;
  416. case TextAlignment.Justify:
  417. StrTextAlign = "Justify";
  418. break;
  419. default:
  420. StrTextAlign = "None";
  421. break;
  422. }
  423. }
  424. //设置文本颜色
  425. private void SelectedColor(object obj)
  426. {
  427. if (obj != null)
  428. {
  429. var colorValue = (Color)obj;
  430. if (colorValue != null)
  431. {
  432. SelectColor = new SolidColorBrush(colorValue);
  433. }
  434. }
  435. }
  436. //设置文本样式
  437. private void SelectedFontStyle(object obj)
  438. {
  439. if (obj != null && (PresetFontItem)obj != null)
  440. {
  441. var item = (PresetFontItem)obj;
  442. }
  443. }
  444. //设置字体大小
  445. private void FontSizeChanged(object obj)
  446. {
  447. //if (CurrentFontSize != null)
  448. //{
  449. // CurrentFontSize = new ComboDataItem(TextEditEvent.FontSize);
  450. //}
  451. if (CurrentPresetFont != null)
  452. {
  453. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  454. if (currentItem.mTag != "Custom")
  455. {
  456. foreach (var item in PresetFontList)
  457. {
  458. if (item.mTag == currentItem.mTag)
  459. {
  460. if (CurrentFontSize.Value != item.mFontSize)
  461. {
  462. string txt = string.Format($" * {currentItem.mTagContent}");
  463. if (Title == txt)
  464. {
  465. Title = "";
  466. }
  467. Title = txt;
  468. break;
  469. }
  470. else
  471. {
  472. if (FontWeightItem == item.mFontWeight && FontStyleItem == item.mFontStyle && CurrentFontSize.Value == item.mFontSize && CurrentFontFamily.ValueStr == item.mFontFamily.Source)
  473. {
  474. string txt = currentItem.mTagContent;
  475. if (Title == txt)
  476. {
  477. Title = "";
  478. }
  479. Title = txt;
  480. }
  481. }
  482. }
  483. }
  484. }
  485. }
  486. }
  487. #endregion
  488. #region 右键菜单
  489. //点击空白处时
  490. private ContextMenu EmptyStateMenu(object sender)
  491. {
  492. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  493. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  494. //粘贴
  495. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  496. //添加文本
  497. customMenu.SetMenuBinding(1, AddTextCommand);
  498. //添加图像
  499. customMenu.SetMenuBinding(2, AddImgCommand);
  500. return popMenu;
  501. }
  502. #endregion
  503. #region 编辑PDF内容触发的事件
  504. /// <summary>
  505. /// 右键菜单的事件
  506. /// </summary>
  507. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  508. {
  509. if (e == null)
  510. return;
  511. switch (e.CommandType)
  512. {
  513. case CommandType.Context:
  514. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  515. {
  516. e.PopupMenu = EmptyStateMenu(sender);
  517. }
  518. break;
  519. default:
  520. e.DoCommand();
  521. break;
  522. }
  523. if (e.PopupMenu != null)
  524. {
  525. e.Handle = true;
  526. }
  527. }
  528. /// <summary>
  529. /// 更多菜单
  530. /// </summary>
  531. /// <param name="isEnable"></param>
  532. /// <returns></returns>
  533. private ContextMenu SelectAnnotContextMenu(bool isEnable)
  534. {
  535. var popMenu = App.Current.FindResource("CustomFontStyleFlyoutMenu") as ContextMenu;
  536. if (popMenu != null && popMenu.Items.Count == 2)
  537. {
  538. //用所选部分重新定义
  539. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  540. menuItem.IsEnabled = isEnable;
  541. menuItem.Command = ReDefineFontStyleCommand;
  542. //恢复默认预设样式
  543. menuItem = popMenu.Items[1] as MenuItem;
  544. menuItem.IsEnabled = isEnable;
  545. menuItem.Command = RestoreDefaultStyleCommand;
  546. }
  547. return popMenu;
  548. }
  549. #endregion
  550. protected List<PDFEditEvent> TextEditEventList;
  551. public void OnNavigatedTo(NavigationContext navigationContext)
  552. {
  553. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  554. navigationContext.Parameters.TryGetValue<List<PDFEditEvent>>(ParameterNames.AnnotEvent, out TextEditEventList);
  555. if (PDFViewer != null)
  556. {
  557. if (TextEditEventList != null && TextEditEventList.Count > 0)
  558. {
  559. TextEditEvent = TextEditEventList[0];
  560. if(TextEditEvent.FontName!=""&& TextEditEvent.FontName !=null)
  561. {
  562. GetPDFEdit();
  563. }
  564. if (TextEditEventList.Count > 1)
  565. IsSelectedEmpty = true;
  566. else
  567. IsSelectedEmpty = false;
  568. if (TextEditEventList.Count == 2)
  569. {
  570. IsLayoutAlign = true;
  571. IsLayoutAvgAlign = false;
  572. }
  573. else if (TextEditEventList.Count > 2)
  574. {
  575. IsLayoutAlign = true;
  576. IsLayoutAvgAlign = true;
  577. }
  578. else
  579. {
  580. IsLayoutAlign = false;
  581. IsLayoutAvgAlign = false;
  582. }
  583. }
  584. else if(TextEditEventList==null)
  585. {
  586. var defaultlists = TextFont.GetPresetFontStyle();
  587. var defaulItem = defaultlists.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  588. if (defaulItem != null)
  589. {
  590. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  591. if (currentItem != null)
  592. {
  593. currentItem.mTagContent = defaulItem.mTagContent;
  594. currentItem.mFontStyle = defaulItem.mFontStyle;
  595. currentItem.mFontWeight = defaulItem.mFontWeight;
  596. currentItem.mFontFamily = defaulItem.mFontFamily;
  597. currentItem.mFontSize = defaulItem.mFontSize;
  598. GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
  599. GetCurrentFontSize(currentItem.mFontSize);
  600. GetFontWeights_Style(currentItem.mFontStyle, currentItem.mFontWeight);
  601. CurrentPresetFont = new ComboDataItem(defaulItem.mTag, defaulItem.mTagContent);
  602. SelectedPresetFont();
  603. SelectColor = new SolidColorBrush(Colors.Black);
  604. CurrentFontSize = new ComboDataItem(currentItem.mFontSize);
  605. CurrentFontFamily = new ComboDataItem(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
  606. FontStyleItem = FontStyles.Normal;
  607. FontWeightItem = FontWeights.Normal;
  608. }
  609. }
  610. }
  611. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  612. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  613. ChangedValue -= FontMode_ChangedValue;
  614. ChangedValue += FontMode_ChangedValue;
  615. IsCanSave = true;
  616. }
  617. }
  618. //文本内容改变触发
  619. private void FontMode_ChangedValue(object sender, FontSetModeType e)
  620. {
  621. if (sender != null&& TextEditEvent != null)
  622. {
  623. switch (e)
  624. {
  625. case FontSetModeType.PresetFontStyes:
  626. if (PresetFontList != null && sender is string == true)
  627. {
  628. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == (string)sender);
  629. if (item != null && TextEditEvent != null)
  630. {
  631. if (TextEditEventList.Count == 1 && IsSelectedEmpty == false)
  632. {
  633. TextEditEvent.FontName = item.mFontFamily != null ? item.mFontFamily.Source : string.Empty;
  634. TextEditEvent.FontSize = item.mFontSize;
  635. TextEditEvent.IsBold = item.mFontWeight == FontWeights.Bold;
  636. TextEditEvent.IsItalic = item.mFontStyle == FontStyles.Italic;
  637. TextEditEvent.UpdatePDFEditByEventArgs();
  638. }
  639. else
  640. {
  641. foreach (var itemlist in TextEditEventList)
  642. {
  643. itemlist.FontName = item.mFontFamily != null ? item.mFontFamily.Source : string.Empty;
  644. itemlist.FontSize = item.mFontSize;
  645. itemlist.IsBold = item.mFontWeight == FontWeights.Bold;
  646. itemlist.IsItalic = item.mFontStyle == FontStyles.Italic;
  647. itemlist.UpdatePDFEditByEventArgs();
  648. }
  649. }
  650. }
  651. }
  652. break;
  653. case FontSetModeType.FontFamilys:
  654. if (sender is string == true)
  655. {
  656. TextEditEvent.FontName = (string)sender;
  657. TextEditEvent.UpdatePDFEditByEventArgs();
  658. }
  659. break;
  660. case FontSetModeType.FontSizes:
  661. if (sender is double == true && (double)sender > 0 && TextEditEvent.FontSize > 0)
  662. {
  663. TextEditEvent.FontSize = (double)sender;
  664. TextEditEvent.UpdatePDFEditByEventArgs();
  665. }
  666. break;
  667. case FontSetModeType.FontWeight_Style:
  668. UpdateFontWeight_Style();
  669. TextEditEvent.IsBold = FontWeightItem == FontWeights.Bold;
  670. TextEditEvent.IsItalic = FontStyleItem == FontStyles.Italic;
  671. TextEditEvent.UpdatePDFEditByEventArgs();
  672. break;
  673. case FontSetModeType.FontColor:
  674. if (sender is Color == true)
  675. {
  676. var setColor = (Color)sender;
  677. var eventColor = TextEditEvent.FontColor;
  678. bool isok = eventColor.A != setColor.A || eventColor.B != setColor.B ||
  679. eventColor.G != setColor.G || eventColor.R != setColor.R;
  680. if (isok)
  681. {
  682. TextEditEvent.FontColor = setColor;
  683. TextEditEvent.UpdatePDFEditByEventArgs();
  684. }
  685. }
  686. break;
  687. case FontSetModeType.TextAlignment:
  688. break;
  689. }
  690. }
  691. }
  692. //获取文本参数
  693. private void GetPDFEdit()
  694. {
  695. SelectColor = new SolidColorBrush(TextEditEvent.FontColor);
  696. CurrentFontSize =new ComboDataItem(TextEditEvent.FontSize);
  697. CurrentFontFamily = new ComboDataItem(TextEditEvent.FontName, TextEditEvent.FontName);
  698. FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
  699. FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
  700. GetFontWeights_Style(FontStyleItem, FontWeightItem);
  701. //判断样式列表中是否存在对应样式
  702. bool isExist = false;
  703. foreach (var item in PresetFontList)
  704. {
  705. if (TextEditEvent.FontSize == item.mFontSize && TextEditEvent.IsBold == (item.mFontWeight == FontWeights.Bold) && TextEditEvent.IsItalic == (item.mFontStyle == FontStyles.Italic)
  706. && (TextEditEvent.FontName == item.mFontFamily.Source || TextEditEvent.FontName == "Arial" && item.mFontFamily.Source == "Helvetica")
  707. )
  708. {
  709. if (item.mTag != "Custom")
  710. {
  711. CurrentPresetFont = new ComboDataItem(item.mTag, item.mTagContent);
  712. isExist = true;
  713. }
  714. break;
  715. }
  716. }
  717. if (isExist == false)
  718. {
  719. FontVm.CurrentPresetFont = new ComboDataItem("Custom", "Custom");
  720. CurrentPresetFont = new ComboDataItem("Custom", "Custom");
  721. GetCurrentFontSize((int)TextEditEvent.FontSize);
  722. if (TextEditEvent.FontName == ""|| TextEditEvent.FontName == null)
  723. {
  724. GetCurrentFontFamily("Helvetica", "Helvetica");
  725. }
  726. else
  727. {
  728. GetCurrentFontFamily(TextEditEvent.FontName, TextEditEvent.FontName);
  729. }
  730. GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal);
  731. }
  732. else
  733. {
  734. if (CurrentPresetFont != null)
  735. {
  736. var itemComboxItem = CurrentPresetFont;
  737. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == itemComboxItem.ValueStr);
  738. if (item != null)
  739. {
  740. if (item.mTag != "Custom")
  741. {
  742. GetCurrentFontSize(item.mFontSize);
  743. if (item.mFontFamily != null)
  744. {
  745. GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
  746. }
  747. GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal);
  748. }
  749. }
  750. }
  751. }
  752. //初始化传入的对齐方式
  753. switch (TextEditEvent.TextAlign.ToString())
  754. {
  755. case "AlignLeft":
  756. GetAnnotAlign(TextAlignment.Left);
  757. break;
  758. case "AlignCenter":
  759. GetAnnotAlign(TextAlignment.Center);
  760. break;
  761. case "AlignRight":
  762. GetAnnotAlign(TextAlignment.Right);
  763. break;
  764. case "Align":
  765. GetAnnotAlign(TextAlignment.Justify);
  766. break;
  767. }
  768. TextAlign = TextEditEvent.TextAlign;
  769. TextEditEvent.UpdatePDFEditByEventArgs();
  770. }
  771. public bool IsNavigationTarget(NavigationContext navigationContext) { return true; }
  772. public void OnNavigatedFrom(NavigationContext navigationContext)
  773. {
  774. IsCanSave = false;
  775. TextEditEvent = null;
  776. IsSelectedEmpty = false;
  777. ClearCheckedAglin?.Invoke(null, null);
  778. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  779. ChangedValue -= FontMode_ChangedValue;
  780. }
  781. }
  782. }