TextEditPropertyViewModel.cs 21 KB

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