ImageEditPropertyViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Win32;
  4. using PDF_Office.Model;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Input;
  16. namespace PDF_Office.ViewModels.PropertyPanel.PDFEdit
  17. {
  18. public class ImageEditPropertyViewModel : PDFEditVM, INavigationAware
  19. {
  20. #region 图像属性
  21. private bool _isCrop = false;
  22. public bool IsCrop{get { return _isCrop; } set{SetProperty(ref _isCrop, value); }}
  23. //选中的图像
  24. private System.Windows.Media.Imaging.BitmapSource _currentImg;
  25. public System.Windows.Media.Imaging.BitmapSource CurrentImg {get { return _currentImg; } set{ SetProperty(ref _currentImg, value); }}
  26. private bool _isMultiSelectImage = false;
  27. public bool IsMultiSelectImage { get { return _isMultiSelectImage; } set { SetProperty(ref _isMultiSelectImage, value); }}
  28. #endregion
  29. #region 图像Command
  30. public DelegateCommand ReplaceImgCommand { get; set; }
  31. public DelegateCommand ExportImgCommand { get; set; }
  32. public DelegateCommand CropImgCommand { get; set; }
  33. public DelegateCommand<object> ImgAlignCheckedCommand { get; set; }
  34. /// <summary>
  35. /// 逆时针旋转
  36. /// </summary>
  37. public DelegateCommand AntiClockwiseCommand { get; set; }
  38. /// <summary>
  39. /// 顺时针旋转
  40. /// </summary>
  41. public DelegateCommand ClockwiseCommand { get; set; }
  42. public DelegateCommand CropModeCommand { get; set; }
  43. public DelegateCommand CancelCropCommand { get; set; }
  44. public DelegateCommand AddTextCommand { get; set; }
  45. public DelegateCommand AddImgCommand { get; set; }
  46. #endregion
  47. public ImageEditPropertyViewModel()
  48. {
  49. InitCommand();
  50. }
  51. private void InitCommand()
  52. {
  53. AddTextCommand = new DelegateCommand(AddText);
  54. AddImgCommand = new DelegateCommand(AddImg);
  55. ReplaceImgCommand = new DelegateCommand(ReplaceImg);
  56. ExportImgCommand = new DelegateCommand(ExportImg);
  57. CropImgCommand = new DelegateCommand(CropImg);
  58. ImgAlignCheckedCommand = new DelegateCommand<object>(ImgAlignChecked);
  59. AntiClockwiseCommand = new DelegateCommand(AntiClockwise);
  60. ClockwiseCommand = new DelegateCommand(Clockwise);
  61. CropModeCommand = new DelegateCommand(CropMode);
  62. CancelCropCommand = new DelegateCommand(CancelCropImg);
  63. }
  64. private void AddText()
  65. {
  66. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  67. }
  68. #region 图像处理
  69. private void CancelCropImg()
  70. {
  71. if (TextEditEvent != null)
  72. {
  73. TextEditEvent.ClipImage = false;
  74. TextEditEvent.CancelClip();
  75. TextEditEvent.UpdatePDFEditByEventArgs();
  76. IsCrop = false;
  77. }
  78. }
  79. private void CropImg()
  80. {
  81. if (TextEditEvent != null)
  82. {
  83. TextEditEvent.SaveClip();
  84. TextEditEvent.ClipImage = false;
  85. TextEditEvent.UpdatePDFEditByEventArgs();
  86. IsCrop = false;
  87. }
  88. }
  89. private void ExportImg()
  90. {
  91. if (PDFViewer == null || TextEditEvent == null || TextEditEvent.EditType != ComPDFKit.PDFPage.CPDFEditType.EditImage) return;
  92. System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
  93. folder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  94. if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  95. {
  96. if (string.IsNullOrEmpty(folder.SelectedPath))
  97. return;
  98. var keyValueList = PDFViewer.GetSelectedImages();
  99. int i = 0;
  100. foreach (var bitmap in keyValueList)
  101. {
  102. foreach (var bitmapItem in bitmap.Value)
  103. {
  104. bitmapItem.Save(folder.SelectedPath + "\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
  105. i++;
  106. }
  107. }
  108. }
  109. }
  110. private void AddImg()
  111. {
  112. OpenFileDialog openFileDialog = new OpenFileDialog();
  113. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  114. openFileDialog.Multiselect = true;
  115. if ((bool)openFileDialog.ShowDialog())
  116. {
  117. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  118. {
  119. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  120. PDFViewer.AddPDFEditImage(openFileDialog.FileName);
  121. }
  122. }
  123. }
  124. private void ReplaceImg()
  125. {
  126. OpenFileDialog openFileDialog = new OpenFileDialog();
  127. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  128. openFileDialog.Multiselect = true;
  129. if ((bool)openFileDialog.ShowDialog())
  130. {
  131. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  132. {
  133. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  134. TextEditEvent.ReplaceImagePath = openFileDialog.FileName;
  135. TextEditEvent.UpdatePDFEditByEventArgs();
  136. }
  137. }
  138. }
  139. private void CropMode()
  140. {
  141. IsCrop = true;
  142. if (TextEditEvent != null)
  143. {
  144. TextEditEvent.ClipImage = true;
  145. TextEditEvent.UpdatePDFEditByEventArgs();
  146. }
  147. }
  148. private void Clockwise()
  149. {
  150. TextEditEvent.Rotate = TextEditEvent.Rotate + 90;
  151. TextEditEvent.UpdatePDFEditByEventArgs();
  152. GetImagePreView();
  153. }
  154. private void AntiClockwise()
  155. {
  156. TextEditEvent.Rotate = TextEditEvent.Rotate - 90;
  157. TextEditEvent?.UpdatePDFEditByEventArgs();
  158. GetImagePreView();
  159. }
  160. #endregion
  161. #region 布局处理
  162. private void ImgAlignChecked(object obj)
  163. {
  164. if (obj != null)
  165. {
  166. switch ((string)obj)
  167. {
  168. case "AlignLeft":
  169. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  170. break;
  171. case "AlignHorizonCenter":
  172. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  173. break;
  174. case "AlignRight":
  175. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  176. break;
  177. case "DistributeHorizontal":
  178. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  179. break;
  180. case "AlignTop":
  181. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  182. break;
  183. case "AlignVerticalCenter":
  184. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  185. break;
  186. case "AlignBottom":
  187. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  188. break;
  189. case "DistributeVertical":
  190. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  191. break;
  192. }
  193. }
  194. }
  195. private void ReLoadLayoutAlign(int count)
  196. {
  197. if (count >= 2)
  198. IsLayoutAlign = true;
  199. else
  200. IsLayoutAlign = false;
  201. if (count >= 3)
  202. IsLayoutAvgAlign = true;
  203. else
  204. IsLayoutAvgAlign = false;
  205. }
  206. #endregion
  207. protected List<PDFEditEvent> TextEditEventList;
  208. public void OnNavigatedTo(NavigationContext navigationContext)
  209. {
  210. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  211. navigationContext.Parameters.TryGetValue<List<PDFEditEvent>>(ParameterNames.AnnotEvent, out TextEditEventList);
  212. if (PDFViewer != null)
  213. {
  214. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  215. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  216. if (TextEditEventList != null && TextEditEventList.Count > 0)
  217. {
  218. TextEditEvent = TextEditEventList[0];
  219. if(TextEditEventList.Count >1)
  220. {
  221. IsMultiSelectImage = true;
  222. }
  223. else
  224. {
  225. GetImagePreView();
  226. }
  227. GetPDFEdit();
  228. }
  229. }
  230. }
  231. private void GetPDFEdit()
  232. {
  233. }
  234. //点击空白处时
  235. private ContextMenu EmptyStateMenu(object sender)
  236. {
  237. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  238. if (popMenu != null && popMenu.Items.Count == 3)
  239. {
  240. //粘贴
  241. SetPopMenuItem(popMenu.Items[0] as MenuItem, sender, ApplicationCommands.Paste);
  242. //添加文本
  243. SetPopMenuItem(popMenu.Items[1] as MenuItem, sender, AddTextCommand);
  244. //添加图像
  245. SetPopMenuItem(popMenu.Items[2] as MenuItem, sender, AddImgCommand);
  246. }
  247. return popMenu;
  248. }
  249. //选中图像时
  250. private ContextMenu SelectImgPDFEdit(object sender)
  251. {
  252. var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu;
  253. if (popMenu != null && popMenu.Items.Count == 7)
  254. {
  255. //复制
  256. SetPopMenuItem(popMenu.Items[0] as MenuItem, sender, ApplicationCommands.Copy);
  257. //剪切
  258. SetPopMenuItem(popMenu.Items[1] as MenuItem, sender, ApplicationCommands.Cut);
  259. //粘贴
  260. SetPopMenuItem(popMenu.Items[2] as MenuItem, sender, ApplicationCommands.Paste);
  261. //删除
  262. SetPopMenuItem(popMenu.Items[3] as MenuItem, sender, ApplicationCommands.Delete);
  263. //裁剪
  264. SetPopMenuItem(popMenu.Items[4] as MenuItem, sender, CropModeCommand);
  265. //替换
  266. SetPopMenuItem(popMenu.Items[5] as MenuItem, sender, ReplaceImgCommand);
  267. //导出
  268. SetPopMenuItem(popMenu.Items[6] as MenuItem, sender, ExportImgCommand);
  269. }
  270. return popMenu;
  271. }
  272. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  273. {
  274. if (e == null)
  275. return;
  276. switch (e.CommandType)
  277. {
  278. case CommandType.Context:
  279. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  280. {
  281. e.PopupMenu = EmptyStateMenu(sender);
  282. }
  283. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  284. {
  285. e.PopupMenu = SelectImgPDFEdit(sender);
  286. }
  287. break;
  288. default:
  289. e.DoCommand();
  290. break;
  291. }
  292. if (e.PopupMenu != null)
  293. {
  294. e.Handle = true;
  295. }
  296. }
  297. private void GetImagePreView()
  298. {
  299. var list = PDFViewer.GetSelectedImages();
  300. if (list != null && list.Count > 0)
  301. {
  302. System.Drawing.Bitmap bitmap = null;
  303. foreach (var item in list)
  304. {
  305. if (item.Value.Count > 0)
  306. {
  307. bitmap = item.Value[0];
  308. break;
  309. }
  310. }
  311. if (bitmap != null)
  312. {
  313. IntPtr ip = bitmap.GetHbitmap();
  314. System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
  315. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  316. CurrentImg = bitmapSource;
  317. }
  318. }
  319. }
  320. #region 全局
  321. public event EventHandler ClearCheckedAglin;
  322. #endregion
  323. public bool IsNavigationTarget(NavigationContext navigationContext) { return true; }
  324. public void OnNavigatedFrom(NavigationContext navigationContext)
  325. {
  326. IsMultiSelectImage = false;
  327. TextEditEvent = null;
  328. ClearCheckedAglin?.Invoke(null, null);
  329. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  330. }
  331. }
  332. }