TextEditPropertyViewModel.cs 40 KB

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