TextEditToolContentViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. ShowPropertyPanel(true);
  248. }
  249. }
  250. private void ShowPropertyPanel(bool show = true)
  251. {
  252. viewContentViewModel.IsPropertyOpen = show;
  253. }
  254. public void OnNavigatedTo(NavigationContext navigationContext)
  255. {
  256. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  257. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  258. if (PDFViewer != null)
  259. {
  260. //左键激活
  261. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  262. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  263. //右键
  264. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  265. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  266. //图片添加
  267. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  268. PDFViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler;
  269. //选中
  270. PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler;
  271. PDFViewer.PDFEditHandler += PDFViewer_PDFEditHandler;
  272. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  273. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  274. }
  275. }
  276. private void PDFViewer_PDFEditHandler(object sender, List<PDFEditSelectionData> e)
  277. {
  278. PDFViewer.SelectPDFEdit(e,true);
  279. }
  280. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  281. {
  282. if (e != null && e.Count > 0)
  283. {
  284. bool isText = false;
  285. bool isImg = false;
  286. foreach (var item in e)
  287. {
  288. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  289. {
  290. isImg = true;
  291. }
  292. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  293. {
  294. isText = true;
  295. }
  296. }
  297. if (isText == true && isImg == false)
  298. {
  299. AddToPropertyPanel("Text", e);
  300. }
  301. if (isText == false && isImg == true)
  302. {
  303. AddToPropertyPanel("Image", e);
  304. }
  305. if (isText == true && isImg == true)
  306. {
  307. AddToPropertyPanel("TextAndImage", e);
  308. }
  309. }
  310. else
  311. {
  312. AddToPropertyPanel("PropertyPanelContent", null);
  313. }
  314. }
  315. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  316. {
  317. if (e == null)
  318. return;
  319. switch (e.CommandType)
  320. {
  321. case CommandType.Context:
  322. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)//点击空白区域
  323. {
  324. e.PopupMenu = EmptyStateMenu(sender);
  325. }
  326. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  327. {
  328. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  329. {
  330. //文字编辑
  331. if (e.PressOnBorder == true)
  332. {
  333. e.PopupMenu = SelectTextBorder(sender);
  334. }
  335. else if(e.PressOnSelectedText == true)
  336. {
  337. e.PopupMenu = SelectTextContent(sender);
  338. }
  339. else if(e.SelectText=="")
  340. {
  341. e.PopupMenu= EditTextContent(sender);
  342. }
  343. else
  344. {
  345. e.PopupMenu = SelectTextContent(sender);
  346. }
  347. }
  348. }
  349. break;
  350. default:
  351. e.DoCommand();
  352. break;
  353. }
  354. if (e.PopupMenu != null)
  355. {
  356. e.Handle = true;
  357. }
  358. }
  359. private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
  360. {
  361. if (e.NotifyType == CustomNotifyType.PDFEditImage)
  362. {
  363. OpenFileDialog openFileDialog = new OpenFileDialog();
  364. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  365. if (openFileDialog.ShowDialog() == true)
  366. {
  367. PDFViewer.AddPDFEditImage(openFileDialog.FileName,500,500);
  368. }
  369. }
  370. }
  371. public bool IsNavigationTarget(NavigationContext navigationContext)
  372. {
  373. return true;
  374. }
  375. public void OnNavigatedFrom(NavigationContext navigationContext)
  376. {
  377. IsImgEdit = false;
  378. IsTextEdit = false;
  379. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  380. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  381. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  382. PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler;
  383. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  384. ShowPropertyPanel(false);
  385. }
  386. }
  387. }