TextEditPropertyViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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(6, 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. #endregion
  325. #region 编辑PDF内容触发的事件
  326. /// <summary>
  327. /// 右键菜单的事件
  328. /// </summary>
  329. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  330. {
  331. if (e == null)
  332. return;
  333. switch (e.CommandType)
  334. {
  335. case CommandType.Context:
  336. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  337. {
  338. e.PopupMenu = EmptyStateMenu(sender);
  339. }
  340. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  341. {
  342. //文字编辑
  343. if (e.PressOnBorder == true)
  344. {
  345. e.PopupMenu = SelectTextBorder(sender);
  346. }
  347. if (e.PressOnSelectedText == true)
  348. {
  349. e.PopupMenu = SelectTextContent(sender);
  350. }
  351. }
  352. break;
  353. default:
  354. e.DoCommand();
  355. break;
  356. }
  357. if (e.PopupMenu != null)
  358. {
  359. e.Handle = true;
  360. }
  361. }
  362. private ContextMenu SelectAnnotContextMenu(bool isEnable)
  363. {
  364. var popMenu = App.Current.FindResource("CustomFontStyleFlyoutMenu") as ContextMenu;
  365. if (popMenu != null && popMenu.Items.Count == 2)
  366. {
  367. //用所选部分重新定义
  368. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  369. menuItem.IsEnabled = isEnable;
  370. menuItem.Command = ReDefineFontStyleCommand;
  371. //恢复默认预设样式
  372. menuItem = popMenu.Items[1] as MenuItem;
  373. menuItem.IsEnabled = isEnable;
  374. menuItem.Command = RestoreDefaultStyleCommand;
  375. }
  376. return popMenu;
  377. }
  378. #region 全局
  379. public event EventHandler ClearCheckedAglin;
  380. #endregion
  381. #endregion
  382. protected List<PDFEditEvent> TextEditEventList;
  383. public void OnNavigatedTo(NavigationContext navigationContext)
  384. {
  385. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  386. navigationContext.Parameters.TryGetValue<List<PDFEditEvent>>(ParameterNames.AnnotEvent, out TextEditEventList);
  387. if (PDFViewer != null)
  388. {
  389. if (TextEditEventList != null && TextEditEventList.Count > 0)
  390. {
  391. TextEditEvent = TextEditEventList[0];
  392. GetPDFEdit();
  393. if (TextEditEventList.Count > 1)
  394. IsSelectedEmpty = true;
  395. else
  396. IsSelectedEmpty = false;
  397. if (TextEditEventList.Count == 2)
  398. {
  399. IsLayoutAlign = true;
  400. IsLayoutAvgAlign = false;
  401. }
  402. else if(TextEditEventList.Count >2)
  403. {
  404. IsLayoutAlign = true;
  405. IsLayoutAvgAlign = true;
  406. }
  407. else
  408. {
  409. IsLayoutAlign = false;
  410. IsLayoutAvgAlign = false;
  411. }
  412. }
  413. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  414. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  415. ChangedValue -= FontMode_ChangedValue;
  416. ChangedValue += FontMode_ChangedValue;
  417. IsCanSave = true;
  418. }
  419. }
  420. private void FontMode_ChangedValue(object sender, FontSetModeType e)
  421. {
  422. if(sender != null)
  423. {
  424. switch(e)
  425. {
  426. case FontSetModeType.PresetFontStyes:
  427. if(PresetFontList != null && sender is string == true)
  428. {
  429. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == (string)sender);
  430. if (item != null && TextEditEvent != null)
  431. {
  432. if(TextEditEventList.Count == 1 && IsSelectedEmpty == false)
  433. {
  434. TextEditEvent.FontFamily = item.mFontFamily;
  435. TextEditEvent.FontSize = item.mFontSize;
  436. TextEditEvent.FontWeight = item.mFontWeight;
  437. TextEditEvent.FontStyle = item.mFontStyle;
  438. TextEditEvent.UpdatePDFEditByEventArgs();
  439. }
  440. else
  441. {
  442. foreach (var itemlist in TextEditEventList)
  443. {
  444. itemlist.FontFamily = item.mFontFamily;
  445. itemlist.FontSize = item.mFontSize;
  446. itemlist.FontWeight = item.mFontWeight;
  447. itemlist.FontStyle = item.mFontStyle;
  448. itemlist.UpdatePDFEditByEventArgs();
  449. }
  450. }
  451. }
  452. }
  453. break;
  454. case FontSetModeType.FontFamilys:
  455. if(sender is string == true)
  456. {
  457. TextEditEvent.FontFamily = new FontFamily((string)sender);
  458. TextEditEvent.UpdatePDFEditByEventArgs();
  459. }
  460. break;
  461. case FontSetModeType.FontSizes:
  462. if (sender is double == true && (double)sender > 0 && TextEditEvent.FontSize > 0)
  463. {
  464. TextEditEvent.FontSize = (double)sender;
  465. TextEditEvent.UpdatePDFEditByEventArgs();
  466. }
  467. break;
  468. case FontSetModeType.FontWeight_Style:
  469. UpdateFontWeight_Style();
  470. TextEditEvent.FontWeight = FontWeightItem;
  471. TextEditEvent.FontStyle = FontStyleItem;
  472. TextEditEvent.UpdatePDFEditByEventArgs();
  473. break;
  474. case FontSetModeType.FontColor:
  475. if(sender is Color == true)
  476. {
  477. var setColor = (Color)sender;
  478. var eventColor = TextEditEvent.FontColor;
  479. bool isok = eventColor.A != setColor.A || eventColor.B != setColor.B ||
  480. eventColor.G != setColor.G || eventColor.R != setColor.R;
  481. if(isok)
  482. {
  483. TextEditEvent.FontColor = setColor;
  484. TextEditEvent.UpdatePDFEditByEventArgs();
  485. }
  486. }
  487. break;
  488. case FontSetModeType.TextAlignment:
  489. break;
  490. }
  491. }
  492. }
  493. private void GetPDFEdit()
  494. {
  495. if (TextEditEvent != null && TextEditEventList.Count == 1)
  496. {
  497. SelectColor = new SolidColorBrush(TextEditEvent.FontColor);
  498. GetCurrentFontSize((int)TextEditEvent.FontSize);
  499. if(TextEditEvent.FontFamily != null)
  500. GetCurrentFontFamily(TextEditEvent.FontFamily.ToString(), TextEditEvent.FontFamily.ToString());
  501. GetFontWeights_Style(TextEditEvent.FontStyle, TextEditEvent.FontWeight);
  502. TextAlign = TextEditEvent.TextAlign;
  503. }
  504. }
  505. public bool IsNavigationTarget(NavigationContext navigationContext){ return true; }
  506. public void OnNavigatedFrom(NavigationContext navigationContext)
  507. {
  508. IsCanSave = false;
  509. TextEditEvent = null;
  510. IsSelectedEmpty = false;
  511. ClearCheckedAglin?.Invoke(null, null);
  512. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  513. ChangedValue -= FontMode_ChangedValue;
  514. }
  515. }
  516. }