TextEditPropertyViewModel.cs 20 KB

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