ImageEditPropertyViewModel.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Win32;
  4. using PDF_Master.EventAggregators;
  5. using PDF_Master.Helper;
  6. using PDF_Master.Model;
  7. using PDF_Master.ViewModels.Tools;
  8. using Prism.Commands;
  9. using Prism.Events;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.ComponentModel;
  15. using System.Drawing;
  16. using System.Drawing.Imaging;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using System.Windows;
  22. using System.Windows.Controls;
  23. using System.Windows.Input;
  24. namespace PDF_Master.ViewModels.PropertyPanel.PDFEdit
  25. {
  26. public class ImageEditPropertyViewModel : PDFEditVM, INavigationAware
  27. {
  28. public TextEditToolContentViewModel TextEditToolContentViewModel;
  29. private IEventAggregator events;
  30. /// <summary>
  31. /// 用于区分事件的唯一码
  32. /// </summary>
  33. private string unicode;
  34. #region 快捷键
  35. private void ShortCut_KeyDown(object sender, KeyEventArgs e)
  36. {
  37. if (e.Key == Key.Escape)
  38. {
  39. if (PDFViewer != null)
  40. {
  41. if (PDFViewer.ToolManager != null && IsCrop == true)
  42. {
  43. CancelCropImg();
  44. }
  45. }
  46. }
  47. else if(e.Key==Key.Enter)
  48. {
  49. CropImg();
  50. }
  51. }
  52. #endregion
  53. #region 属性和变量
  54. //防止自动保存属性值
  55. private bool isCanSave = false;
  56. public event EventHandler ClearCheckedAglin;
  57. #region 是否为多选内容
  58. private bool _isMultiSelectImage = false;
  59. public bool IsMultiSelectImage { get { return _isMultiSelectImage; } set { SetProperty(ref _isMultiSelectImage, value); } }
  60. #endregion
  61. private string _opacity1= "Opacity";
  62. public string Opacity
  63. {
  64. get { return _opacity1; }
  65. set { SetProperty(ref _opacity1, value); }
  66. }
  67. //不透明度
  68. private double _opacity;
  69. public double OpacityUI
  70. {
  71. get { return _opacity; }
  72. set { SetProperty(ref _opacity, value); }
  73. }
  74. private int _OpacitySelectedIndex = 1;
  75. public int OpacitySelectedIndex
  76. {
  77. get { return _OpacitySelectedIndex; }
  78. set { SetProperty(ref _OpacitySelectedIndex, value); }
  79. }
  80. private double _transpent;
  81. public double Transpent
  82. {
  83. get { return _transpent; }
  84. set
  85. {
  86. SetProperty(ref _transpent, value);
  87. if (Transpent == 100|| Transpent == 75||Transpent == 50|| (Transpent == 25))
  88. {
  89. TextEditEvent.Transparency = (int)((_transpent * 255) / 100.0);
  90. TextEditEvent.UpdatePDFEditByEventArgs();
  91. OpacityUI = _transpent / 100.0;
  92. }
  93. else
  94. {
  95. OpacitySelectedIndex = -1;
  96. }
  97. }
  98. }
  99. //是否为图片裁剪状态
  100. private bool _isCrop = false;
  101. public bool IsCrop { get { return _isCrop; } set { SetProperty(ref _isCrop, value); } }
  102. public TextEditToolContentViewModel _viewModel1;
  103. //public ImageEditPropertyViewModel(TextEditToolContentViewModel viewModel1)
  104. //{
  105. // _viewModel1 = viewModel1;
  106. // _viewModel1.PropertyChanged += OnViewModel1PropertyChanged;
  107. //}
  108. private void OnViewModel1PropertyChanged(object sender, PropertyChangedEventArgs e)
  109. {
  110. if (e.PropertyName == "flg")
  111. {
  112. IsCrop = _viewModel1.flg;
  113. }
  114. if(e.PropertyName== "ReplaceImgflg")
  115. {
  116. if(_viewModel1.ReplaceImgflg==true)
  117. {
  118. ReplaceImg();
  119. _viewModel1.ReplaceImgflg = false;
  120. }
  121. }
  122. if (e.PropertyName == "REImgflg")
  123. {
  124. if (_viewModel1.REImgflg == true)
  125. {
  126. GetImagePreView();
  127. _viewModel1.REImgflg = false;
  128. }
  129. }
  130. if (e.PropertyName == "CropImgflg")
  131. {
  132. if (_viewModel1.CropImgflg == true)
  133. {
  134. GetImagePreView();
  135. _viewModel1.CropImgflg = false;
  136. }
  137. }
  138. }
  139. //选中的图像
  140. private System.Windows.Media.Imaging.BitmapSource _currentImg;
  141. public System.Windows.Media.Imaging.BitmapSource CurrentImg { get { return _currentImg; } set { SetProperty(ref _currentImg, value); } }
  142. #endregion
  143. #region Command
  144. //替换
  145. public DelegateCommand ReplaceImgCommand { get; set; }
  146. //导出
  147. public DelegateCommand ExportImgCommand { get; set; }
  148. //裁剪
  149. public DelegateCommand CropImgCommand { get; set; }
  150. //对齐
  151. public DelegateCommand<object> ImgAlignCheckedCommand { get; set; }
  152. //逆时针旋转
  153. public DelegateCommand AntiClockwiseCommand { get; set; }
  154. //顺时针旋转
  155. public DelegateCommand ClockwiseCommand { get; set; }
  156. //左右翻转
  157. public DelegateCommand FlipleftrightCommand { get; set; }
  158. //上下翻转
  159. public DelegateCommand UpsidedownCommand { get; set; }
  160. //裁剪状态
  161. public DelegateCommand CropModeCommand { get; set; }
  162. //取消裁剪状态
  163. public DelegateCommand CancelCropCommand { get; set; }
  164. //还原裁剪状态
  165. public DelegateCommand RestoreCropCommand { get; set; }
  166. //添加文本
  167. public DelegateCommand AddTextCommand { get; set; }
  168. //添加图片
  169. public DelegateCommand AddImgCommand { get; set; }
  170. //透明度条
  171. public DelegateCommand TranspentslidCommand { get; set; }
  172. public DelegateCommand EditImgModeCommand { get; set; }
  173. #endregion
  174. public ImageEditPropertyViewModel(IEventAggregator eventAggregator)
  175. {
  176. InitCommand();
  177. events = eventAggregator;
  178. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  179. }
  180. private void InitCommand()
  181. {
  182. AddTextCommand = new DelegateCommand(AddText);
  183. AddImgCommand = new DelegateCommand(AddImg);
  184. ReplaceImgCommand = new DelegateCommand(ReplaceImg);
  185. ExportImgCommand = new DelegateCommand(ExportImg);
  186. CropImgCommand = new DelegateCommand(CropImg);
  187. ImgAlignCheckedCommand = new DelegateCommand<object>(ImgAlignChecked);
  188. TranspentslidCommand = new DelegateCommand(Transpentslid);
  189. AntiClockwiseCommand = new DelegateCommand(AntiClockwise);
  190. ClockwiseCommand = new DelegateCommand(Clockwise);
  191. FlipleftrightCommand = new DelegateCommand(Flipleftright);
  192. UpsidedownCommand = new DelegateCommand(Upsidedown);
  193. CropModeCommand = new DelegateCommand(CropMode);
  194. CancelCropCommand = new DelegateCommand(CancelCropImg);
  195. RestoreCropCommand = new DelegateCommand(RestoreCropImg);
  196. EditImgModeCommand = new DelegateCommand(EditImgMode);
  197. }
  198. #region Command实现
  199. //不透明度滑动左键松开
  200. private void Transpentslid()
  201. {
  202. if (TextEditEvent != null && isCanSave)
  203. {
  204. if (IsMultiSelectImage)
  205. {
  206. foreach (var item in TextEditEventList)
  207. {
  208. item.Transparency = (int)(_transpent * 2.55);
  209. item.UpdatePDFEditByEventArgs();
  210. TextEditEvent.SaveClip();
  211. }
  212. }
  213. else
  214. {
  215. TextEditEvent.Transparency = (int)((_transpent * 255)/100.0);
  216. TextEditEvent.UpdatePDFEditByEventArgs();
  217. OpacityUI = _transpent / 100.0;
  218. TextEditEvent.SaveClip();
  219. }
  220. }
  221. }
  222. //点击编辑按钮,暂时保留
  223. private void EditImgMode()
  224. {
  225. }
  226. //添加文本模式
  227. private void AddText()
  228. {
  229. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  230. }
  231. //添加图片
  232. private void AddImg()
  233. {
  234. OpenFileDialog openFileDialog = new OpenFileDialog();
  235. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  236. openFileDialog.Multiselect = true;
  237. if ((bool)openFileDialog.ShowDialog())
  238. {
  239. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  240. {
  241. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  242. PDFViewer.AddPDFEditImage(CompressImage(openFileDialog.FileName, 800), 500, 500);
  243. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  244. }
  245. }
  246. }
  247. /// <summary>
  248. /// 压缩
  249. /// </summary>
  250. /// <param name="filePath"></param>
  251. /// <param name="maxwidth"></param>
  252. /// <returns></returns>
  253. public static string CompressImage(string filePath, int maxwidth = 600)
  254. {
  255. try
  256. {
  257. string sourcepath = filePath;
  258. var guid = Guid.NewGuid().ToString();
  259. string folder = Path.Combine(App.CurrentPath, "Temp");
  260. if (!Directory.Exists(folder))
  261. {
  262. Directory.CreateDirectory(folder);
  263. }
  264. var path = System.IO.Path.Combine(App.CurrentPath, "Temp", guid);
  265. Bitmap bitmap = new Bitmap(sourcepath);
  266. var b = bitmap;
  267. //bitmap.Dispose();
  268. double scale = Math.Min((double)maxwidth / b.Width, (double)maxwidth / b.Height);
  269. scale = Math.Min(scale, 1);
  270. System.Drawing.Size newsize = new System.Drawing.Size(maxwidth, maxwidth);
  271. newsize.Width = (int)(scale * b.Width);
  272. newsize.Height = (int)(scale * b.Height);
  273. if (!File.Exists(path))
  274. {
  275. if (CheckTextFile(sourcepath) == FileExtension.PNG)
  276. {
  277. using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
  278. {
  279. bp.Save(path, ImageFormat.Png);
  280. }
  281. }
  282. else
  283. {
  284. using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
  285. {
  286. bp.Save(path, ImageFormat.Jpeg);
  287. }
  288. }
  289. }
  290. return path;
  291. }
  292. catch
  293. {
  294. return filePath;
  295. }
  296. }
  297. /// <summary>
  298. /// 根据图片数据判断图片类型
  299. /// </summary>
  300. /// <param name="fileName"></param>
  301. /// <returns></returns>
  302. public static FileExtension CheckTextFile(string fileName)
  303. {
  304. FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  305. System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
  306. string fileType = string.Empty; ;
  307. try
  308. {
  309. byte data = br.ReadByte();
  310. fileType += data.ToString();
  311. data = br.ReadByte();
  312. fileType += data.ToString();
  313. FileExtension extension;
  314. try
  315. {
  316. extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
  317. }
  318. catch
  319. {
  320. extension = FileExtension.VALIDFILE;
  321. }
  322. return extension;
  323. }
  324. catch (Exception ex)
  325. {
  326. throw ex;
  327. }
  328. finally
  329. {
  330. if (fs != null)
  331. {
  332. fs.Close();
  333. br.Close();
  334. }
  335. }
  336. }
  337. //进入裁剪模式
  338. private void CropMode()
  339. {
  340. IsCrop = true;
  341. if (TextEditEvent != null)
  342. {
  343. TextEditEvent.ClipImage = true;
  344. TextEditEvent.UpdatePDFEditByEventArgs();
  345. }
  346. }
  347. //取消裁剪
  348. private void CancelCropImg()
  349. {
  350. if (TextEditEvent != null)
  351. {
  352. TextEditEvent.ClipImage = false;
  353. TextEditEvent.CancelClip();
  354. TextEditEvent.UpdatePDFEditByEventArgs();
  355. IsCrop = false;
  356. }
  357. }
  358. //还原裁剪
  359. private void RestoreCropImg()
  360. {
  361. if (TextEditEvent != null)
  362. {
  363. TextEditEvent.RestoreClip();
  364. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  365. }
  366. }
  367. //完成裁剪
  368. private void CropImg()
  369. {
  370. if (TextEditEvent != null)
  371. {
  372. TextEditEvent.SaveClip();
  373. TextEditEvent.ClipImage = false;
  374. TextEditEvent.UpdatePDFEditByEventArgs();
  375. IsCrop = false;
  376. GetImagePreView();
  377. }
  378. }
  379. //导出图片
  380. private void ExportImg()
  381. {
  382. if (PDFViewer == null || TextEditEvent == null || TextEditEvent.EditType != ComPDFKit.PDFPage.CPDFEditType.EditImage) return;
  383. System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
  384. folder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  385. if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  386. {
  387. if (string.IsNullOrEmpty(folder.SelectedPath))
  388. return;
  389. var keyValueList = PDFViewer.GetSelectedImages();
  390. int i = 0;
  391. foreach (var bitmap in keyValueList)
  392. {
  393. foreach (var bitmapItem in bitmap.Value)
  394. {
  395. bitmapItem.Save(folder.SelectedPath + "\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
  396. i++;
  397. }
  398. }
  399. var strFilePath = folder.SelectedPath + "\\0.png";
  400. CommonHelper.ShowFileBrowser(strFilePath);
  401. }
  402. }
  403. //替换图片
  404. private void ReplaceImg()
  405. {
  406. OpenFileDialog openFileDialog = new OpenFileDialog();
  407. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  408. openFileDialog.Multiselect = true;
  409. if ((bool)openFileDialog.ShowDialog())
  410. {
  411. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  412. {
  413. TextEditEvent.ReplaceImagePath = openFileDialog.FileName;
  414. TextEditEvent.UpdatePDFEditByEventArgs();
  415. ReplaceimgTask();
  416. Transpent =100;
  417. }
  418. }
  419. }
  420. //此处只有异步 ReplaceimgTask()才有作用
  421. private async void ReplaceimgTask()
  422. {
  423. await Task.Delay(100);
  424. GetImagePreView();
  425. }
  426. //顺时针旋转
  427. private void Clockwise()
  428. {
  429. ImgRoateAngle(90);
  430. }
  431. //逆时针旋转
  432. private void AntiClockwise()
  433. {
  434. ImgRoateAngle(-90);
  435. }
  436. //左右翻转
  437. private void Flipleftright()
  438. {
  439. TextEditEvent.HorizontalMirror = true;
  440. TextEditEvent.UpdatePDFEditByEventArgs();
  441. GetImagePreView();
  442. }
  443. //上下翻转
  444. private void Upsidedown()
  445. {
  446. TextEditEvent.VerticalMirror = true;
  447. TextEditEvent.UpdatePDFEditByEventArgs();
  448. GetImagePreView();
  449. }
  450. //旋转逻辑
  451. private void ImgRoateAngle(int angle)
  452. {
  453. if (IsMultiSelectImage)
  454. {
  455. foreach (var item in TextEditEventList)
  456. {
  457. item.Rotate = item.Rotate + angle;
  458. item.UpdatePDFEditByEventArgs();
  459. }
  460. }
  461. else
  462. {
  463. TextEditEvent.Rotate = TextEditEvent.Rotate + angle;
  464. TextEditEvent.UpdatePDFEditByEventArgs();
  465. GetImagePreView();
  466. }
  467. }
  468. #endregion
  469. #region 布局处理
  470. private void ImgAlignChecked(object obj)
  471. {
  472. if (obj != null)
  473. {
  474. switch ((string)obj)
  475. {
  476. case "AlignLeft":
  477. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  478. break;
  479. case "AlignHorizonCenter":
  480. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  481. break;
  482. case "AlignRight":
  483. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  484. break;
  485. case "DistributeHorizontal":
  486. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  487. break;
  488. case "AlignTop":
  489. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  490. break;
  491. case "AlignVerticalCenter":
  492. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  493. break;
  494. case "AlignBottom":
  495. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  496. break;
  497. case "DistributeVertical":
  498. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  499. break;
  500. }
  501. }
  502. }
  503. #endregion
  504. protected List<PDFEditEvent> TextEditEventList;
  505. public void OnNavigatedTo(NavigationContext navigationContext)
  506. {
  507. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  508. navigationContext.Parameters.TryGetValue<List<PDFEditEvent>>(ParameterNames.AnnotEvent, out TextEditEventList);
  509. navigationContext.Parameters.TryGetValue<TextEditToolContentViewModel>(ParameterNames.TextEditToolContentViewModel, out TextEditToolContentViewModel);
  510. if (PDFViewer != null)
  511. {
  512. _viewModel1 = TextEditToolContentViewModel;
  513. _viewModel1.PropertyChanged += OnViewModel1PropertyChanged;
  514. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  515. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  516. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  517. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  518. PDFViewer.LostFocus -= PDFViewer_LostFocus;
  519. PDFViewer.LostFocus += PDFViewer_LostFocus;
  520. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  521. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  522. LoadedPDFEdit();
  523. isCanSave = true;
  524. }
  525. }
  526. private void PDFViewer_LostFocus(object sender, RoutedEventArgs e)
  527. {
  528. TextEditEvent.ClipImage = false;
  529. TextEditEvent.CancelClip();
  530. TextEditEvent.UpdatePDFEditByEventArgs();
  531. IsCrop = false;
  532. }
  533. //获取图片参数
  534. private void LoadedPDFEdit()
  535. {
  536. if (TextEditEventList != null && TextEditEventList.Count > 0)
  537. {
  538. TextEditEvent = TextEditEventList[0];
  539. if (TextEditEventList.Count > 1)
  540. {
  541. IsMultiSelectImage = true;
  542. }
  543. else
  544. {
  545. GetImagePreView();
  546. }
  547. if (TextEditEventList.Count == 2)
  548. {
  549. IsLayoutAlign = true;
  550. IsLayoutAvgAlign = false;
  551. }
  552. else if (TextEditEventList.Count > 2)
  553. {
  554. IsLayoutAlign = true;
  555. IsLayoutAvgAlign = true;
  556. }
  557. else
  558. {
  559. IsLayoutAlign = false;
  560. IsLayoutAvgAlign = false;
  561. }
  562. var tranUI = (TextEditEvent.Transparency * 100 / 255);
  563. var temp = Math.Round((double)tranUI, 0);
  564. Transpent = temp;
  565. OpacityUI = temp / 100.0;
  566. }
  567. }
  568. #region 右键菜单
  569. //点击空白处时
  570. private ContextMenu EmptyStateMenu(object sender)
  571. {
  572. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  573. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  574. //粘贴
  575. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  576. //添加文本
  577. customMenu.SetMenuBinding(1, AddTextCommand);
  578. //添加图像
  579. customMenu.SetMenuBinding(2, AddImgCommand);
  580. return popMenu;
  581. }
  582. //选中图像时
  583. private ContextMenu SelectImgPDFEdit(object sender)
  584. {
  585. var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu;
  586. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  587. //复制
  588. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  589. //剪切
  590. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  591. //粘贴
  592. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  593. //删除
  594. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  595. //裁剪
  596. customMenu.SetMenuBinding(4, CropModeCommand);
  597. //替换
  598. customMenu.SetMenuBinding(5, ReplaceImgCommand);
  599. //导出
  600. customMenu.SetMenuBinding(6, ExportImgCommand);
  601. return popMenu;
  602. }
  603. //选中裁剪图像时
  604. private ContextMenu CropImgPDFEdit(object sender)
  605. {
  606. var popMenu = App.Current.FindResource("CropImgMenu") as ContextMenu;
  607. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  608. //确认裁剪
  609. customMenu.SetMenuBinding(0, CropImgCommand);
  610. //取消裁剪
  611. customMenu.SetMenuBinding(1, CancelCropCommand);
  612. //还原裁剪
  613. customMenu.SetMenuBinding(2, RestoreCropCommand);
  614. return popMenu;
  615. }
  616. //多选图片右键
  617. private ContextMenu SelectMoreImage(object sender)
  618. {
  619. var popMenu = App.Current.FindResource("SelectMoreImageMenu") as ContextMenu;
  620. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  621. //复制
  622. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  623. //剪切
  624. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  625. //粘贴
  626. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  627. //删除
  628. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  629. //导出
  630. customMenu.SetMenuBinding(4, ExportImgCommand);
  631. return popMenu;
  632. }
  633. #endregion
  634. //左键点击逻辑
  635. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  636. {
  637. //退出编辑模式
  638. IsCrop = false;
  639. TextEditEvent.ClipImage = false;
  640. TextEditEvent.UpdatePDFEditByEventArgs();
  641. }
  642. //右键逻辑
  643. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  644. {
  645. if (e == null)
  646. return;
  647. switch (e.CommandType)
  648. {
  649. case CommandType.Context:
  650. // if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  651. //{
  652. // if (IsCrop == true)
  653. // {
  654. // e.PopupMenu =CropImgPDFEdit(sender);
  655. // }
  656. // else
  657. // {
  658. // e.PopupMenu = SelectImgPDFEdit(sender);
  659. // }
  660. //}
  661. //else
  662. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  663. {
  664. e.PopupMenu = EmptyStateMenu(sender);
  665. }
  666. break;
  667. default:
  668. e.DoCommand();
  669. break;
  670. }
  671. if (e.PopupMenu != null)
  672. {
  673. e.Handle = true;
  674. }
  675. }
  676. //属性面板图像更新
  677. private void GetImagePreView()
  678. {
  679. try
  680. {
  681. var list = PDFViewer.GetSelectedImages();
  682. if (list != null && list.Count > 0)
  683. {
  684. System.Drawing.Bitmap bitmap = null;
  685. foreach (var item in list)
  686. {
  687. if (item.Value.Count > 0)
  688. {
  689. bitmap = item.Value[0];
  690. break;
  691. }
  692. }
  693. if (bitmap != null)
  694. {
  695. IntPtr ip = bitmap.GetHbitmap();
  696. System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
  697. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  698. CurrentImg = bitmapSource;
  699. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  700. }
  701. }
  702. }
  703. catch
  704. {
  705. }
  706. }
  707. public bool IsNavigationTarget(NavigationContext navigationContext) { return true; }
  708. public void OnNavigatedFrom(NavigationContext navigationContext)
  709. {
  710. isCanSave = false;
  711. IsMultiSelectImage = false;
  712. TextEditEvent = null;
  713. ClearCheckedAglin?.Invoke(null, null);
  714. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  715. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  716. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  717. PDFViewer.LostFocus -= PDFViewer_LostFocus;
  718. }
  719. }
  720. }