ImageEditPropertyViewModel.cs 16 KB

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