TextEditToolContentViewModel.cs 16 KB

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