TextEditPropertyViewModel.cs 19 KB

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