TextEditToolContentViewModel.cs 12 KB

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