ImageEditPropertyViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  285. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  286. LoadedPDFEdit();
  287. isCanSave = true;
  288. }
  289. }
  290. private void LoadedPDFEdit()
  291. {
  292. if (TextEditEventList != null && TextEditEventList.Count > 0)
  293. {
  294. TextEditEvent = TextEditEventList[0];
  295. if (TextEditEventList.Count > 1)
  296. {
  297. IsMultiSelectImage = true;
  298. }
  299. else
  300. {
  301. GetImagePreView();
  302. }
  303. if (TextEditEventList.Count == 2)
  304. {
  305. IsLayoutAlign = true;
  306. IsLayoutAvgAlign = false;
  307. }
  308. else if (TextEditEventList.Count > 2)
  309. {
  310. IsLayoutAlign = true;
  311. IsLayoutAvgAlign = true;
  312. }
  313. else
  314. {
  315. IsLayoutAlign = false;
  316. IsLayoutAvgAlign = false;
  317. }
  318. GetPDFEdit();
  319. }
  320. }
  321. private void GetPDFEdit()
  322. {
  323. var tranUI = (TextEditEvent.Transparent / 255.0)*100;
  324. var temp = Math.Round((double)tranUI, 0);
  325. Transpent = temp;
  326. OpacityUI = temp/100.0;
  327. }
  328. //点击空白处时
  329. private ContextMenu EmptyStateMenu(object sender)
  330. {
  331. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  332. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  333. //粘贴
  334. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  335. //添加文本
  336. customMenu.SetMenuBinding(1, AddTextCommand);
  337. //添加图像
  338. customMenu.SetMenuBinding(2, AddImgCommand);
  339. return popMenu;
  340. }
  341. //选中图像时
  342. private ContextMenu SelectImgPDFEdit(object sender)
  343. {
  344. var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu;
  345. CustomPopMenu customMenu = new CustomPopMenu(popMenu,sender);
  346. //复制
  347. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  348. //剪切
  349. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  350. //粘贴
  351. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  352. //删除
  353. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  354. //裁剪
  355. customMenu.SetMenuBinding(4, CropModeCommand);
  356. //替换
  357. customMenu.SetMenuBinding(5, ReplaceImgCommand);
  358. //导出
  359. customMenu.SetMenuBinding(6, ExportImgCommand);
  360. return popMenu;
  361. }
  362. //选中文字的框(暂时暂时)
  363. private ContextMenu SelectTextBorder(object sender)
  364. {
  365. var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu;
  366. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  367. //编辑
  368. customMenu.SetMenuBinding(0, EditImgModeCommand);
  369. //复制
  370. customMenu.SetMenuBinding(1, ApplicationCommands.Copy);
  371. //剪切
  372. customMenu.SetMenuBinding(2, ApplicationCommands.Cut);
  373. //粘贴
  374. customMenu.SetMenuBinding(3, ApplicationCommands.Paste);
  375. //删除
  376. customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  377. customMenu.SetVisibilityProperty(4, false);
  378. return popMenu;
  379. }
  380. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  381. {
  382. if (e == null)
  383. return;
  384. switch (e.CommandType)
  385. {
  386. case CommandType.Context:
  387. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  388. {
  389. e.PopupMenu = SelectImgPDFEdit(sender);
  390. }
  391. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  392. {
  393. e.PopupMenu = SelectTextBorder(sender);
  394. }
  395. break;
  396. default:
  397. e.DoCommand();
  398. break;
  399. }
  400. if (e.PopupMenu != null)
  401. {
  402. e.Handle = true;
  403. }
  404. }
  405. private void GetImagePreView()
  406. {
  407. try
  408. {
  409. var list = PDFViewer.GetSelectedImages();
  410. if (list != null && list.Count > 0)
  411. {
  412. System.Drawing.Bitmap bitmap = null;
  413. foreach (var item in list)
  414. {
  415. if (item.Value.Count > 0)
  416. {
  417. bitmap = item.Value[0];
  418. break;
  419. }
  420. }
  421. if (bitmap != null)
  422. {
  423. IntPtr ip = bitmap.GetHbitmap();
  424. System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
  425. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  426. CurrentImg = bitmapSource;
  427. }
  428. }
  429. }
  430. catch
  431. {
  432. }
  433. }
  434. #region 全局
  435. #endregion
  436. public bool IsNavigationTarget(NavigationContext navigationContext) { return true; }
  437. public void OnNavigatedFrom(NavigationContext navigationContext)
  438. {
  439. isCanSave = false;
  440. IsMultiSelectImage = false;
  441. TextEditEvent = null;
  442. ClearCheckedAglin?.Invoke(null, null);
  443. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  444. }
  445. }
  446. }