ImageEditPropertyViewModel.cs 14 KB

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