TextEditPropertyViewModel.cs 37 KB

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