TextEditPropertyViewModel.cs 19 KB

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