ImageEditPropertyViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Win32;
  4. using PDF_Master.Helper;
  5. using PDF_Master.Model;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Input;
  17. namespace PDF_Master.ViewModels.PropertyPanel.PDFEdit
  18. {
  19. public class ImageEditPropertyViewModel : PDFEditVM, INavigationAware
  20. {
  21. #region 变量
  22. //防止自动保存属性值
  23. private bool isCanSave = false;
  24. public event EventHandler ClearCheckedAglin;
  25. #endregion
  26. #region 快捷键
  27. private void ShortCut_KeyDown(object sender, KeyEventArgs e)
  28. {
  29. if (e.Key == Key.Escape)
  30. {
  31. if (PDFViewer != null)
  32. {
  33. if (PDFViewer.ToolManager != null && IsCrop == true)
  34. {
  35. CancelCropImg();
  36. }
  37. }
  38. }
  39. if(e.Key==Key.Enter)
  40. {
  41. CropImg();
  42. }
  43. }
  44. #endregion
  45. #region 属性
  46. #region 是否为多选内容
  47. private bool _isMultiSelectImage = false;
  48. public bool IsMultiSelectImage { get { return _isMultiSelectImage; } set { SetProperty(ref _isMultiSelectImage, value); } }
  49. #endregion
  50. private string _opacity1= "Opacity";
  51. public string Opacity
  52. {
  53. get { return _opacity1; }
  54. set { SetProperty(ref _opacity1, value); }
  55. }
  56. #region 不透明度
  57. private double _opacity;
  58. public double OpacityUI
  59. {
  60. get { return _opacity; }
  61. set { SetProperty(ref _opacity, value); }
  62. }
  63. private double _transpent;
  64. public double Transpent
  65. {
  66. get { return _transpent; }
  67. set
  68. {
  69. SetProperty(ref _transpent, value);
  70. }
  71. }
  72. #endregion
  73. #region 是否为图片裁剪状态
  74. private bool _isCrop = false;
  75. public bool IsCrop { get { return _isCrop; } set { SetProperty(ref _isCrop, value); } }
  76. #endregion
  77. #region 当前显示图像
  78. //选中的图像
  79. private System.Windows.Media.Imaging.BitmapSource _currentImg;
  80. public System.Windows.Media.Imaging.BitmapSource CurrentImg { get { return _currentImg; } set { SetProperty(ref _currentImg, value); } }
  81. #endregion
  82. #endregion
  83. #region Command
  84. //替换
  85. public DelegateCommand ReplaceImgCommand { get; set; }
  86. //导出
  87. public DelegateCommand ExportImgCommand { get; set; }
  88. //裁剪
  89. public DelegateCommand CropImgCommand { get; set; }
  90. //对齐
  91. public DelegateCommand<object> ImgAlignCheckedCommand { get; set; }
  92. //逆时针旋转
  93. public DelegateCommand AntiClockwiseCommand { get; set; }
  94. //顺时针旋转
  95. public DelegateCommand ClockwiseCommand { get; set; }
  96. //左右翻转
  97. public DelegateCommand FlipleftrightCommand { get; set; }
  98. //上下翻转
  99. public DelegateCommand UpsidedownCommand { get; set; }
  100. //裁剪状态
  101. public DelegateCommand CropModeCommand { get; set; }
  102. //取消裁剪状态
  103. public DelegateCommand CancelCropCommand { get; set; }
  104. //还原裁剪状态
  105. public DelegateCommand RestoreCropCommand { get; set; }
  106. //添加文本
  107. public DelegateCommand AddTextCommand { get; set; }
  108. //添加图片
  109. public DelegateCommand AddImgCommand { get; set; }
  110. //透明度条
  111. public DelegateCommand TranspentslidCommand { get; set; }
  112. public DelegateCommand EditImgModeCommand { get; set; }
  113. #endregion
  114. public ImageEditPropertyViewModel()
  115. {
  116. InitCommand();
  117. }
  118. private void InitCommand()
  119. {
  120. AddTextCommand = new DelegateCommand(AddText);
  121. AddImgCommand = new DelegateCommand(AddImg);
  122. ReplaceImgCommand = new DelegateCommand(ReplaceImg);
  123. ExportImgCommand = new DelegateCommand(ExportImg);
  124. CropImgCommand = new DelegateCommand(CropImg);
  125. ImgAlignCheckedCommand = new DelegateCommand<object>(ImgAlignChecked);
  126. TranspentslidCommand = new DelegateCommand(Transpentslid);
  127. AntiClockwiseCommand = new DelegateCommand(AntiClockwise);
  128. ClockwiseCommand = new DelegateCommand(Clockwise);
  129. FlipleftrightCommand = new DelegateCommand(Flipleftright);
  130. UpsidedownCommand = new DelegateCommand(Upsidedown);
  131. CropModeCommand = new DelegateCommand(CropMode);
  132. CancelCropCommand = new DelegateCommand(CancelCropImg);
  133. RestoreCropCommand = new DelegateCommand(RestoreCropImg);
  134. EditImgModeCommand = new DelegateCommand(EditImgMode);
  135. }
  136. #region Command实现
  137. private void Transpentslid()
  138. {
  139. if (TextEditEvent != null && isCanSave)
  140. {
  141. if (IsMultiSelectImage)
  142. {
  143. foreach (var item in TextEditEventList)
  144. {
  145. item.Transparency = (int)(_transpent * 2.55);
  146. item.UpdatePDFEditByEventArgs();
  147. }
  148. }
  149. else
  150. {
  151. TextEditEvent.Transparency = (int)(_transpent * 2.55);
  152. TextEditEvent.UpdatePDFEditByEventArgs();
  153. OpacityUI = _transpent / 100.0;
  154. }
  155. }
  156. }
  157. private void EditImgMode()
  158. {
  159. }
  160. private void AddText()
  161. {
  162. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  163. }
  164. private void CancelCropImg()
  165. {
  166. if (TextEditEvent != null)
  167. {
  168. TextEditEvent.ClipImage = false;
  169. TextEditEvent.CancelClip();
  170. TextEditEvent.UpdatePDFEditByEventArgs();
  171. IsCrop = false;
  172. }
  173. }
  174. private void RestoreCropImg()
  175. {
  176. if (TextEditEvent != null)
  177. {
  178. TextEditEvent.RestoreClip();
  179. }
  180. }
  181. private void CropImg()
  182. {
  183. if (TextEditEvent != null)
  184. {
  185. TextEditEvent.SaveClip();
  186. TextEditEvent.ClipImage = false;
  187. TextEditEvent.UpdatePDFEditByEventArgs();
  188. IsCrop = false;
  189. GetImagePreView();
  190. }
  191. }
  192. private void ExportImg()
  193. {
  194. if (PDFViewer == null || TextEditEvent == null || TextEditEvent.EditType != ComPDFKit.PDFPage.CPDFEditType.EditImage) return;
  195. System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
  196. folder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  197. if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  198. {
  199. if (string.IsNullOrEmpty(folder.SelectedPath))
  200. return;
  201. var keyValueList = PDFViewer.GetSelectedImages();
  202. int i = 0;
  203. foreach (var bitmap in keyValueList)
  204. {
  205. foreach (var bitmapItem in bitmap.Value)
  206. {
  207. bitmapItem.Save(folder.SelectedPath + "\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
  208. i++;
  209. }
  210. }
  211. var strFilePath = folder.SelectedPath + "\\0.png";
  212. CommonHelper.ShowFileBrowser(strFilePath);
  213. }
  214. }
  215. private void AddImg()
  216. {
  217. OpenFileDialog openFileDialog = new OpenFileDialog();
  218. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  219. openFileDialog.Multiselect = true;
  220. if ((bool)openFileDialog.ShowDialog())
  221. {
  222. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  223. {
  224. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  225. PDFViewer.AddPDFEditImage(openFileDialog.FileName,500,500);
  226. }
  227. }
  228. }
  229. private void ReplaceImg()
  230. {
  231. OpenFileDialog openFileDialog = new OpenFileDialog();
  232. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  233. openFileDialog.Multiselect = true;
  234. if ((bool)openFileDialog.ShowDialog())
  235. {
  236. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  237. {
  238. TextEditEvent.ReplaceImagePath = openFileDialog.FileName;
  239. TextEditEvent.UpdatePDFEditByEventArgs();
  240. ReplaceimgTask();
  241. }
  242. }
  243. }
  244. //此处只有多线程 ReplaceimgTask()才有作用
  245. async Task ReplaceimgTask()
  246. {
  247. await Task.Delay(100);
  248. GetImagePreView();
  249. }
  250. private void CropMode()
  251. {
  252. IsCrop = true;
  253. if (TextEditEvent != null)
  254. {
  255. TextEditEvent.ClipImage = true;
  256. TextEditEvent.UpdatePDFEditByEventArgs();
  257. }
  258. }
  259. private void Clockwise()
  260. {
  261. ImgRoateAngle(90);
  262. }
  263. private void AntiClockwise()
  264. {
  265. ImgRoateAngle(-90);
  266. }
  267. private void Flipleftright()
  268. {
  269. TextEditEvent.HorizontalMirror = true;
  270. TextEditEvent.UpdatePDFEditByEventArgs();
  271. }
  272. private void Upsidedown()
  273. {
  274. TextEditEvent.VerticalMirror = true;
  275. TextEditEvent.UpdatePDFEditByEventArgs();
  276. }
  277. private void ImgRoateAngle(int angle)
  278. {
  279. if (IsMultiSelectImage)
  280. {
  281. foreach (var item in TextEditEventList)
  282. {
  283. item.Rotate = item.Rotate + angle;
  284. item.UpdatePDFEditByEventArgs();
  285. }
  286. }
  287. else
  288. {
  289. TextEditEvent.Rotate = TextEditEvent.Rotate + angle;
  290. TextEditEvent.UpdatePDFEditByEventArgs();
  291. GetImagePreView();
  292. }
  293. }
  294. #endregion
  295. #region 布局处理
  296. private void ImgAlignChecked(object obj)
  297. {
  298. if (obj != null)
  299. {
  300. switch ((string)obj)
  301. {
  302. case "AlignLeft":
  303. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  304. break;
  305. case "AlignHorizonCenter":
  306. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  307. break;
  308. case "AlignRight":
  309. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  310. break;
  311. case "DistributeHorizontal":
  312. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  313. break;
  314. case "AlignTop":
  315. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  316. break;
  317. case "AlignVerticalCenter":
  318. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  319. break;
  320. case "AlignBottom":
  321. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  322. break;
  323. case "DistributeVertical":
  324. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  325. break;
  326. }
  327. }
  328. }
  329. private void ReLoadLayoutAlign(int count)
  330. {
  331. if (count >= 2)
  332. IsLayoutAlign = true;
  333. else
  334. IsLayoutAlign = false;
  335. if (count >= 3)
  336. IsLayoutAvgAlign = true;
  337. else
  338. IsLayoutAvgAlign = false;
  339. }
  340. #endregion
  341. protected List<PDFEditEvent> TextEditEventList;
  342. public void OnNavigatedTo(NavigationContext navigationContext)
  343. {
  344. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  345. navigationContext.Parameters.TryGetValue<List<PDFEditEvent>>(ParameterNames.AnnotEvent, out TextEditEventList);
  346. if (PDFViewer != null)
  347. {
  348. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  349. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  350. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  351. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  352. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  353. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  354. LoadedPDFEdit();
  355. isCanSave = true;
  356. }
  357. }
  358. private void LoadedPDFEdit()
  359. {
  360. if (TextEditEventList != null && TextEditEventList.Count > 0)
  361. {
  362. TextEditEvent = TextEditEventList[0];
  363. if (TextEditEventList.Count > 1)
  364. {
  365. IsMultiSelectImage = true;
  366. }
  367. else
  368. {
  369. GetImagePreView();
  370. }
  371. if (TextEditEventList.Count == 2)
  372. {
  373. IsLayoutAlign = true;
  374. IsLayoutAvgAlign = false;
  375. }
  376. else if (TextEditEventList.Count > 2)
  377. {
  378. IsLayoutAlign = true;
  379. IsLayoutAvgAlign = true;
  380. }
  381. else
  382. {
  383. IsLayoutAlign = false;
  384. IsLayoutAvgAlign = false;
  385. }
  386. GetPDFEdit();
  387. }
  388. }
  389. private void GetPDFEdit()
  390. {
  391. var tranUI = (TextEditEvent.Transparency / (255.0*255.0))*100;
  392. var temp = Math.Round((double)tranUI, 0);
  393. Transpent = temp;
  394. OpacityUI = temp/100.0;
  395. }
  396. //点击空白处时
  397. private ContextMenu EmptyStateMenu(object sender)
  398. {
  399. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  400. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  401. //粘贴
  402. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  403. //添加文本
  404. customMenu.SetMenuBinding(1, AddTextCommand);
  405. //添加图像
  406. customMenu.SetMenuBinding(2, AddImgCommand);
  407. return popMenu;
  408. }
  409. //选中图像时
  410. private ContextMenu SelectImgPDFEdit(object sender)
  411. {
  412. var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu;
  413. CustomPopMenu customMenu = new CustomPopMenu(popMenu,sender);
  414. //复制
  415. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  416. //剪切
  417. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  418. //粘贴
  419. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  420. //删除
  421. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  422. //裁剪
  423. customMenu.SetMenuBinding(4, CropModeCommand);
  424. //替换
  425. customMenu.SetMenuBinding(5, ReplaceImgCommand);
  426. //导出
  427. customMenu.SetMenuBinding(6, ExportImgCommand);
  428. return popMenu;
  429. }
  430. //选中裁剪图像时
  431. private ContextMenu CropImgPDFEdit(object sender)
  432. {
  433. var popMenu = App.Current.FindResource("CropImgMenu") as ContextMenu;
  434. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  435. //确认裁剪
  436. customMenu.SetMenuBinding(0, CropImgCommand);
  437. //取消裁剪
  438. customMenu.SetMenuBinding(1, CancelCropCommand);
  439. //还原裁剪
  440. customMenu.SetMenuBinding(2, RestoreCropCommand);
  441. return popMenu;
  442. }
  443. //多选图片右键
  444. private ContextMenu SelectMoreImage(object sender)
  445. {
  446. var popMenu = App.Current.FindResource("SelectMoreImageMenu") as ContextMenu;
  447. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  448. //复制
  449. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  450. //剪切
  451. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  452. //粘贴
  453. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  454. //删除
  455. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  456. //导出
  457. customMenu.SetMenuBinding(4, ExportImgCommand);
  458. return popMenu;
  459. }
  460. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  461. {
  462. IsCrop = false;
  463. }
  464. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  465. {
  466. if (e == null)
  467. return;
  468. switch (e.CommandType)
  469. {
  470. case CommandType.Context:
  471. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  472. {
  473. if (IsCrop == true)
  474. {
  475. e.PopupMenu =CropImgPDFEdit(sender);
  476. }
  477. else
  478. {
  479. e.PopupMenu = SelectImgPDFEdit(sender);
  480. }
  481. }
  482. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  483. {
  484. e.PopupMenu = EmptyStateMenu(sender);
  485. }
  486. break;
  487. default:
  488. e.DoCommand();
  489. break;
  490. }
  491. if (e.PopupMenu != null)
  492. {
  493. e.Handle = true;
  494. }
  495. }
  496. private void GetImagePreView()
  497. {
  498. try
  499. {
  500. var list = PDFViewer.GetSelectedImages();
  501. if (list != null && list.Count > 0)
  502. {
  503. System.Drawing.Bitmap bitmap = null;
  504. foreach (var item in list)
  505. {
  506. if (item.Value.Count > 0)
  507. {
  508. bitmap = item.Value[0];
  509. break;
  510. }
  511. }
  512. if (bitmap != null)
  513. {
  514. IntPtr ip = bitmap.GetHbitmap();
  515. System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
  516. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  517. CurrentImg = bitmapSource;
  518. }
  519. }
  520. }
  521. catch
  522. {
  523. }
  524. }
  525. #region 全局
  526. //private void ShowPropertyPanel(bool show = true)
  527. //{
  528. // viewContentViewModel.IsPropertyOpen = show;
  529. //}
  530. #endregion
  531. public bool IsNavigationTarget(NavigationContext navigationContext) { return true; }
  532. public void OnNavigatedFrom(NavigationContext navigationContext)
  533. {
  534. isCanSave = false;
  535. IsMultiSelectImage = false;
  536. TextEditEvent = null;
  537. ClearCheckedAglin?.Invoke(null, null);
  538. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  539. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  540. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  541. }
  542. }
  543. }