TextEditToolContentViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Media;
  9. using System.Windows;
  10. using PDF_Master.Model.PropertyPanel.AnnotPanel;
  11. using System.Windows.Controls;
  12. using Prism.Regions;
  13. using ComPDFKitViewer.PdfViewer;
  14. using PDF_Master.Model;
  15. using Microsoft.Win32;
  16. using PDF_Master.CustomControl;
  17. using ComPDFKitViewer;
  18. using System.Windows.Input;
  19. using PDF_Master.Helper;
  20. using ComPDFKitViewer.AnnotEvent;
  21. namespace PDF_Master.ViewModels.Tools
  22. {
  23. public class TextEditToolContentViewModel: BindableBase, INavigationAware
  24. {
  25. #region 变量
  26. public ViewContentViewModel viewContentViewModel;
  27. private CPDFViewer PDFViewer;
  28. private IRegionManager regions;
  29. private Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
  30. #endregion
  31. #region 属性
  32. private bool _isTextEdit = false;
  33. public bool IsTextEdit
  34. {
  35. get { return _isTextEdit; }
  36. set { SetProperty(ref _isTextEdit, value); }
  37. }
  38. private bool _isImgEdit = false;
  39. public bool IsImgEdit
  40. {
  41. get { return _isImgEdit; }
  42. set { SetProperty(ref _isImgEdit, value); }
  43. }
  44. #endregion
  45. #region Command
  46. // 添加文本、图像
  47. public DelegateCommand<object> AddContentCommand { get; set; }
  48. public DelegateCommand AddTextCommand { get; set; }
  49. public DelegateCommand AddImgCommand { get; set; }
  50. public DelegateCommand EditTextModeCommand { get; set; }
  51. #endregion
  52. #region 初始化
  53. public TextEditToolContentViewModel(IRegionManager regionManager)
  54. {
  55. regions = regionManager;
  56. InitCommand();
  57. InitBtnToProperty();
  58. }
  59. private void InitCommand()
  60. {
  61. AddContentCommand = new DelegateCommand<object>(AddContent);
  62. AddTextCommand = new DelegateCommand(AddText);
  63. AddImgCommand = new DelegateCommand(AddImg);
  64. EditTextModeCommand = new DelegateCommand(EditTextMode);
  65. }
  66. private void InitBtnToProperty()
  67. {
  68. btnToProperty.Add("Text", "TextEditProperty");
  69. btnToProperty.Add("Image", "ImageEditProperty");
  70. btnToProperty.Add("TextAndImage", "ImageTextEditProperty");
  71. btnToProperty.Add("PropertyPanelContent", "PropertyPanelContent");
  72. }
  73. #endregion
  74. #region 右键菜单
  75. //点击空白处时
  76. private ContextMenu EmptyStateMenu(object sender)
  77. {
  78. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  79. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  80. //粘贴
  81. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  82. //添加文本
  83. customMenu.SetMenuBinding(1, AddTextCommand);
  84. //添加图像
  85. customMenu.SetMenuBinding(2, AddImgCommand);
  86. return popMenu;
  87. }
  88. //选中文字的框
  89. private ContextMenu SelectTextBorder(object sender)
  90. {
  91. var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu;
  92. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  93. //复制
  94. customMenu.SetMenuBinding(1, ApplicationCommands.Copy);
  95. //剪切
  96. customMenu.SetMenuBinding(2, ApplicationCommands.Cut);
  97. //粘贴
  98. customMenu.SetMenuBinding(3, ApplicationCommands.Paste);
  99. //删除
  100. customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  101. customMenu.SetVisibilityProperty(6, false);
  102. return popMenu;
  103. }
  104. //选中文字内容
  105. private ContextMenu SelectTextContent(object sender)
  106. {
  107. var popMenu = App.Current.FindResource("SelectContentMenu") as ContextMenu;
  108. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  109. //复制
  110. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  111. //剪切
  112. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  113. //粘贴
  114. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  115. //粘贴并匹配样式
  116. customMenu.SetVisibilityProperty(3, false);
  117. //删除
  118. customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  119. //全部选定
  120. customMenu.SetMenuBinding(5, ApplicationCommands.SelectAll);
  121. return popMenu;
  122. }
  123. //编辑中右键
  124. private ContextMenu EditTextContent(object sender)
  125. {
  126. var popMenu = App.Current.FindResource("NoneSelectContentMenu") as ContextMenu;
  127. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  128. ////复制
  129. //customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  130. ////剪切
  131. //customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  132. //全部选定
  133. customMenu.SetMenuBinding(0, ApplicationCommands.SelectAll);
  134. //粘贴
  135. customMenu.SetMenuBinding(1, ApplicationCommands.Paste);
  136. //粘贴并匹配样式
  137. customMenu.SetVisibilityProperty(2, false);
  138. ////删除
  139. //customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  140. return popMenu;
  141. }
  142. #endregion
  143. #region 快捷键
  144. private void ShortCut_KeyDown(object sender, KeyEventArgs e)
  145. {
  146. if (e.Key == Key.Escape)
  147. {
  148. if (PDFViewer != null)
  149. {
  150. if (PDFViewer.ToolManager != null && IsTextEdit == true)
  151. {
  152. PDFViewer.RemovePDFEditEmptyText();
  153. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  154. PDFViewer.ReloadDocument();
  155. ShowPropertyPanel(true);
  156. }
  157. else if (PDFViewer.ToolManager != null && IsImgEdit == true)
  158. {
  159. PDFViewer.RemovePDFEditEmptyText();
  160. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  161. PDFViewer.ReloadDocument();
  162. ShowPropertyPanel(true);
  163. }
  164. if (PDFViewer.MouseMode == MouseModes.PDFEdit)
  165. {
  166. PDFViewer.RemoveTool(false);
  167. }
  168. }
  169. }
  170. }
  171. #endregion
  172. public void AddContent(object obj)
  173. {
  174. if (PDFViewer == null || obj == null || obj as CustomIconToggleBtn == null) return;
  175. var btn = obj as CustomIconToggleBtn;
  176. EnterEditMode((bool)btn.IsChecked, btn.Tag.ToString());
  177. }
  178. private void EditTextMode()
  179. {
  180. }
  181. private void EnterEditMode(bool isCheckMode,string modeType)
  182. {
  183. if (isCheckMode)
  184. {
  185. if (modeType == "Text")
  186. {
  187. IsImgEdit = false;
  188. IsTextEdit = true;
  189. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  190. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  191. PDFViewer.ReloadDocument();
  192. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  193. }
  194. else
  195. {
  196. IsImgEdit = true;
  197. IsTextEdit = false;
  198. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  199. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  200. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  201. PDFViewer.ReloadDocument();
  202. }
  203. }
  204. else
  205. {
  206. IsImgEdit = false;
  207. IsTextEdit = false;
  208. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  209. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
  210. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  211. PDFViewer.ReloadDocument();
  212. }
  213. }
  214. #region Command功能
  215. private void AddText()
  216. {
  217. EnterEditMode(true, "Text");
  218. }
  219. private void AddImg()
  220. {
  221. EnterEditMode(true, "Image");
  222. }
  223. #endregion
  224. private void AddToPropertyPanel(string type, List<PDFEditEvent> e)
  225. {
  226. if (btnToProperty.ContainsKey(type))
  227. {
  228. NavigationParameters parameters = new NavigationParameters();
  229. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  230. parameters.Add(ParameterNames.AnnotEvent, e);
  231. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  232. {
  233. regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters);
  234. }));
  235. ShowPropertyPanel(true);
  236. }
  237. }
  238. private void ShowPropertyPanel(bool show = true)
  239. {
  240. viewContentViewModel.IsPropertyOpen = show;
  241. }
  242. public void OnNavigatedTo(NavigationContext navigationContext)
  243. {
  244. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  245. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  246. if (PDFViewer != null)
  247. {
  248. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  249. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  250. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  251. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  252. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  253. PDFViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler;
  254. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  255. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  256. }
  257. }
  258. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  259. {
  260. if (e != null && e.Count > 0)
  261. {
  262. bool isText = false;
  263. bool isImg = false;
  264. foreach (var item in e)
  265. {
  266. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  267. {
  268. isImg = true;
  269. }
  270. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  271. {
  272. isText = true;
  273. }
  274. }
  275. if (isText == true && isImg == false)
  276. {
  277. AddToPropertyPanel("Text", e);
  278. }
  279. if (isText == false && isImg == true)
  280. {
  281. AddToPropertyPanel("Image", e);
  282. }
  283. if (isText == true && isImg == true)
  284. {
  285. AddToPropertyPanel("TextAndImage", e);
  286. }
  287. }
  288. else
  289. {
  290. AddToPropertyPanel("PropertyPanelContent", null);
  291. }
  292. }
  293. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  294. {
  295. if (e == null)
  296. return;
  297. switch (e.CommandType)
  298. {
  299. case CommandType.Context:
  300. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)//点击空白区域
  301. {
  302. e.PopupMenu = EmptyStateMenu(sender);
  303. }
  304. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  305. {
  306. e.PopupMenu = SelectTextBorder(sender);
  307. }
  308. break;
  309. default:
  310. e.DoCommand();
  311. break;
  312. }
  313. if (e.PopupMenu != null)
  314. {
  315. e.Handle = true;
  316. }
  317. }
  318. private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
  319. {
  320. if (e.NotifyType == CustomNotifyType.PDFEditImage)
  321. {
  322. OpenFileDialog openFileDialog = new OpenFileDialog();
  323. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  324. if (openFileDialog.ShowDialog() == true)
  325. {
  326. PDFViewer.AddPDFEditImage(openFileDialog.FileName);
  327. }
  328. }
  329. }
  330. public bool IsNavigationTarget(NavigationContext navigationContext)
  331. {
  332. return true;
  333. }
  334. public void OnNavigatedFrom(NavigationContext navigationContext)
  335. {
  336. IsImgEdit = false;
  337. IsTextEdit = false;
  338. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  339. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  340. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  341. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  342. ShowPropertyPanel(false);
  343. }
  344. }
  345. }