TextEditToolContentViewModel.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. using System.ComponentModel;
  22. using System.IO;
  23. using System.Drawing;
  24. using System.Drawing.Imaging;
  25. using PDF_Master.EventAggregators;
  26. using Prism.Events;
  27. using System.Windows.Media.Imaging;
  28. using PDFReader_WPF.Helper;
  29. using PDF_Master.Properties;
  30. namespace PDF_Master.ViewModels.Tools
  31. {
  32. public class TextEditToolContentViewModel: BindableBase, INavigationAware
  33. {
  34. #region 属性与变量
  35. public ViewContentViewModel viewContentViewModel;
  36. private CPDFViewer PDFViewer;
  37. private IEventAggregator events;
  38. private IRegionManager regions;
  39. /// <summary>
  40. /// 用于区分事件的唯一码
  41. /// </summary>
  42. private string unicode;
  43. private Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
  44. private bool _isTextEdit = false;
  45. public bool IsTextEdit
  46. {
  47. get { return _isTextEdit; }
  48. set { SetProperty(ref _isTextEdit, value);
  49. }
  50. }
  51. private bool _isImgEdit = false;
  52. public bool IsImgEdit
  53. {
  54. get { return _isImgEdit; }
  55. set
  56. {
  57. SetProperty(ref _isImgEdit, value);
  58. }
  59. }
  60. private bool _flg=false;
  61. public bool flg
  62. {
  63. get { return _flg; }
  64. set { SetProperty(ref _flg, value); }
  65. }
  66. /// <summary>
  67. /// 替换图片指令
  68. /// </summary>
  69. private bool _ReplaceImgflg = false;
  70. public bool ReplaceImgflg
  71. {
  72. get { return _ReplaceImgflg; }
  73. set { SetProperty(ref _ReplaceImgflg, value); }
  74. }
  75. /// <summary>
  76. /// 完成裁剪图片指令
  77. /// </summary>
  78. private bool _CropImgflg = false;
  79. public bool CropImgflg
  80. {
  81. get { return _CropImgflg; }
  82. set { SetProperty(ref _CropImgflg, value); }
  83. }
  84. /// <summary>
  85. /// 刷新预览图片指令
  86. /// </summary>
  87. private bool _REImgflg = false;
  88. public bool REImgflg
  89. {
  90. get { return _REImgflg; }
  91. set { SetProperty(ref _REImgflg, value); }
  92. }
  93. #endregion
  94. #region Command
  95. // 添加文本、图像
  96. public DelegateCommand<object> AddContentCommand { get; set; }
  97. public DelegateCommand AddTextCommand { get; set; }
  98. public DelegateCommand AddImgCommand { get; set; }
  99. public DelegateCommand EditTextModeCommand { get; set; }
  100. #endregion
  101. #region 初始化
  102. public TextEditToolContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
  103. {
  104. events = eventAggregator;
  105. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  106. regions = regionManager;
  107. InitCommand();
  108. InitBtnToProperty();
  109. }
  110. private void InitCommand()
  111. {
  112. AddContentCommand = new DelegateCommand<object>(AddContent);
  113. AddTextCommand = new DelegateCommand(AddText);
  114. AddImgCommand = new DelegateCommand(AddImg);
  115. EditTextModeCommand = new DelegateCommand(EditTextMode);
  116. }
  117. private void InitBtnToProperty()
  118. {
  119. btnToProperty.Add("Text", "TextEditProperty");
  120. btnToProperty.Add("Image", "ImageEditProperty");
  121. btnToProperty.Add("TextAndImage", "ImageTextEditProperty");
  122. btnToProperty.Add("PropertyPanelContent", "PropertyPanelContent");
  123. }
  124. #endregion
  125. #region 右键菜单
  126. //点击空白处时
  127. private ContextMenu EmptyStateMenu(object sender)
  128. {
  129. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  130. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  131. //粘贴
  132. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  133. //添加文本
  134. customMenu.SetMenuBinding(1, AddTextCommand);
  135. //添加图像
  136. customMenu.SetMenuBinding(2, AddImgCommand);
  137. return popMenu;
  138. }
  139. //选中文字的框
  140. private ContextMenu SelectTextBorder(object sender)
  141. {
  142. var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu;
  143. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  144. //复制
  145. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  146. //剪切
  147. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  148. //粘贴
  149. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  150. //删除
  151. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  152. customMenu.SetVisibilityProperty(6, false);
  153. return popMenu;
  154. }
  155. //选中文字内容
  156. private ContextMenu SelectTextContent(object sender)
  157. {
  158. var popMenu = App.Current.FindResource("SelectContentMenu") as ContextMenu;
  159. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  160. //复制
  161. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  162. //剪切
  163. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  164. //粘贴
  165. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  166. //粘贴并匹配样式
  167. customMenu.SetVisibilityProperty(3, false);
  168. //删除
  169. customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  170. //全部选定
  171. customMenu.SetMenuBinding(5, ApplicationCommands.SelectAll);
  172. return popMenu;
  173. }
  174. //编辑中右键
  175. private ContextMenu EditTextContent(object sender)
  176. {
  177. var popMenu = App.Current.FindResource("NoneSelectContentMenu") as ContextMenu;
  178. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  179. ////复制
  180. //customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  181. ////剪切
  182. //customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  183. //全部选定
  184. customMenu.SetMenuBinding(0, ApplicationCommands.SelectAll);
  185. //粘贴
  186. customMenu.SetMenuBinding(1, ApplicationCommands.Paste);
  187. //粘贴并匹配样式
  188. customMenu.SetVisibilityProperty(2, false);
  189. ////删除
  190. //customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  191. return popMenu;
  192. }
  193. #endregion
  194. #region 图片右键菜单
  195. //选中图像时
  196. private ContextMenu SelectImgPDFEdit(object sender)
  197. {
  198. var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu;
  199. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  200. //复制
  201. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  202. //剪切
  203. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  204. //粘贴
  205. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  206. //删除
  207. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  208. //裁剪
  209. customMenu.SetMenuBinding(4, new DelegateCommand(CropMode));
  210. //替换
  211. customMenu.SetMenuBinding(5, new DelegateCommand(ReplaceImg));
  212. //导出
  213. customMenu.SetMenuBinding(6, new DelegateCommand(ExportImg));
  214. return popMenu;
  215. }
  216. //选中裁剪图像时
  217. private ContextMenu CropImgPDFEdit(object sender)
  218. {
  219. var popMenu = App.Current.FindResource("CropImgMenu") as ContextMenu;
  220. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  221. //确认裁剪
  222. customMenu.SetMenuBinding(0, new DelegateCommand(CropImg));
  223. //取消裁剪
  224. customMenu.SetMenuBinding(1, new DelegateCommand(CancelCropImg));
  225. //还原裁剪
  226. customMenu.SetMenuBinding(2, new DelegateCommand(RestoreCropImg));
  227. return popMenu;
  228. }
  229. //多选图片右键
  230. private ContextMenu SelectMoreImage(object sender)
  231. {
  232. var popMenu = App.Current.FindResource("SelectMoreImageMenu") as ContextMenu;
  233. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  234. //复制
  235. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  236. //剪切
  237. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  238. //粘贴
  239. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  240. //删除
  241. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  242. //导出
  243. customMenu.SetMenuBinding(4, new DelegateCommand(ExportImg));
  244. return popMenu;
  245. }
  246. #endregion
  247. #region 快捷键
  248. private void ShortCut_KeyDown(object sender, KeyEventArgs e)
  249. {
  250. try
  251. {
  252. if (e.Key == Key.Escape)
  253. {
  254. if (PDFViewer != null)
  255. {
  256. //缩小esc的操作范围
  257. if (PDFViewer.ToolManager != null && PDFViewer.GetPDFEditCreateType() == ComPDFKit.PDFPage.CPDFEditType.EditText)
  258. {
  259. if(PDFViewer.GetPDFEditSelectionCount(ComPDFKit.PDFPage.CPDFEditType.EditText) ==0)
  260. {
  261. PDFViewer.RemovePDFEditEmptyText();
  262. //只有在有画框的时候才进行
  263. if (PDFViewer.MouseMode == MouseModes.PDFEdit&& PDFViewer.ToolManager.HasTool == true)
  264. {
  265. PDFViewer.RemoveTool(false);
  266. }
  267. else if (IsTextEdit == true || IsImgEdit == true)
  268. {
  269. IsImgEdit = false;
  270. IsTextEdit = false;
  271. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  272. //文本和图像都框选
  273. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
  274. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  275. PDFViewer.ReloadDocument();
  276. ShowPropertyPanel(false);
  277. }
  278. }
  279. {
  280. PDFViewer.ClearSelectPDFEdit();
  281. PDFViewer.RemovePDFEditEmptyText();
  282. PDFViewer.ReloadDocument();
  283. }
  284. }
  285. else if (PDFViewer.ToolManager != null && PDFViewer.GetPDFEditCreateType() == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  286. {
  287. if (PDFViewer.GetPDFEditSelectionCount(ComPDFKit.PDFPage.CPDFEditType.EditImage) == 0)
  288. {
  289. PDFViewer.RemovePDFEditEmptyText();
  290. if (PDFViewer.MouseMode == MouseModes.PDFEdit && PDFViewer.ToolManager.HasTool == true)
  291. {
  292. PDFViewer.RemoveTool(false);
  293. }
  294. else if (IsTextEdit == true || IsImgEdit == true)
  295. {
  296. IsImgEdit = false;
  297. IsTextEdit = false;
  298. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  299. //文本和图像都框选
  300. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
  301. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  302. PDFViewer.ReloadDocument();
  303. ShowPropertyPanel(false);
  304. }
  305. }
  306. else
  307. {
  308. PDFViewer.ClearSelectPDFEdit();
  309. PDFViewer.ReloadDocument();
  310. }
  311. }
  312. }
  313. }
  314. }
  315. catch
  316. {
  317. }
  318. }
  319. #endregion
  320. //模式选择进入
  321. public void AddContent(object obj)
  322. {
  323. if (PDFViewer == null || obj == null || obj as CustomIconToggleBtn == null) return;
  324. //判断是否已退出登录,限制编辑操作
  325. if (!App.IsLogin)
  326. {
  327. IsImgEdit = false;
  328. IsTextEdit = false;
  329. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.None);
  330. PDFViewer.ReloadDocument();
  331. App.mainWindowViewModel.OpenLogin();
  332. return;
  333. }
  334. var btn = obj as CustomIconToggleBtn;
  335. EnterEditMode((bool)btn.IsChecked, btn.Tag.ToString());
  336. }
  337. //编辑按钮逻辑,暂时保留
  338. private void EditTextMode()
  339. {
  340. }
  341. //对应模式的逻辑
  342. private void EnterEditMode(bool isCheckMode,string modeType)
  343. {
  344. if (isCheckMode)
  345. {
  346. if (modeType == "Text")
  347. {
  348. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_EditPDF, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  349. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.EditText);
  350. DataTrackingHelper.IsClearEntryPath = true;
  351. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_EditPDF, "SubTbr_Btn", "Btn_SubTbr_AddText");
  352. IsImgEdit = false;
  353. IsTextEdit = true;
  354. //只框选文本
  355. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  356. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  357. PDFViewer.ReloadDocument();
  358. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  359. AddToPropertyPanel("Text", null);
  360. ShowPropertyPanel(true);
  361. }
  362. else
  363. {
  364. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_EditPDF, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  365. DataTrackingHelper.IsClearEntryPath = true;
  366. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.EditImage);
  367. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_EditPDF, "SubTbr_Btn", "Btn_SubTbr_AddImage");
  368. IsImgEdit = true;
  369. IsTextEdit = false;
  370. //只框选图像
  371. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  372. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  373. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  374. PDFViewer.ReloadDocument();
  375. AddToPropertyPanel("PropertyPanelContent", null);
  376. ShowPropertyPanel(true);
  377. }
  378. }
  379. else
  380. {
  381. IsImgEdit = false;
  382. IsTextEdit = false;
  383. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  384. //文本和图像都框选
  385. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
  386. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  387. PDFViewer.ReloadDocument();
  388. ShowPropertyPanel(false);
  389. }
  390. }
  391. #region Command功能
  392. private void AddText()
  393. {
  394. EnterEditMode(true, "Text");
  395. }
  396. private void AddImg()
  397. {
  398. EnterEditMode(true, "Image");
  399. }
  400. //进入裁剪模式
  401. private void CropMode()
  402. {
  403. if (TextEditEvent != null)
  404. {
  405. TextEditEvent.ClipImage = true;
  406. TextEditEvent.UpdatePDFEditByEventArgs();
  407. flg = true;
  408. }
  409. }
  410. //取消裁剪
  411. private void CancelCropImg()
  412. {
  413. if (TextEditEvent != null)
  414. {
  415. TextEditEvent.ClipImage = false;
  416. TextEditEvent.CancelClip();
  417. flg = false;
  418. TextEditEvent.UpdatePDFEditByEventArgs();
  419. }
  420. }
  421. //还原裁剪
  422. private void RestoreCropImg()
  423. {
  424. if (TextEditEvent != null)
  425. {
  426. TextEditEvent.RestoreClip();
  427. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  428. }
  429. }
  430. //完成裁剪
  431. private void CropImg()
  432. {
  433. CropImgflg = true;
  434. }
  435. //导出图片
  436. private void ExportImg()
  437. {
  438. if (PDFViewer == null || TextEditEvent == null || TextEditEvent.EditType != ComPDFKit.PDFPage.CPDFEditType.EditImage) return;
  439. System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
  440. folder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  441. if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  442. {
  443. if (string.IsNullOrEmpty(folder.SelectedPath))
  444. return;
  445. var keyValueList = PDFViewer.GetSelectedImages();
  446. int i = 0;
  447. foreach (var bitmap in keyValueList)
  448. {
  449. foreach (var bitmapItem in bitmap.Value)
  450. {
  451. bitmapItem.Save(folder.SelectedPath + "\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
  452. i++;
  453. }
  454. }
  455. var strFilePath = folder.SelectedPath + "\\0.png";
  456. CommonHelper.ShowFileBrowser(strFilePath);
  457. }
  458. }
  459. //替换图片
  460. private void ReplaceImg()
  461. {
  462. ReplaceImgflg = true;
  463. }
  464. //选中的图像
  465. private System.Windows.Media.Imaging.BitmapSource _currentImg;
  466. public System.Windows.Media.Imaging.BitmapSource CurrentImg { get { return _currentImg; } set { SetProperty(ref _currentImg, value); } }
  467. private void GetImagePreView()
  468. {
  469. try
  470. {
  471. var list = PDFViewer.GetSelectedImages();
  472. if (list != null && list.Count > 0)
  473. {
  474. System.Drawing.Bitmap bitmap = null;
  475. foreach (var item in list)
  476. {
  477. if (item.Value.Count > 0)
  478. {
  479. bitmap = item.Value[0];
  480. break;
  481. }
  482. }
  483. if (bitmap != null)
  484. {
  485. IntPtr ip = bitmap.GetHbitmap();
  486. System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
  487. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  488. CurrentImg = bitmapSource;
  489. }
  490. }
  491. }
  492. catch
  493. {
  494. }
  495. }
  496. #endregion
  497. //传参数给属性面板
  498. private void AddToPropertyPanel(string type, List<PDFEditEvent> e)
  499. {
  500. if (btnToProperty.ContainsKey(type))
  501. {
  502. NavigationParameters parameters = new NavigationParameters();
  503. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  504. parameters.Add(ParameterNames.AnnotEvent, e);
  505. parameters.Add(ParameterNames.TextEditToolContentViewModel, this);
  506. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  507. {
  508. regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters);
  509. }));
  510. if(e!=null)
  511. {
  512. ShowPropertyPanel(true);
  513. }
  514. else if(e==null&&IsImgEdit==false&&IsTextEdit==false)
  515. {
  516. ShowPropertyPanel(false);
  517. }
  518. }
  519. }
  520. //控制属性面板是否展示
  521. private void ShowPropertyPanel(bool show = true)
  522. {
  523. viewContentViewModel.IsPropertyOpen = show;
  524. }
  525. private void OnViewModel1PropertyChanged(object sender, PropertyChangedEventArgs e)
  526. {
  527. if (e.PropertyName == "TabSelectedIndex")
  528. {
  529. if (viewContentViewModel.ToolTooRow == new GridLength(40))
  530. {
  531. IsImgEdit = false;
  532. IsTextEdit = false;
  533. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
  534. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  535. PDFViewer.ReloadDocument();
  536. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  537. AddToPropertyPanel("PropertyPanelContent", null);
  538. ShowPropertyPanel(false);
  539. }
  540. else
  541. {
  542. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.None);
  543. PDFViewer.ReloadDocument();
  544. }
  545. }
  546. }
  547. public void OnNavigatedTo(NavigationContext navigationContext)
  548. {
  549. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  550. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  551. if (PDFViewer != null)
  552. {
  553. viewContentViewModel.PropertyChanged += OnViewModel1PropertyChanged;
  554. //左键激活
  555. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  556. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  557. //右键
  558. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  559. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  560. //图片添加
  561. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  562. PDFViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler;
  563. //粘贴后选中
  564. PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler;
  565. PDFViewer.PDFEditHandler += PDFViewer_PDFEditHandler;
  566. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  567. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  568. }
  569. }
  570. private void PDFViewer_PDFEditHandler(object sender, List<PDFEditSelectionData> e)
  571. {
  572. PDFViewer.SelectPDFEdit(e,true);
  573. }
  574. protected PDFEditEvent TextEditEvent;
  575. //左键激活逻辑
  576. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  577. {
  578. //判断是否已退出登录,限制编辑操作
  579. if (!App.IsLogin)
  580. {
  581. IsImgEdit = false;
  582. IsTextEdit = false;
  583. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.None);
  584. PDFViewer.ReloadDocument();
  585. App.mainWindowViewModel.OpenLogin();
  586. return;
  587. }
  588. if (e != null && e.Count > 0)
  589. {
  590. TextEditEvent=e[0];
  591. bool isText = false;
  592. bool isImg = false;
  593. foreach (var item in e)
  594. {
  595. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  596. {
  597. isImg = true;
  598. }
  599. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  600. {
  601. isText = true;
  602. }
  603. }
  604. if (isText == true && isImg == false)
  605. {
  606. AddToPropertyPanel("Text", e);
  607. }
  608. if (isText == false && isImg == true)
  609. {
  610. AddToPropertyPanel("Image", e);
  611. }
  612. if (isText == true && isImg == true)
  613. {
  614. AddToPropertyPanel("TextAndImage", e);
  615. }
  616. }
  617. else if(IsTextEdit==true)
  618. {
  619. AddToPropertyPanel("PropertyPanelContent", null);
  620. AddToPropertyPanel("Text", null);
  621. }
  622. else
  623. {
  624. AddToPropertyPanel("PropertyPanelContent", null);
  625. }
  626. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  627. }
  628. //右键点击逻辑
  629. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  630. {
  631. if (e == null)
  632. return;
  633. switch (e.CommandType)
  634. {
  635. case CommandType.Context:
  636. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)//点击空白区域
  637. {
  638. e.PopupMenu = EmptyStateMenu(sender);
  639. }
  640. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  641. {
  642. //文字编辑
  643. if (e.PressOnBorder == true)
  644. {
  645. e.PopupMenu = SelectTextBorder(sender);
  646. }
  647. else if(e.PressOnSelectedText == true)
  648. {
  649. e.PopupMenu = SelectTextContent(sender);
  650. }
  651. else if(e.SelectText=="")
  652. {
  653. e.PopupMenu= EditTextContent(sender);
  654. }
  655. else
  656. {
  657. e.PopupMenu = SelectTextContent(sender);
  658. }
  659. }
  660. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  661. {
  662. if (e.IsClipState == true)
  663. {
  664. e.PopupMenu = CropImgPDFEdit(sender);
  665. }
  666. else
  667. {
  668. e.PopupMenu = SelectImgPDFEdit(sender);
  669. }
  670. }
  671. break;
  672. default:
  673. e.DoCommand();
  674. break;
  675. }
  676. if (e.PopupMenu != null)
  677. {
  678. e.Handle = true;
  679. }
  680. flg = e.IsClipState;
  681. }
  682. //图片添加逻辑
  683. private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
  684. {
  685. //图片创建
  686. if (e.NotifyType == CustomNotifyType.PDFEditImageCreate)
  687. {
  688. OpenFileDialog openFileDialog = new OpenFileDialog();
  689. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  690. if (openFileDialog.ShowDialog() == true)
  691. {
  692. PDFViewer.AddPDFEditImage(CompressImage(openFileDialog.FileName, 800), 500,500);
  693. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  694. }
  695. }
  696. //图片调整大小
  697. else if(e.NotifyType == CustomNotifyType.PDFEditImageResize)
  698. {
  699. REImgflg = true;
  700. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  701. }
  702. }
  703. /// <summary>
  704. /// 压缩
  705. /// </summary>
  706. /// <param name="filePath"></param>
  707. /// <param name="maxwidth"></param>
  708. /// <returns></returns>
  709. public static string CompressImage(string filePath, int maxwidth = 600)
  710. {
  711. try
  712. {
  713. string sourcepath = filePath;
  714. var guid = Guid.NewGuid().ToString();
  715. string folder = Path.Combine(App.CurrentPath, "Temp");
  716. if (!Directory.Exists(folder))
  717. {
  718. Directory.CreateDirectory(folder);
  719. }
  720. var path = System.IO.Path.Combine(App.CurrentPath, "Temp", guid);
  721. Bitmap bitmap = new Bitmap(sourcepath);
  722. var b = bitmap;
  723. //bitmap.Dispose();
  724. double scale = Math.Min((double)maxwidth / b.Width, (double)maxwidth / b.Height);
  725. scale = Math.Min(scale, 1);
  726. System.Drawing.Size newsize = new System.Drawing.Size(maxwidth, maxwidth);
  727. newsize.Width = (int)(scale * b.Width);
  728. newsize.Height = (int)(scale * b.Height);
  729. if (!File.Exists(path))
  730. {
  731. if (CheckTextFile(sourcepath) == FileExtension.PNG)
  732. {
  733. using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
  734. {
  735. bp.Save(path, ImageFormat.Png);
  736. }
  737. }
  738. else
  739. {
  740. using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
  741. {
  742. bp.Save(path, ImageFormat.Jpeg);
  743. }
  744. }
  745. }
  746. return path;
  747. }
  748. catch
  749. {
  750. return filePath;
  751. }
  752. }
  753. /// <summary>
  754. /// 根据图片数据判断图片类型
  755. /// </summary>
  756. /// <param name="fileName"></param>
  757. /// <returns></returns>
  758. public static FileExtension CheckTextFile(string fileName)
  759. {
  760. FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  761. System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
  762. string fileType = string.Empty; ;
  763. try
  764. {
  765. byte data = br.ReadByte();
  766. fileType += data.ToString();
  767. data = br.ReadByte();
  768. fileType += data.ToString();
  769. FileExtension extension;
  770. try
  771. {
  772. extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
  773. }
  774. catch
  775. {
  776. extension = FileExtension.VALIDFILE;
  777. }
  778. return extension;
  779. }
  780. catch (Exception ex)
  781. {
  782. throw ex;
  783. }
  784. finally
  785. {
  786. if (fs != null)
  787. {
  788. fs.Close();
  789. br.Close();
  790. }
  791. }
  792. }
  793. public bool IsNavigationTarget(NavigationContext navigationContext)
  794. {
  795. return true;
  796. }
  797. public void OnNavigatedFrom(NavigationContext navigationContext)
  798. {
  799. IsImgEdit = false;
  800. IsTextEdit = false;
  801. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  802. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  803. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  804. PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler;
  805. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  806. ShowPropertyPanel(false);
  807. }
  808. }
  809. }