TextEditPropertyViewModel.cs 37 KB

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