TextEditToolContentViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. try
  147. {
  148. if (e.Key == Key.Escape)
  149. {
  150. if (PDFViewer != null)
  151. {
  152. if (PDFViewer.ToolManager != null && PDFViewer.GetPDFEditCreateType() == ComPDFKit.PDFPage.CPDFEditType.EditText)
  153. {
  154. PDFViewer.RemovePDFEditEmptyText();
  155. //PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  156. //PDFViewer.ReloadDocument();
  157. //ShowPropertyPanel(true);
  158. if (PDFViewer.MouseMode == MouseModes.PDFEdit&& PDFViewer.ToolManager.HasTool == true)
  159. {
  160. PDFViewer.RemoveTool(false);
  161. }
  162. }
  163. else if (PDFViewer.ToolManager != null && PDFViewer.GetPDFEditCreateType() == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  164. {
  165. PDFViewer.RemovePDFEditEmptyText();
  166. //PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  167. //PDFViewer.ReloadDocument();
  168. //ShowPropertyPanel(true);
  169. if (PDFViewer.MouseMode == MouseModes.PDFEdit&&PDFViewer.ToolManager.HasTool == true)
  170. {
  171. PDFViewer.RemoveTool(false);
  172. }
  173. }
  174. }
  175. }
  176. }
  177. catch
  178. {
  179. }
  180. }
  181. #endregion
  182. public void AddContent(object obj)
  183. {
  184. if (PDFViewer == null || obj == null || obj as CustomIconToggleBtn == null) return;
  185. var btn = obj as CustomIconToggleBtn;
  186. EnterEditMode((bool)btn.IsChecked, btn.Tag.ToString());
  187. }
  188. private void EditTextMode()
  189. {
  190. }
  191. private void EnterEditMode(bool isCheckMode,string modeType)
  192. {
  193. if (isCheckMode)
  194. {
  195. if (modeType == "Text")
  196. {
  197. IsImgEdit = false;
  198. IsTextEdit = true;
  199. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  200. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  201. PDFViewer.ReloadDocument();
  202. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  203. }
  204. else
  205. {
  206. IsImgEdit = true;
  207. IsTextEdit = false;
  208. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  209. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  210. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  211. PDFViewer.ReloadDocument();
  212. }
  213. }
  214. else
  215. {
  216. IsImgEdit = false;
  217. IsTextEdit = false;
  218. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  219. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
  220. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  221. PDFViewer.ReloadDocument();
  222. }
  223. }
  224. #region Command功能
  225. private void AddText()
  226. {
  227. EnterEditMode(true, "Text");
  228. }
  229. private void AddImg()
  230. {
  231. EnterEditMode(true, "Image");
  232. }
  233. #endregion
  234. private void AddToPropertyPanel(string type, List<PDFEditEvent> e)
  235. {
  236. if (btnToProperty.ContainsKey(type))
  237. {
  238. NavigationParameters parameters = new NavigationParameters();
  239. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  240. parameters.Add(ParameterNames.AnnotEvent, e);
  241. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  242. {
  243. regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters);
  244. }));
  245. ShowPropertyPanel(true);
  246. }
  247. }
  248. private void ShowPropertyPanel(bool show = true)
  249. {
  250. viewContentViewModel.IsPropertyOpen = show;
  251. }
  252. public void OnNavigatedTo(NavigationContext navigationContext)
  253. {
  254. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  255. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  256. if (PDFViewer != null)
  257. {
  258. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  259. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  260. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  261. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  262. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  263. PDFViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler;
  264. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  265. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  266. }
  267. }
  268. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  269. {
  270. if (e != null && e.Count > 0)
  271. {
  272. bool isText = false;
  273. bool isImg = false;
  274. foreach (var item in e)
  275. {
  276. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  277. {
  278. isImg = true;
  279. }
  280. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  281. {
  282. isText = true;
  283. }
  284. }
  285. if (isText == true && isImg == false)
  286. {
  287. AddToPropertyPanel("Text", e);
  288. }
  289. if (isText == false && isImg == true)
  290. {
  291. AddToPropertyPanel("Image", e);
  292. }
  293. if (isText == true && isImg == true)
  294. {
  295. AddToPropertyPanel("TextAndImage", e);
  296. }
  297. }
  298. else
  299. {
  300. AddToPropertyPanel("PropertyPanelContent", null);
  301. }
  302. }
  303. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  304. {
  305. if (e == null)
  306. return;
  307. switch (e.CommandType)
  308. {
  309. case CommandType.Context:
  310. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)//点击空白区域
  311. {
  312. e.PopupMenu = EmptyStateMenu(sender);
  313. }
  314. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  315. {
  316. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  317. {
  318. //文字编辑
  319. if (e.PressOnBorder == true)
  320. {
  321. e.PopupMenu = SelectTextBorder(sender);
  322. }
  323. else if(e.PressOnSelectedText == true)
  324. {
  325. e.PopupMenu = SelectTextContent(sender);
  326. }
  327. else if(e.SelectText=="")
  328. {
  329. e.PopupMenu= EditTextContent(sender);
  330. }
  331. else
  332. {
  333. e.PopupMenu = SelectTextContent(sender);
  334. }
  335. }
  336. }
  337. break;
  338. default:
  339. e.DoCommand();
  340. break;
  341. }
  342. if (e.PopupMenu != null)
  343. {
  344. e.Handle = true;
  345. }
  346. }
  347. private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
  348. {
  349. if (e.NotifyType == CustomNotifyType.PDFEditImage)
  350. {
  351. OpenFileDialog openFileDialog = new OpenFileDialog();
  352. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  353. if (openFileDialog.ShowDialog() == true)
  354. {
  355. PDFViewer.AddPDFEditImage(openFileDialog.FileName);
  356. }
  357. }
  358. }
  359. public bool IsNavigationTarget(NavigationContext navigationContext)
  360. {
  361. return true;
  362. }
  363. public void OnNavigatedFrom(NavigationContext navigationContext)
  364. {
  365. IsImgEdit = false;
  366. IsTextEdit = false;
  367. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  368. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  369. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  370. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  371. ShowPropertyPanel(false);
  372. }
  373. }
  374. }