ImageEditPropertyViewModel.cs 19 KB

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