TextEditPropertyViewModel.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. if (GetCurrentFontFamily(item) == false)
  153. {
  154. GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
  155. }
  156. }
  157. GetFontWeights_Style(item.mFontStyle, item.mFontWeight);
  158. //CurrentFontSize = new ComboDataItem(TextEditEvent.FontSize);
  159. //CurrentFontFamily = new ComboDataItem(TextEditEvent.FontName, TextEditEvent.FontName);
  160. //FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
  161. //FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
  162. }
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// 编辑 获取预设字体,需要做处理,有些字体带空格,不做处理无法识别
  168. /// </summary>
  169. /// <param name="item"></param>
  170. /// <returns></returns>
  171. private bool GetCurrentFontFamily(PresetFontItem item)
  172. {
  173. DefaultEditProperty defaultEdit = SettingHelper.GetPDFEditDefaultProperty(CPDFEditType.EditText);
  174. if (defaultEdit != null && defaultEdit.SystemFontNameList != null)
  175. {
  176. List<string> itemq = defaultEdit.SystemFontNameList.FindAll(p => p.Replace(" ", "").Equals(item.mFontFamily.ToString()));
  177. if (itemq != null && itemq.Count > 0)
  178. {
  179. GetCurrentFontFamily(item.mFontFamily.ToString(), itemq[0]);
  180. return true;
  181. }
  182. }
  183. return false;
  184. }
  185. /// <summary>
  186. /// 自定义文字样式
  187. /// </summary>
  188. private void CustomFontStyle()
  189. {
  190. if (CurrentPresetFont != null)
  191. {
  192. ContextMenu menu;
  193. if (CurrentPresetFont.ValueStr == "Custom")
  194. {
  195. menu = SelectAnnotContextMenu(false);
  196. }
  197. else
  198. {
  199. menu = SelectAnnotContextMenu(false);
  200. var defaultlists = TextFont.GetPresetFontStyle();
  201. var defaulItem = defaultlists.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  202. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  203. if (currentItem.mTag != "Custom")
  204. {
  205. if (FontWeightItem != defaulItem.mFontWeight ||
  206. FontStyleItem != defaulItem.mFontStyle ||
  207. CurrentFontSize.Value != defaulItem.mFontSize ||
  208. CurrentFontFamily.ValueStr != defaulItem.mFontFamily.Source)
  209. {
  210. menu = SelectAnnotContextMenu(true);
  211. }
  212. }
  213. }
  214. if (menu != null)
  215. {
  216. menu.IsOpen = true;
  217. }
  218. }
  219. }
  220. //设置字体样式
  221. private void FontFamilyChanged()
  222. {
  223. if (CurrentPresetFont != null)
  224. {
  225. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  226. if (currentItem.mTag != "Custom")
  227. {
  228. foreach (var item in PresetFontList)
  229. {
  230. if (item.mTag == currentItem.mTag)
  231. {
  232. if (CurrentFontFamily.ValueStr != item.mFontFamily.Source)
  233. {
  234. if (noAddAnAsterisk(currentItem, item) == false)
  235. {
  236. break;
  237. }
  238. }
  239. else
  240. {
  241. if (noAddAnAsterisk(currentItem, item) == false)
  242. {
  243. break;
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. private bool noAddAnAsterisk(PresetFontItem currentItem, PresetFontItem item)
  252. {
  253. if (FontWeightItem == item.mFontWeight && FontStyleItem == item.mFontStyle && CurrentFontSize.Value == item.mFontSize && (CurrentFontFamily.ValueStr == item.mFontFamily.Source || (CurrentFontFamily.ValueStr == "Arial" && item.mFontFamily.Source == "Helvetica")))
  254. {
  255. string txt = currentItem.mTagContent;
  256. if (Title == txt)
  257. {
  258. Title = "";
  259. }
  260. Title = txt;
  261. return true;
  262. }
  263. else
  264. {
  265. addAnAsterisk(currentItem);
  266. return false;
  267. }
  268. }
  269. private void addAnAsterisk(PresetFontItem currentItem)
  270. {
  271. string txt = string.Format($" * {currentItem.mTagContent}");
  272. if (Title == txt)
  273. {
  274. Title = "";
  275. }
  276. Title = txt;
  277. }
  278. /// <summary>
  279. /// 设置文本字重、样式
  280. /// </summary>
  281. private void FontStyleWeightChanged()
  282. {
  283. if (CurrrentFontWeightStyle != null)
  284. {
  285. UpdateFontWeight_Style();
  286. //FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
  287. //FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
  288. if (CurrentPresetFont != null)
  289. {
  290. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  291. if (currentItem.mTag != "Custom")
  292. {
  293. foreach (var item in PresetFontList)
  294. {
  295. if (item.mTag == currentItem.mTag)
  296. {
  297. if (FontWeightItem != item.mFontWeight || FontStyleItem != item.mFontStyle)
  298. {
  299. string txt = string.Format($" * {currentItem.mTagContent}");
  300. if (Title == txt)
  301. {
  302. Title = "";
  303. }
  304. Title = txt;
  305. break;
  306. }
  307. else
  308. {
  309. if (FontWeightItem == item.mFontWeight && FontStyleItem == item.mFontStyle && CurrentFontSize.Value == item.mFontSize && CurrentFontFamily.ValueStr == item.mFontFamily.Source)
  310. {
  311. string txt = currentItem.mTagContent;
  312. if (Title == txt)
  313. {
  314. Title = "";
  315. }
  316. Title = txt;
  317. }
  318. }
  319. }
  320. }
  321. }
  322. }
  323. }
  324. }
  325. /// <summary>
  326. /// 用所选部分重新定义
  327. /// </summary>
  328. private void ReDefineFontStyle()
  329. {
  330. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  331. if (item == null) return;
  332. item.mFontFamily = new FontFamily(CurrentFontFamily.ValueStr);
  333. if (CurrrentFontWeightStyle.ValueStr == "Bold")
  334. {
  335. item.mFontStyle = FontStyles.Normal;
  336. item.mFontWeight = FontWeights.Bold;
  337. }
  338. else if (CurrrentFontWeightStyle.ValueStr == "Regular")
  339. {
  340. item.mFontStyle = FontStyles.Normal;
  341. item.mFontWeight = FontWeights.Normal;
  342. }
  343. else if (CurrrentFontWeightStyle.ValueStr == "Italic")
  344. {
  345. item.mFontStyle = FontStyles.Italic;
  346. item.mFontWeight = FontWeights.Normal;
  347. }
  348. else
  349. {
  350. item.mFontStyle = FontStyles.Italic;
  351. item.mFontWeight = FontWeights.Bold;
  352. }
  353. item.mFontSize = (int)CurrentFontSize.Value;
  354. CurrentPresetFont = new ComboDataItem(item.mTag, item.mTagContent);
  355. //SelectedPresetFont();
  356. //更改后 保存到本地缓存
  357. TextFont.SavePresetEditFontList(PresetFontList);
  358. }
  359. //重置定义
  360. private void RestoreDefaultStyle()
  361. {
  362. var defaultlists = TextFont.GetPresetFontStyle();
  363. if (CurrentPresetFont.ValueStr != "Custom")
  364. {
  365. var defaulItem = defaultlists.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  366. if (defaulItem != null)
  367. {
  368. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  369. if (currentItem != null)
  370. {
  371. currentItem.mTagContent = defaulItem.mTagContent;
  372. currentItem.mFontStyle = defaulItem.mFontStyle;
  373. currentItem.mFontWeight = defaulItem.mFontWeight;
  374. currentItem.mFontFamily = defaulItem.mFontFamily;
  375. currentItem.mFontSize = defaulItem.mFontSize;
  376. //GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
  377. if (GetCurrentFontFamily(currentItem) == false)
  378. {
  379. GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
  380. }
  381. GetCurrentFontSize(currentItem.mFontSize);
  382. GetFontWeights_Style(currentItem.mFontStyle, currentItem.mFontWeight);
  383. CurrentPresetFont = new ComboDataItem(defaulItem.mTag, defaulItem.mTagContent);
  384. SelectedPresetFont();
  385. TextFont.SavePresetEditFontList(PresetFontList);
  386. }
  387. }
  388. }
  389. }
  390. //设置多选对齐方式
  391. private void LayoutAlignChecked(object obj)
  392. {
  393. if (obj != null)
  394. {
  395. if (obj != null)
  396. {
  397. switch ((string)obj)
  398. {
  399. case "AlignLeft":
  400. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  401. break;
  402. case "AlignHorizonCenter":
  403. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  404. break;
  405. case "AlignRight":
  406. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  407. break;
  408. case "DistributeHorizontal":
  409. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  410. break;
  411. case "AlignTop":
  412. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  413. break;
  414. case "AlignVerticalCenter":
  415. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  416. break;
  417. case "AlignBottom":
  418. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  419. break;
  420. case "DistributeVertical":
  421. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  422. break;
  423. }
  424. }
  425. }
  426. }
  427. //设置文本框内对齐方式
  428. private void TextAlignChecked(object obj)
  429. {
  430. if ((string)obj != null && TextEditEvent != null)
  431. {
  432. switch ((string)obj)
  433. {
  434. case "AlignLeft":
  435. GetAnnotAlign(TextAlignment.Left);
  436. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignLeft;
  437. break;
  438. case "AlignCenter":
  439. GetAnnotAlign(TextAlignment.Center);
  440. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignMiddle;
  441. break;
  442. case "AlignRight":
  443. GetAnnotAlign(TextAlignment.Right);
  444. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignRight;
  445. break;
  446. case "Align":
  447. GetAnnotAlign(TextAlignment.Justify);
  448. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignNone;
  449. break;
  450. }
  451. TextAlign = TextEditEvent.TextAlign;
  452. TextEditEvent.UpdatePDFEditByEventArgs();
  453. }
  454. }
  455. //设置文本内容对齐属性面板显示
  456. private void GetAnnotAlign(TextAlignment align)
  457. {
  458. switch (align)
  459. {
  460. case TextAlignment.Left:
  461. StrTextAlign = "Left";
  462. break;
  463. case TextAlignment.Center:
  464. StrTextAlign = "Center";
  465. break;
  466. case TextAlignment.Right:
  467. StrTextAlign = "Right";
  468. break;
  469. case TextAlignment.Justify:
  470. StrTextAlign = "Justify";
  471. break;
  472. default:
  473. StrTextAlign = "None";
  474. break;
  475. }
  476. }
  477. //设置文本颜色
  478. private void SelectedColor(object obj)
  479. {
  480. if (obj != null)
  481. {
  482. var colorValue = (Color)obj;
  483. if (colorValue != null)
  484. {
  485. SelectColor = new SolidColorBrush(colorValue);
  486. }
  487. }
  488. }
  489. //设置文本样式
  490. private void SelectedFontStyle(object obj)
  491. {
  492. if (obj != null && (PresetFontItem)obj != null)
  493. {
  494. var item = (PresetFontItem)obj;
  495. }
  496. }
  497. //设置字体大小
  498. private void FontSizeChanged(object obj)
  499. {
  500. //if (CurrentFontSize != null)
  501. //{
  502. // CurrentFontSize = new ComboDataItem(TextEditEvent.FontSize);
  503. //}
  504. if (CurrentPresetFont != null)
  505. {
  506. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  507. if (currentItem.mTag != "Custom")
  508. {
  509. foreach (var item in PresetFontList)
  510. {
  511. if (item.mTag == currentItem.mTag)
  512. {
  513. if (CurrentFontSize.Value != item.mFontSize)
  514. {
  515. string txt = string.Format($" * {currentItem.mTagContent}");
  516. if (Title == txt)
  517. {
  518. Title = "";
  519. }
  520. Title = txt;
  521. break;
  522. }
  523. else
  524. {
  525. if (FontWeightItem == item.mFontWeight && FontStyleItem == item.mFontStyle && CurrentFontSize.Value == item.mFontSize && CurrentFontFamily.ValueStr == item.mFontFamily.Source)
  526. {
  527. string txt = currentItem.mTagContent;
  528. if (Title == txt)
  529. {
  530. Title = "";
  531. }
  532. Title = txt;
  533. }
  534. }
  535. }
  536. }
  537. }
  538. }
  539. }
  540. #endregion 文本处理逻辑
  541. #region 右键菜单
  542. //点击空白处时
  543. private ContextMenu EmptyStateMenu(object sender)
  544. {
  545. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  546. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  547. //粘贴
  548. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  549. //添加文本
  550. customMenu.SetMenuBinding(1, AddTextCommand);
  551. //添加图像
  552. customMenu.SetMenuBinding(2, AddImgCommand);
  553. return popMenu;
  554. }
  555. #endregion 右键菜单
  556. #region 编辑PDF内容触发的事件
  557. /// <summary>
  558. /// 右键菜单的事件
  559. /// </summary>
  560. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  561. {
  562. //if (e == null)
  563. // return;
  564. //switch (e.CommandType)
  565. //{
  566. // case CommandType.Context:
  567. // if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  568. // {
  569. // e.PopupMenu = EmptyStateMenu(sender);
  570. // }
  571. // break;
  572. // default:
  573. // e.DoCommand();
  574. // break;
  575. //}
  576. //if (e.PopupMenu != null)
  577. //{
  578. // e.Handle = true;
  579. //}
  580. }
  581. /// <summary>
  582. /// 更多菜单
  583. /// </summary>
  584. /// <param name="isEnable"></param>
  585. /// <returns></returns>
  586. private ContextMenu SelectAnnotContextMenu(bool isEnable)
  587. {
  588. var popMenu = App.Current.FindResource("CustomFontStyleFlyoutMenu") as ContextMenu;
  589. if (popMenu != null && popMenu.Items.Count == 2)
  590. {
  591. //用所选部分重新定义
  592. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  593. //恢复默认预设样式
  594. menuItem = popMenu.Items[0] as MenuItem;
  595. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  596. menuItem.IsEnabled = false;
  597. if (currentItem.mTag != "Custom")
  598. {
  599. if (FontWeightItem != currentItem.mFontWeight ||
  600. FontStyleItem != currentItem.mFontStyle ||
  601. CurrentFontSize.Value != currentItem.mFontSize ||
  602. CurrentFontFamily.ValueStr != currentItem.mFontFamily.Source)
  603. {
  604. menuItem.IsEnabled = true;
  605. menuItem.Command = ReDefineFontStyleCommand;
  606. }
  607. }
  608. //恢复默认预设样式
  609. menuItem = popMenu.Items[1] as MenuItem;
  610. menuItem.IsEnabled = isEnable;
  611. menuItem.Command = RestoreDefaultStyleCommand;
  612. }
  613. return popMenu;
  614. }
  615. #endregion 编辑PDF内容触发的事件
  616. protected List<PDFEditEvent> TextEditEventList;
  617. public void OnNavigatedTo(NavigationContext navigationContext)
  618. {
  619. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  620. navigationContext.Parameters.TryGetValue<List<PDFEditEvent>>(ParameterNames.AnnotEvent, out TextEditEventList);
  621. if (PDFViewer != null)
  622. {
  623. if (TextEditEventList != null && TextEditEventList.Count > 0)
  624. {
  625. TextEditEvent = TextEditEventList[0];
  626. if (TextEditEvent.FontName != "" && TextEditEvent.FontName != null)
  627. {
  628. GetPDFEdit();
  629. }
  630. if (TextEditEventList.Count > 1)
  631. IsSelectedEmpty = true;
  632. else
  633. IsSelectedEmpty = false;
  634. if (TextEditEventList.Count == 2)
  635. {
  636. IsLayoutAlign = true;
  637. IsLayoutAvgAlign = false;
  638. }
  639. else if (TextEditEventList.Count > 2)
  640. {
  641. IsLayoutAlign = true;
  642. IsLayoutAvgAlign = true;
  643. }
  644. else
  645. {
  646. IsLayoutAlign = false;
  647. IsLayoutAvgAlign = false;
  648. }
  649. }
  650. else if (TextEditEventList == null || TextEditEvent.FontName == null || TextEditEvent.FontName == "")
  651. {
  652. var defaultlists = TextFont.GetPresetFontStyle();
  653. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == "Custom");
  654. if (currentItem != null)
  655. {
  656. SelectColor = new SolidColorBrush(Colors.Black);
  657. CurrentFontSize = new ComboDataItem(currentItem.mFontSize);
  658. CurrentPresetFont = new ComboDataItem(currentItem.mTag, currentItem.mTagContent);
  659. CurrentFontFamily = new ComboDataItem(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
  660. FontStyleItem = FontStyles.Normal;
  661. FontWeightItem = FontWeights.Normal;
  662. //GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
  663. if (GetCurrentFontFamily(currentItem) == false)
  664. {
  665. GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
  666. }
  667. GetCurrentFontSize(currentItem.mFontSize);
  668. GetFontWeights_Style(currentItem.mFontStyle, currentItem.mFontWeight);
  669. SelectedPresetFont();
  670. }
  671. }
  672. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  673. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  674. ChangedValue -= FontMode_ChangedValue;
  675. ChangedValue += FontMode_ChangedValue;
  676. IsCanSave = true;
  677. }
  678. }
  679. private DefaultEditProperty GetEditDefault(ComPDFKit.PDFPage.CPDFEditType editType)
  680. {
  681. var edit = SettingHelper.GetPDFEditDefaultProperty(editType);
  682. if (edit == null)
  683. {
  684. edit = new DefaultEditProperty();
  685. edit.EditType = editType;
  686. }
  687. return edit;
  688. }
  689. /// <summary>
  690. /// 文本内容改变触发
  691. /// </summary>
  692. /// <param name="sender"></param>
  693. /// <param name="e"></param>
  694. private void FontMode_ChangedValue(object sender, FontSetModeType e)
  695. {
  696. if (sender != null && TextEditEvent != null)
  697. {
  698. PDFEditEvent pdfEditEvent = PDFEditHelper.GetPDFEditDefaultProperty(out DefaultEditProperty defaultEdit, PDFViewer);
  699. switch (e)
  700. {
  701. case FontSetModeType.PresetFontStyes:
  702. if (PresetFontList != null && sender is string == true)
  703. {
  704. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == (string)sender);
  705. if (item != null && TextEditEvent != null)
  706. {
  707. if (TextEditEventList.Count == 1 && IsSelectedEmpty == false)
  708. {
  709. TextEditEvent.FontName = item.mFontFamily != null ? item.mFontFamily.Source : string.Empty;
  710. TextEditEvent.FontSize = item.mFontSize;
  711. TextEditEvent.IsBold = item.mFontWeight == FontWeights.Bold;
  712. TextEditEvent.IsItalic = item.mFontStyle == FontStyles.Italic;
  713. TextEditEvent.UpdatePDFEditByEventArgs();
  714. }
  715. else
  716. {
  717. foreach (var itemlist in TextEditEventList)
  718. {
  719. itemlist.FontName = item.mFontFamily != null ? item.mFontFamily.Source : string.Empty;
  720. itemlist.FontSize = item.mFontSize;
  721. itemlist.IsBold = item.mFontWeight == FontWeights.Bold;
  722. itemlist.IsItalic = item.mFontStyle == FontStyles.Italic;
  723. itemlist.UpdatePDFEditByEventArgs();
  724. }
  725. }
  726. defaultEdit.FontName = TextEditEvent.FontName;
  727. defaultEdit.FontSize = TextEditEvent.FontSize;
  728. defaultEdit.IsBold = TextEditEvent.IsBold;
  729. defaultEdit.IsItalic = TextEditEvent.IsItalic;
  730. }
  731. }
  732. break;
  733. case FontSetModeType.FontFamilys:
  734. if (sender is string == true)
  735. {
  736. TextEditEvent.FontName = (string)sender;
  737. TextEditEvent.UpdatePDFEditByEventArgs();
  738. defaultEdit.FontName = TextEditEvent.FontName;
  739. }
  740. break;
  741. case FontSetModeType.FontSizes:
  742. if (sender is double == true && (double)sender > 0 && TextEditEvent.FontSize >= 0)
  743. {
  744. TextEditEvent.FontSize = (double)sender;
  745. TextEditEvent.UpdatePDFEditByEventArgs();
  746. defaultEdit.FontSize = TextEditEvent.FontSize;
  747. }
  748. break;
  749. case FontSetModeType.FontWeight_Style:
  750. UpdateFontWeight_Style();
  751. TextEditEvent.IsBold = FontWeightItem == FontWeights.Bold;
  752. TextEditEvent.IsItalic = FontStyleItem == FontStyles.Italic;
  753. TextEditEvent.UpdatePDFEditByEventArgs();
  754. defaultEdit.IsBold = TextEditEvent.IsBold;
  755. defaultEdit.IsItalic = TextEditEvent.IsItalic;
  756. break;
  757. case FontSetModeType.FontColor:
  758. if (sender is Color == true)
  759. {
  760. var setColor = (Color)sender;
  761. var eventColor = TextEditEvent.FontColor;
  762. bool isok = eventColor.A != setColor.A || eventColor.B != setColor.B ||
  763. eventColor.G != setColor.G || eventColor.R != setColor.R;
  764. if (isok)
  765. {
  766. TextEditEvent.FontColor = setColor;
  767. TextEditEvent.UpdatePDFEditByEventArgs();
  768. }
  769. }
  770. defaultEdit.FontColor = TextEditEvent.FontColor;
  771. break;
  772. case FontSetModeType.TextAlignment:
  773. break;
  774. }
  775. PDFEditHelper.GetPDFEditEvent(defaultEdit, PDFViewer);
  776. SettingHelper.SetPDFEditProperty(defaultEdit);
  777. Settings.Default.Save();
  778. }
  779. }
  780. //获取文本参数
  781. private void GetPDFEdit()
  782. {
  783. DefaultEditProperty defaultEdit = SettingHelper.GetPDFEditDefaultProperty(CPDFEditType.EditText);
  784. if (!App.IsGetTextFamily)
  785. {
  786. if (TextEditEvent.SystemFontNameList.Count > 0)
  787. {
  788. EditHelper.FontFamily = TextEditEvent.SystemFontNameList;
  789. App.IsGetTextFamily = true;
  790. }
  791. InitBase_FontFamilys();
  792. if (defaultEdit != null)
  793. {
  794. if (defaultEdit.SystemFontNameList.Count < EditHelper.FontFamily.Count)
  795. {
  796. defaultEdit.SystemFontNameList = EditHelper.FontFamily;
  797. SettingHelper.SetPDFEditProperty(defaultEdit);
  798. Settings.Default.Save();
  799. }
  800. }
  801. }
  802. SelectColor = new SolidColorBrush(TextEditEvent.FontColor);
  803. CurrentFontSize = new ComboDataItem(TextEditEvent.FontSize);
  804. CurrentFontFamily = new ComboDataItem(TextEditEvent.FontName, TextEditEvent.FontName);
  805. FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
  806. FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
  807. //GetCurrentFontFamily(TextEditEvent.FontName, TextEditEvent.FontName);
  808. GetFontWeights_Style(FontStyleItem, FontWeightItem);
  809. //判断样式列表中是否存在对应样式
  810. bool isExist = false;
  811. foreach (var item in PresetFontList)
  812. {
  813. string itemmFontFamily = item.mFontFamily.Source;
  814. if (item.mFontFamily.Source == "TimesRoman")
  815. {
  816. itemmFontFamily = "Times-Roman";
  817. }
  818. if (defaultEdit != null && defaultEdit.SystemFontNameList != null)
  819. {
  820. List<string> itemq = defaultEdit.SystemFontNameList.FindAll(p => p.Replace(" ", "").Equals(item.mFontFamily.ToString()));
  821. if (itemq != null && itemq.Count > 0)
  822. {
  823. itemmFontFamily = itemq[0];
  824. }
  825. }
  826. if (TextEditEvent.FontSize == item.mFontSize && TextEditEvent.IsBold == (item.mFontWeight == FontWeights.Bold) && TextEditEvent.IsItalic == (item.mFontStyle == FontStyles.Italic)
  827. && (TextEditEvent.FontName == itemmFontFamily || TextEditEvent.FontName == "Arial")
  828. )
  829. {
  830. if (item.mTag != "Custom")
  831. {
  832. CurrentPresetFont = new ComboDataItem(item.mTag, item.mTagContent);
  833. isExist = true;
  834. }
  835. break;
  836. }
  837. }
  838. if (isExist == false)
  839. {
  840. FontVm.CurrentPresetFont = new ComboDataItem("Custom", "Custom");
  841. CurrentPresetFont = new ComboDataItem("Custom", "Custom");
  842. GetCurrentFontSize((int)TextEditEvent.FontSize);
  843. if (TextEditEvent.FontName == "" || TextEditEvent.FontName == null)
  844. {
  845. GetCurrentFontFamily("Arial", "Arial");
  846. }
  847. else
  848. {
  849. GetCurrentFontFamily(TextEditEvent.FontName.Replace(" ", ""), TextEditEvent.FontName);
  850. //List<ComboDataItem> itemq = FontFamilyItems.FindAll(p => p.ValueStr == currentItem.mFontFamily.ToString());
  851. //if (itemq != null && itemq.Count > 0)
  852. //{
  853. // GetCurrentFontFamily(TextEditEvent.FontName, itemq[0].Content);
  854. //}
  855. }
  856. GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal);
  857. }
  858. else
  859. {
  860. if (CurrentPresetFont != null)
  861. {
  862. var itemComboxItem = CurrentPresetFont;
  863. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == itemComboxItem.ValueStr);
  864. if (item != null)
  865. {
  866. if (item.mTag != "Custom")
  867. {
  868. GetCurrentFontSize(item.mFontSize);
  869. if (item.mFontFamily != null)
  870. {
  871. if (GetCurrentFontFamily(item) == false)
  872. {
  873. GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
  874. }
  875. }
  876. GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal);
  877. }
  878. }
  879. }
  880. }
  881. //初始化传入的对齐方式
  882. switch (TextEditEvent.TextAlign.ToString())
  883. {
  884. case "AlignLeft":
  885. GetAnnotAlign(TextAlignment.Left);
  886. break;
  887. case "AlignCenter":
  888. GetAnnotAlign(TextAlignment.Center);
  889. break;
  890. case "AlignRight":
  891. GetAnnotAlign(TextAlignment.Right);
  892. break;
  893. case "Align":
  894. GetAnnotAlign(TextAlignment.Justify);
  895. break;
  896. }
  897. TextAlign = TextEditEvent.TextAlign;
  898. TextEditEvent.UpdatePDFEditByEventArgs();
  899. }
  900. public bool IsNavigationTarget(NavigationContext navigationContext)
  901. { return true; }
  902. public void OnNavigatedFrom(NavigationContext navigationContext)
  903. {
  904. IsCanSave = false;
  905. TextEditEvent = null;
  906. IsSelectedEmpty = false;
  907. ClearCheckedAglin?.Invoke(null, null);
  908. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  909. ChangedValue -= FontMode_ChangedValue;
  910. }
  911. }
  912. }