TextEditPropertyViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Win32;
  4. using PDF_Master.CustomControl.CompositeControl;
  5. using PDF_Master.Helper;
  6. using PDF_Master.Model;
  7. using PDF_Master.Model.AnnotPanel;
  8. using PDF_Master.Model.PropertyPanel.AnnotPanel;
  9. using PDFSettings;
  10. using Prism.Commands;
  11. using Prism.Mvvm;
  12. using Prism.Regions;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. namespace PDF_Master.ViewModels.PropertyPanel.PDFEdit
  23. {
  24. public class TextEditPropertyViewModel : PDFEditVM, INavigationAware
  25. {
  26. #region
  27. private bool isSelectedEmpty;
  28. public bool IsSelectedEmpty
  29. {
  30. get { return isSelectedEmpty; }
  31. set { SetProperty(ref isSelectedEmpty, value); }
  32. }
  33. private ComPDFKit.PDFPage.Edit.TextAlignType _textAlign;
  34. public ComPDFKit.PDFPage.Edit.TextAlignType TextAlign
  35. {
  36. get { return _textAlign; }
  37. set { _textAlign = value; }
  38. }
  39. #endregion
  40. #region Command
  41. #region 文本Command
  42. public DelegateCommand AddTextCommand { get; set; }
  43. public DelegateCommand AddImgCommand { get; set; }
  44. public DelegateCommand<object> SelectedColorCommand { get; set; }
  45. public DelegateCommand<object> SelectedFontStyleCommand { get; set; }
  46. public DelegateCommand<object> FontSizeChangedCommand { get; set; }
  47. public DelegateCommand<object> TextAlignCheckedCommand { get; set; }
  48. //进入属性面板编辑
  49. public DelegateCommand EditTextModeCommand { get; set; }
  50. public DelegateCommand CustomFontStyleCommand { get; set; }
  51. public DelegateCommand ReDefineFontStyleCommand { get; set; }
  52. public DelegateCommand RestoreDefaultStyleCommand { get; set; }
  53. public DelegateCommand<object> LayoutAlignCheckedCommand { get; set; }
  54. #endregion
  55. #endregion
  56. #region 初始化
  57. public TextEditPropertyViewModel()
  58. {
  59. InitVariable();
  60. InitCommand();
  61. }
  62. private void InitVariable()
  63. {
  64. InitBaseVariable();
  65. }
  66. private void InitCommand()
  67. {
  68. AddTextCommand = new DelegateCommand(AddText);
  69. AddImgCommand = new DelegateCommand(AddImg);
  70. SelectedColorCommand = new DelegateCommand<object>(SelectedColor);
  71. SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
  72. FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged);
  73. TextAlignCheckedCommand = new DelegateCommand<object>(TextAlignChecked);
  74. EditTextModeCommand = new DelegateCommand(EditTextMode);
  75. CustomFontStyleCommand = new DelegateCommand(CustomFontStyle_Click);
  76. ReDefineFontStyleCommand = new DelegateCommand(ReDefineFontStyle);
  77. RestoreDefaultStyleCommand = new DelegateCommand(RestoreDefaultStyle);
  78. LayoutAlignCheckedCommand = new DelegateCommand<object>(LayoutAlignChecked);
  79. }
  80. #endregion
  81. #region 文本处理
  82. private void AddText()
  83. {
  84. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  85. }
  86. private void AddImg()
  87. {
  88. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  89. }
  90. private void EditTextMode()
  91. {
  92. }
  93. private void CustomFontStyle_Click()
  94. {
  95. if(CurrentPresetFont != null)
  96. {
  97. ContextMenu menu;
  98. if (CurrentPresetFont.ValueStr == "custom")
  99. {
  100. menu = SelectAnnotContextMenu(false);
  101. }
  102. else
  103. {
  104. menu = SelectAnnotContextMenu(true);
  105. }
  106. if (menu != null)
  107. {
  108. menu.IsOpen = true;
  109. }
  110. }
  111. }
  112. private void ReDefineFontStyle()
  113. {
  114. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  115. if (CurrentFontFamily.ValueStr == "Bold")
  116. {
  117. item.mFontStyle = FontStyles.Normal;
  118. item.mFontWeight = FontWeights.Bold;
  119. }
  120. else if (CurrentFontFamily.ValueStr == "Regular")
  121. {
  122. item.mFontStyle = FontStyles.Normal;
  123. item.mFontWeight = FontWeights.Normal;
  124. }
  125. else
  126. {
  127. item.mFontStyle = FontStyles.Italic;
  128. item.mFontWeight = FontWeights.Bold;
  129. }
  130. item.mFontSize = (int)CurrentFontSize.Value;
  131. }
  132. private void RestoreDefaultStyle()
  133. {
  134. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  135. }
  136. private void LayoutAlignChecked(object obj)
  137. {
  138. if(obj != null)
  139. {
  140. if (obj != null)
  141. {
  142. switch ((string)obj)
  143. {
  144. case "AlignLeft":
  145. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  146. break;
  147. case "AlignHorizonCenter":
  148. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  149. break;
  150. case "AlignRight":
  151. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  152. break;
  153. case "DistributeHorizontal":
  154. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  155. break;
  156. case "AlignTop":
  157. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  158. break;
  159. case "AlignVerticalCenter":
  160. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  161. break;
  162. case "AlignBottom":
  163. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  164. break;
  165. case "DistributeVertical":
  166. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  167. break;
  168. }
  169. }
  170. }
  171. }
  172. private void TextAlignChecked(object obj)
  173. {
  174. if ((string)obj != null && TextEditEvent != null)
  175. {
  176. switch ((string)obj)
  177. {
  178. case "AlignLeft":
  179. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignLeft;
  180. break;
  181. case "AlignCenter":
  182. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignMiddle;
  183. break;
  184. case "AlignRight":
  185. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignRight;
  186. break;
  187. case "Align":
  188. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignNone;
  189. break;
  190. }
  191. TextAlign = TextEditEvent.TextAlign;
  192. TextEditEvent.UpdatePDFEditByEventArgs();
  193. }
  194. }
  195. private void SelectedColor(object obj)
  196. {
  197. if (obj != null)
  198. {
  199. var colorValue = (Color)obj;
  200. if (colorValue != null)
  201. {
  202. SelectColor = new SolidColorBrush(colorValue);
  203. }
  204. }
  205. }
  206. private void SelectedFontStyle(object obj)
  207. {
  208. if (obj != null && (PresetFontItem)obj != null)
  209. {
  210. var item = (PresetFontItem)obj;
  211. }
  212. }
  213. private void FontSizeChanged(object obj)
  214. {
  215. if (obj != null)
  216. {
  217. var item = (ComboBoxItem)obj;
  218. var content = (string)item.Content;
  219. if (content != null)
  220. {
  221. var intData = int.Parse(content);
  222. //TextFontSize = intData;
  223. }
  224. }
  225. }
  226. #endregion
  227. #region 布局处理
  228. private void ImgAlignChecked(object obj)
  229. {
  230. if (obj != null)
  231. {
  232. switch ((string)obj)
  233. {
  234. case "AlignLeft":
  235. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  236. break;
  237. case "AlignHorizonCenter":
  238. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  239. break;
  240. case "AlignRight":
  241. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  242. break;
  243. case "DistributeHorizontal":
  244. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  245. break;
  246. case "AlignTop":
  247. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  248. break;
  249. case "AlignVerticalCenter":
  250. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  251. break;
  252. case "AlignBottom":
  253. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  254. break;
  255. case "DistributeVertical":
  256. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  257. break;
  258. }
  259. }
  260. }
  261. private void ReLoadLayoutAlign(int count)
  262. {
  263. if (count >= 2)
  264. IsLayoutAlign = true;
  265. else
  266. IsLayoutAlign = false;
  267. if (count >= 3)
  268. IsLayoutAvgAlign = true;
  269. else
  270. IsLayoutAvgAlign = false;
  271. }
  272. #endregion
  273. #region 右键菜单
  274. //点击空白处时
  275. private ContextMenu EmptyStateMenu(object sender)
  276. {
  277. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  278. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  279. //粘贴
  280. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  281. //添加文本
  282. customMenu.SetMenuBinding(1, AddTextCommand);
  283. //添加图像
  284. customMenu.SetMenuBinding(2, AddImgCommand);
  285. return popMenu;
  286. }
  287. //选中文字的框
  288. private ContextMenu SelectTextBorder(object sender)
  289. {
  290. var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu;
  291. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  292. //编辑
  293. customMenu.SetMenuBinding(0, EditTextModeCommand);
  294. //复制
  295. customMenu.SetMenuBinding(1, ApplicationCommands.Copy);
  296. //剪切
  297. customMenu.SetMenuBinding(2, ApplicationCommands.Cut);
  298. //粘贴
  299. customMenu.SetMenuBinding(3, ApplicationCommands.Paste);
  300. //删除
  301. customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  302. customMenu.SetVisibilityProperty(5, false);
  303. return popMenu;
  304. }
  305. //选中文字内容
  306. private ContextMenu SelectTextContent(object sender)
  307. {
  308. var popMenu = App.Current.FindResource("SelectContentMenu") as ContextMenu;
  309. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  310. //复制
  311. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  312. //剪切
  313. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  314. //粘贴
  315. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  316. //粘贴并匹配样式
  317. customMenu.SetVisibilityProperty(3, false);
  318. //删除
  319. customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  320. //全部选定
  321. customMenu.SetMenuBinding(5, ApplicationCommands.SelectAll);
  322. return popMenu;
  323. }
  324. //编辑中右键
  325. private ContextMenu EditTextContent(object sender)
  326. {
  327. var popMenu = App.Current.FindResource("NoneSelectContentMenu") as ContextMenu;
  328. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  329. ////复制
  330. //customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  331. ////剪切
  332. //customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  333. //全部选定
  334. customMenu.SetMenuBinding(0, ApplicationCommands.SelectAll);
  335. //粘贴
  336. customMenu.SetMenuBinding(1, ApplicationCommands.Paste);
  337. //粘贴并匹配样式
  338. customMenu.SetVisibilityProperty(2, false);
  339. ////删除
  340. //customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  341. return popMenu;
  342. }
  343. #endregion
  344. #region 编辑PDF内容触发的事件
  345. /// <summary>
  346. /// 右键菜单的事件
  347. /// </summary>
  348. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  349. {
  350. if (e == null)
  351. return;
  352. switch (e.CommandType)
  353. {
  354. case CommandType.Context:
  355. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  356. {
  357. //文字编辑
  358. if (e.PressOnBorder == true)
  359. {
  360. e.PopupMenu = SelectTextBorder(sender);
  361. }
  362. else if(e.PressOnSelectedText == true)
  363. {
  364. e.PopupMenu = SelectTextContent(sender);
  365. }
  366. else if(e.SelectText=="")
  367. {
  368. e.PopupMenu= EditTextContent(sender);
  369. }
  370. else
  371. {
  372. e.PopupMenu = SelectTextContent(sender);
  373. }
  374. }
  375. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  376. {
  377. e.PopupMenu = SelectTextBorder(sender);
  378. }
  379. break;
  380. default:
  381. e.DoCommand();
  382. break;
  383. }
  384. if (e.PopupMenu != null)
  385. {
  386. e.Handle = true;
  387. }
  388. }
  389. private ContextMenu SelectAnnotContextMenu(bool isEnable)
  390. {
  391. var popMenu = App.Current.FindResource("CustomFontStyleFlyoutMenu") as ContextMenu;
  392. if (popMenu != null && popMenu.Items.Count == 2)
  393. {
  394. //用所选部分重新定义
  395. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  396. menuItem.IsEnabled = isEnable;
  397. menuItem.Command = ReDefineFontStyleCommand;
  398. //恢复默认预设样式
  399. menuItem = popMenu.Items[1] as MenuItem;
  400. menuItem.IsEnabled = isEnable;
  401. menuItem.Command = RestoreDefaultStyleCommand;
  402. }
  403. return popMenu;
  404. }
  405. #region 全局
  406. public event EventHandler ClearCheckedAglin;
  407. #endregion
  408. #endregion
  409. protected List<PDFEditEvent> TextEditEventList;
  410. public void OnNavigatedTo(NavigationContext navigationContext)
  411. {
  412. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  413. navigationContext.Parameters.TryGetValue<List<PDFEditEvent>>(ParameterNames.AnnotEvent, out TextEditEventList);
  414. if (PDFViewer != null)
  415. {
  416. if (TextEditEventList != null && TextEditEventList.Count > 0)
  417. {
  418. TextEditEvent = TextEditEventList[0];
  419. GetPDFEdit();
  420. if (TextEditEventList.Count > 1)
  421. IsSelectedEmpty = true;
  422. else
  423. IsSelectedEmpty = false;
  424. if (TextEditEventList.Count == 2)
  425. {
  426. IsLayoutAlign = true;
  427. IsLayoutAvgAlign = false;
  428. }
  429. else if(TextEditEventList.Count >2)
  430. {
  431. IsLayoutAlign = true;
  432. IsLayoutAvgAlign = true;
  433. }
  434. else
  435. {
  436. IsLayoutAlign = false;
  437. IsLayoutAvgAlign = false;
  438. }
  439. }
  440. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  441. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  442. ChangedValue -= FontMode_ChangedValue;
  443. ChangedValue += FontMode_ChangedValue;
  444. IsCanSave = true;
  445. }
  446. }
  447. private void FontMode_ChangedValue(object sender, FontSetModeType e)
  448. {
  449. if(sender != null)
  450. {
  451. switch(e)
  452. {
  453. case FontSetModeType.PresetFontStyes:
  454. if(PresetFontList != null && sender is string == true)
  455. {
  456. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == (string)sender);
  457. if (item != null && TextEditEvent != null)
  458. {
  459. if(TextEditEventList.Count == 1 && IsSelectedEmpty == false)
  460. {
  461. TextEditEvent.FontFamily = item.mFontFamily;
  462. TextEditEvent.FontSize = item.mFontSize;
  463. TextEditEvent.FontWeight = item.mFontWeight;
  464. TextEditEvent.FontStyle = item.mFontStyle;
  465. TextEditEvent.UpdatePDFEditByEventArgs();
  466. }
  467. else
  468. {
  469. foreach (var itemlist in TextEditEventList)
  470. {
  471. itemlist.FontFamily = item.mFontFamily;
  472. itemlist.FontSize = item.mFontSize;
  473. itemlist.FontWeight = item.mFontWeight;
  474. itemlist.FontStyle = item.mFontStyle;
  475. itemlist.UpdatePDFEditByEventArgs();
  476. }
  477. }
  478. }
  479. }
  480. break;
  481. case FontSetModeType.FontFamilys:
  482. if(sender is string == true)
  483. {
  484. TextEditEvent.FontFamily = new FontFamily((string)sender);
  485. TextEditEvent.UpdatePDFEditByEventArgs();
  486. }
  487. break;
  488. case FontSetModeType.FontSizes:
  489. if (sender is double == true && (double)sender > 0 && TextEditEvent.FontSize > 0)
  490. {
  491. TextEditEvent.FontSize = (double)sender;
  492. TextEditEvent.UpdatePDFEditByEventArgs();
  493. }
  494. break;
  495. case FontSetModeType.FontWeight_Style:
  496. UpdateFontWeight_Style();
  497. TextEditEvent.FontWeight = FontWeightItem;
  498. TextEditEvent.FontStyle = FontStyleItem;
  499. TextEditEvent.UpdatePDFEditByEventArgs();
  500. break;
  501. case FontSetModeType.FontColor:
  502. if(sender is Color == true)
  503. {
  504. var setColor = (Color)sender;
  505. var eventColor = TextEditEvent.FontColor;
  506. bool isok = eventColor.A != setColor.A || eventColor.B != setColor.B ||
  507. eventColor.G != setColor.G || eventColor.R != setColor.R;
  508. if(isok)
  509. {
  510. TextEditEvent.FontColor = setColor;
  511. TextEditEvent.UpdatePDFEditByEventArgs();
  512. }
  513. }
  514. break;
  515. case FontSetModeType.TextAlignment:
  516. break;
  517. }
  518. }
  519. }
  520. private void GetPDFEdit()
  521. {
  522. if (TextEditEvent != null && TextEditEventList.Count == 1)
  523. {
  524. SelectColor = new SolidColorBrush(TextEditEvent.FontColor);
  525. GetCurrentFontSize((int)TextEditEvent.FontSize);
  526. if(TextEditEvent.FontFamily != null)
  527. GetCurrentFontFamily(TextEditEvent.FontFamily.ToString(), TextEditEvent.FontFamily.ToString());
  528. GetFontWeights_Style(TextEditEvent.FontStyle, TextEditEvent.FontWeight);
  529. TextAlign = TextEditEvent.TextAlign;
  530. }
  531. }
  532. public bool IsNavigationTarget(NavigationContext navigationContext){ return true; }
  533. public void OnNavigatedFrom(NavigationContext navigationContext)
  534. {
  535. IsCanSave = false;
  536. TextEditEvent = null;
  537. IsSelectedEmpty = false;
  538. ClearCheckedAglin?.Invoke(null, null);
  539. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  540. ChangedValue -= FontMode_ChangedValue;
  541. }
  542. }
  543. }