ImageEditPropertyViewModel.cs 20 KB

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