ImageEditPropertyViewModel.cs 18 KB

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