ImageEditPropertyViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. }
  241. }
  242. }
  243. private void CropMode()
  244. {
  245. IsCrop = true;
  246. if (TextEditEvent != null)
  247. {
  248. TextEditEvent.ClipImage = true;
  249. TextEditEvent.UpdatePDFEditByEventArgs();
  250. }
  251. }
  252. private void Clockwise()
  253. {
  254. ImgRoateAngle(90);
  255. }
  256. private void AntiClockwise()
  257. {
  258. ImgRoateAngle(-90);
  259. }
  260. private void Flipleftright()
  261. {
  262. TextEditEvent.HorizontalMirror = true;
  263. TextEditEvent.UpdatePDFEditByEventArgs();
  264. }
  265. private void Upsidedown()
  266. {
  267. TextEditEvent.VerticalMirror = true;
  268. TextEditEvent.UpdatePDFEditByEventArgs();
  269. }
  270. private void ImgRoateAngle(int angle)
  271. {
  272. if (IsMultiSelectImage)
  273. {
  274. foreach (var item in TextEditEventList)
  275. {
  276. item.Rotate = item.Rotate + angle;
  277. item.UpdatePDFEditByEventArgs();
  278. }
  279. }
  280. else
  281. {
  282. TextEditEvent.Rotate = TextEditEvent.Rotate + angle;
  283. TextEditEvent.UpdatePDFEditByEventArgs();
  284. GetImagePreView();
  285. }
  286. }
  287. #endregion
  288. #region 布局处理
  289. private void ImgAlignChecked(object obj)
  290. {
  291. if (obj != null)
  292. {
  293. switch ((string)obj)
  294. {
  295. case "AlignLeft":
  296. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  297. break;
  298. case "AlignHorizonCenter":
  299. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  300. break;
  301. case "AlignRight":
  302. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  303. break;
  304. case "DistributeHorizontal":
  305. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  306. break;
  307. case "AlignTop":
  308. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  309. break;
  310. case "AlignVerticalCenter":
  311. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  312. break;
  313. case "AlignBottom":
  314. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  315. break;
  316. case "DistributeVertical":
  317. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  318. break;
  319. }
  320. }
  321. }
  322. private void ReLoadLayoutAlign(int count)
  323. {
  324. if (count >= 2)
  325. IsLayoutAlign = true;
  326. else
  327. IsLayoutAlign = false;
  328. if (count >= 3)
  329. IsLayoutAvgAlign = true;
  330. else
  331. IsLayoutAvgAlign = false;
  332. }
  333. #endregion
  334. protected List<PDFEditEvent> TextEditEventList;
  335. public void OnNavigatedTo(NavigationContext navigationContext)
  336. {
  337. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  338. navigationContext.Parameters.TryGetValue<List<PDFEditEvent>>(ParameterNames.AnnotEvent, out TextEditEventList);
  339. if (PDFViewer != null)
  340. {
  341. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  342. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  343. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  344. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  345. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  346. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  347. LoadedPDFEdit();
  348. isCanSave = true;
  349. }
  350. }
  351. private void LoadedPDFEdit()
  352. {
  353. if (TextEditEventList != null && TextEditEventList.Count > 0)
  354. {
  355. TextEditEvent = TextEditEventList[0];
  356. if (TextEditEventList.Count > 1)
  357. {
  358. IsMultiSelectImage = true;
  359. }
  360. else
  361. {
  362. GetImagePreView();
  363. }
  364. if (TextEditEventList.Count == 2)
  365. {
  366. IsLayoutAlign = true;
  367. IsLayoutAvgAlign = false;
  368. }
  369. else if (TextEditEventList.Count > 2)
  370. {
  371. IsLayoutAlign = true;
  372. IsLayoutAvgAlign = true;
  373. }
  374. else
  375. {
  376. IsLayoutAlign = false;
  377. IsLayoutAvgAlign = false;
  378. }
  379. GetPDFEdit();
  380. }
  381. }
  382. private void GetPDFEdit()
  383. {
  384. var tranUI = (TextEditEvent.Transparency / (255.0*255.0))*100;
  385. var temp = Math.Round((double)tranUI, 0);
  386. Transpent = temp;
  387. OpacityUI = temp/100.0;
  388. }
  389. //点击空白处时
  390. private ContextMenu EmptyStateMenu(object sender)
  391. {
  392. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  393. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  394. //粘贴
  395. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  396. //添加文本
  397. customMenu.SetMenuBinding(1, AddTextCommand);
  398. //添加图像
  399. customMenu.SetMenuBinding(2, AddImgCommand);
  400. return popMenu;
  401. }
  402. //选中图像时
  403. private ContextMenu SelectImgPDFEdit(object sender)
  404. {
  405. var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu;
  406. CustomPopMenu customMenu = new CustomPopMenu(popMenu,sender);
  407. //复制
  408. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  409. //剪切
  410. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  411. //粘贴
  412. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  413. //删除
  414. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  415. //裁剪
  416. customMenu.SetMenuBinding(4, CropModeCommand);
  417. //替换
  418. customMenu.SetMenuBinding(5, ReplaceImgCommand);
  419. //导出
  420. customMenu.SetMenuBinding(6, ExportImgCommand);
  421. return popMenu;
  422. }
  423. //选中裁剪图像时
  424. private ContextMenu CropImgPDFEdit(object sender)
  425. {
  426. var popMenu = App.Current.FindResource("CropImgMenu") as ContextMenu;
  427. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  428. //确认裁剪
  429. customMenu.SetMenuBinding(0, CropImgCommand);
  430. //取消裁剪
  431. customMenu.SetMenuBinding(1, CancelCropCommand);
  432. //还原裁剪
  433. customMenu.SetMenuBinding(2, RestoreCropCommand);
  434. return popMenu;
  435. }
  436. //多选图片右键
  437. private ContextMenu SelectMoreImage(object sender)
  438. {
  439. var popMenu = App.Current.FindResource("SelectMoreImageMenu") as ContextMenu;
  440. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  441. //复制
  442. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  443. //剪切
  444. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  445. //粘贴
  446. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  447. //删除
  448. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  449. //导出
  450. customMenu.SetMenuBinding(4, ExportImgCommand);
  451. return popMenu;
  452. }
  453. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  454. {
  455. IsCrop = false;
  456. }
  457. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  458. {
  459. if (e == null)
  460. return;
  461. switch (e.CommandType)
  462. {
  463. case CommandType.Context:
  464. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  465. {
  466. if (IsCrop == true)
  467. {
  468. e.PopupMenu =CropImgPDFEdit(sender);
  469. }
  470. else
  471. {
  472. e.PopupMenu = SelectImgPDFEdit(sender);
  473. }
  474. }
  475. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  476. {
  477. e.PopupMenu = EmptyStateMenu(sender);
  478. }
  479. break;
  480. default:
  481. e.DoCommand();
  482. break;
  483. }
  484. if (e.PopupMenu != null)
  485. {
  486. e.Handle = true;
  487. }
  488. }
  489. private void GetImagePreView()
  490. {
  491. try
  492. {
  493. var list = PDFViewer.GetSelectedImages();
  494. if (list != null && list.Count > 0)
  495. {
  496. System.Drawing.Bitmap bitmap = null;
  497. foreach (var item in list)
  498. {
  499. if (item.Value.Count > 0)
  500. {
  501. bitmap = item.Value[0];
  502. break;
  503. }
  504. }
  505. if (bitmap != null)
  506. {
  507. IntPtr ip = bitmap.GetHbitmap();
  508. System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
  509. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  510. CurrentImg = bitmapSource;
  511. }
  512. }
  513. }
  514. catch
  515. {
  516. }
  517. }
  518. #region 全局
  519. //private void ShowPropertyPanel(bool show = true)
  520. //{
  521. // viewContentViewModel.IsPropertyOpen = show;
  522. //}
  523. #endregion
  524. public bool IsNavigationTarget(NavigationContext navigationContext) { return true; }
  525. public void OnNavigatedFrom(NavigationContext navigationContext)
  526. {
  527. isCanSave = false;
  528. IsMultiSelectImage = false;
  529. TextEditEvent = null;
  530. ClearCheckedAglin?.Invoke(null, null);
  531. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  532. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  533. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  534. }
  535. }
  536. }