TextEditToolContentViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. private bool _isTextEdit = false;
  31. public bool IsTextEdit
  32. {
  33. get { return _isTextEdit; }
  34. set { SetProperty(ref _isTextEdit, value); }
  35. }
  36. private bool _isImgEdit = false;
  37. public bool IsImgEdit
  38. {
  39. get { return _isImgEdit; }
  40. set { SetProperty(ref _isImgEdit, value); }
  41. }
  42. #endregion
  43. #region Command
  44. // 添加文本、图像
  45. public DelegateCommand<object> AddContentCommand { get; set; }
  46. public DelegateCommand AddTextCommand { get; set; }
  47. public DelegateCommand AddImgCommand { get; set; }
  48. public DelegateCommand EditTextModeCommand { get; set; }
  49. #endregion
  50. #region 初始化
  51. public TextEditToolContentViewModel(IRegionManager regionManager)
  52. {
  53. regions = regionManager;
  54. InitCommand();
  55. InitBtnToProperty();
  56. }
  57. private void InitCommand()
  58. {
  59. AddContentCommand = new DelegateCommand<object>(AddContent);
  60. AddTextCommand = new DelegateCommand(AddText);
  61. AddImgCommand = new DelegateCommand(AddImg);
  62. EditTextModeCommand = new DelegateCommand(EditTextMode);
  63. }
  64. private void InitBtnToProperty()
  65. {
  66. btnToProperty.Add("Text", "TextEditProperty");
  67. btnToProperty.Add("Image", "ImageEditProperty");
  68. btnToProperty.Add("TextAndImage", "ImageTextEditProperty");
  69. btnToProperty.Add("PropertyPanelContent", "PropertyPanelContent");
  70. }
  71. #endregion
  72. #region 右键菜单
  73. //点击空白处时
  74. private ContextMenu EmptyStateMenu(object sender)
  75. {
  76. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  77. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  78. //粘贴
  79. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  80. //添加文本
  81. customMenu.SetMenuBinding(1, AddTextCommand);
  82. //添加图像
  83. customMenu.SetMenuBinding(2, AddImgCommand);
  84. return popMenu;
  85. }
  86. //选中文字的框
  87. private ContextMenu SelectTextBorder(object sender)
  88. {
  89. var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu;
  90. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  91. //复制
  92. customMenu.SetMenuBinding(1, ApplicationCommands.Copy);
  93. //剪切
  94. customMenu.SetMenuBinding(2, ApplicationCommands.Cut);
  95. //粘贴
  96. customMenu.SetMenuBinding(3, ApplicationCommands.Paste);
  97. //删除
  98. customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  99. customMenu.SetVisibilityProperty(6, false);
  100. return popMenu;
  101. }
  102. //选中文字内容
  103. private ContextMenu SelectTextContent(object sender)
  104. {
  105. var popMenu = App.Current.FindResource("SelectContentMenu") as ContextMenu;
  106. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  107. //复制
  108. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  109. //剪切
  110. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  111. //粘贴
  112. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  113. //粘贴并匹配样式
  114. customMenu.SetVisibilityProperty(3, false);
  115. //删除
  116. customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  117. //全部选定
  118. customMenu.SetMenuBinding(5, ApplicationCommands.SelectAll);
  119. return popMenu;
  120. }
  121. //编辑中右键
  122. private ContextMenu EditTextContent(object sender)
  123. {
  124. var popMenu = App.Current.FindResource("NoneSelectContentMenu") as ContextMenu;
  125. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  126. ////复制
  127. //customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  128. ////剪切
  129. //customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  130. //全部选定
  131. customMenu.SetMenuBinding(0, ApplicationCommands.SelectAll);
  132. //粘贴
  133. customMenu.SetMenuBinding(1, ApplicationCommands.Paste);
  134. //粘贴并匹配样式
  135. customMenu.SetVisibilityProperty(2, false);
  136. ////删除
  137. //customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  138. return popMenu;
  139. }
  140. #endregion
  141. #region 快捷键
  142. private void ShortCut_KeyDown(object sender, KeyEventArgs e)
  143. {
  144. try
  145. {
  146. if (e.Key == Key.Escape)
  147. {
  148. if (PDFViewer != null)
  149. {
  150. //缩小esc的操作范围
  151. if (PDFViewer.ToolManager != null && PDFViewer.GetPDFEditCreateType() == ComPDFKit.PDFPage.CPDFEditType.EditText)
  152. {
  153. PDFViewer.RemovePDFEditEmptyText();
  154. //只有在有画框的时候才进行
  155. if (PDFViewer.MouseMode == MouseModes.PDFEdit&& PDFViewer.ToolManager.HasTool == true)
  156. {
  157. PDFViewer.RemoveTool(false);
  158. }
  159. }
  160. else if (PDFViewer.ToolManager != null && PDFViewer.GetPDFEditCreateType() == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  161. {
  162. PDFViewer.RemovePDFEditEmptyText();
  163. if (PDFViewer.MouseMode == MouseModes.PDFEdit&&PDFViewer.ToolManager.HasTool == true)
  164. {
  165. PDFViewer.RemoveTool(false);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. catch
  172. {
  173. }
  174. }
  175. #endregion
  176. //模式选择进入
  177. public void AddContent(object obj)
  178. {
  179. if (PDFViewer == null || obj == null || obj as CustomIconToggleBtn == null) return;
  180. var btn = obj as CustomIconToggleBtn;
  181. EnterEditMode((bool)btn.IsChecked, btn.Tag.ToString());
  182. }
  183. //编辑按钮逻辑,暂时保留
  184. private void EditTextMode()
  185. {
  186. }
  187. //对应模式的逻辑
  188. private void EnterEditMode(bool isCheckMode,string modeType)
  189. {
  190. if (isCheckMode)
  191. {
  192. if (modeType == "Text")
  193. {
  194. IsImgEdit = false;
  195. IsTextEdit = true;
  196. //只框选文本
  197. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  198. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  199. PDFViewer.ReloadDocument();
  200. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  201. }
  202. else
  203. {
  204. IsImgEdit = true;
  205. IsTextEdit = false;
  206. //只框选图像
  207. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  208. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  209. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  210. PDFViewer.ReloadDocument();
  211. }
  212. }
  213. else
  214. {
  215. IsImgEdit = false;
  216. IsTextEdit = false;
  217. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  218. //文本和图像都框选
  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. //传参数给属性面板
  235. private void AddToPropertyPanel(string type, List<PDFEditEvent> e)
  236. {
  237. if (btnToProperty.ContainsKey(type))
  238. {
  239. NavigationParameters parameters = new NavigationParameters();
  240. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  241. parameters.Add(ParameterNames.AnnotEvent, e);
  242. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  243. {
  244. regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters);
  245. }));
  246. if(e!=null)
  247. {
  248. ShowPropertyPanel(true);
  249. }
  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. //左键激活逻辑
  284. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  285. {
  286. if (e != null && e.Count > 0)
  287. {
  288. bool isText = false;
  289. bool isImg = false;
  290. foreach (var item in e)
  291. {
  292. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  293. {
  294. isImg = true;
  295. }
  296. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  297. {
  298. isText = true;
  299. }
  300. }
  301. if (isText == true && isImg == false)
  302. {
  303. AddToPropertyPanel("Text", e);
  304. }
  305. if (isText == false && isImg == true)
  306. {
  307. AddToPropertyPanel("Image", e);
  308. }
  309. if (isText == true && isImg == true)
  310. {
  311. AddToPropertyPanel("TextAndImage", e);
  312. }
  313. }
  314. else
  315. {
  316. AddToPropertyPanel("PropertyPanelContent", null);
  317. }
  318. }
  319. //右键点击逻辑
  320. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  321. {
  322. if (e == null)
  323. return;
  324. switch (e.CommandType)
  325. {
  326. case CommandType.Context:
  327. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)//点击空白区域
  328. {
  329. e.PopupMenu = EmptyStateMenu(sender);
  330. }
  331. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  332. {
  333. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  334. {
  335. //文字编辑
  336. if (e.PressOnBorder == true)
  337. {
  338. e.PopupMenu = SelectTextBorder(sender);
  339. }
  340. else if(e.PressOnSelectedText == true)
  341. {
  342. e.PopupMenu = SelectTextContent(sender);
  343. }
  344. else if(e.SelectText=="")
  345. {
  346. e.PopupMenu= EditTextContent(sender);
  347. }
  348. else
  349. {
  350. e.PopupMenu = SelectTextContent(sender);
  351. }
  352. }
  353. }
  354. break;
  355. default:
  356. e.DoCommand();
  357. break;
  358. }
  359. if (e.PopupMenu != null)
  360. {
  361. e.Handle = true;
  362. }
  363. }
  364. //图片添加逻辑
  365. private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
  366. {
  367. if (e.NotifyType == CustomNotifyType.PDFEditImage)
  368. {
  369. OpenFileDialog openFileDialog = new OpenFileDialog();
  370. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  371. if (openFileDialog.ShowDialog() == true)
  372. {
  373. PDFViewer.AddPDFEditImage(openFileDialog.FileName,500,500);
  374. }
  375. }
  376. }
  377. public bool IsNavigationTarget(NavigationContext navigationContext)
  378. {
  379. return true;
  380. }
  381. public void OnNavigatedFrom(NavigationContext navigationContext)
  382. {
  383. IsImgEdit = false;
  384. IsTextEdit = false;
  385. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  386. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  387. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  388. PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler;
  389. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  390. ShowPropertyPanel(false);
  391. }
  392. }
  393. }