ImageEditPropertyViewModel.cs 16 KB

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