ImageEditPropertyViewModel.cs 28 KB

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