TextEditPropertyViewModel.cs 24 KB

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