ImageEditPropertyViewModel.cs 27 KB

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