using ComPDFKitViewer; using ComPDFKitViewer.PdfViewer; using Microsoft.Win32; using PDF_Office.Model; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace PDF_Office.ViewModels.PropertyPanel.PDFEdit { public class ImageEditPropertyViewModel : PDFEditVM, INavigationAware { #region 图像属性 private double _transpent ; public double Transpent { get { return _transpent; } set { SetProperty(ref _transpent, value); if (TextEditEvent != null) { TextEditEvent.Transparent = (int)(_transpent*2.55); TextEditEvent.UpdatePDFEditByEventArgs(); } } } private bool _isCrop = false; public bool IsCrop{get { return _isCrop; } set{SetProperty(ref _isCrop, value); }} //选中的图像 private System.Windows.Media.Imaging.BitmapSource _currentImg; public System.Windows.Media.Imaging.BitmapSource CurrentImg {get { return _currentImg; } set{ SetProperty(ref _currentImg, value); }} private bool _isMultiSelectImage = false; public bool IsMultiSelectImage { get { return _isMultiSelectImage; } set { SetProperty(ref _isMultiSelectImage, value); }} #endregion #region 图像Command public DelegateCommand ReplaceImgCommand { get; set; } public DelegateCommand ExportImgCommand { get; set; } public DelegateCommand CropImgCommand { get; set; } public DelegateCommand ImgAlignCheckedCommand { get; set; } /// /// 逆时针旋转 /// public DelegateCommand AntiClockwiseCommand { get; set; } /// /// 顺时针旋转 /// public DelegateCommand ClockwiseCommand { get; set; } public DelegateCommand CropModeCommand { get; set; } public DelegateCommand CancelCropCommand { get; set; } public DelegateCommand AddTextCommand { get; set; } public DelegateCommand AddImgCommand { get; set; } #endregion public ImageEditPropertyViewModel() { InitCommand(); } private void InitCommand() { AddTextCommand = new DelegateCommand(AddText); AddImgCommand = new DelegateCommand(AddImg); ReplaceImgCommand = new DelegateCommand(ReplaceImg); ExportImgCommand = new DelegateCommand(ExportImg); CropImgCommand = new DelegateCommand(CropImg); ImgAlignCheckedCommand = new DelegateCommand(ImgAlignChecked); AntiClockwiseCommand = new DelegateCommand(AntiClockwise); ClockwiseCommand = new DelegateCommand(Clockwise); CropModeCommand = new DelegateCommand(CropMode); CancelCropCommand = new DelegateCommand(CancelCropImg); } private void AddText() { PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText); } #region 图像处理 private void CancelCropImg() { if (TextEditEvent != null) { TextEditEvent.ClipImage = false; TextEditEvent.CancelClip(); TextEditEvent.UpdatePDFEditByEventArgs(); IsCrop = false; } } private void CropImg() { if (TextEditEvent != null) { TextEditEvent.SaveClip(); TextEditEvent.ClipImage = false; TextEditEvent.UpdatePDFEditByEventArgs(); IsCrop = false; } } private void ExportImg() { if (PDFViewer == null || TextEditEvent == null || TextEditEvent.EditType != ComPDFKit.PDFPage.CPDFEditType.EditImage) return; System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog(); folder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (string.IsNullOrEmpty(folder.SelectedPath)) return; var keyValueList = PDFViewer.GetSelectedImages(); int i = 0; foreach (var bitmap in keyValueList) { foreach (var bitmapItem in bitmap.Value) { bitmapItem.Save(folder.SelectedPath + "\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png); i++; } } } } private void AddImg() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;"; openFileDialog.Multiselect = true; if ((bool)openFileDialog.ShowDialog()) { if (string.IsNullOrEmpty(openFileDialog.FileName) == false) { PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage); PDFViewer.AddPDFEditImage(openFileDialog.FileName); } } } private void ReplaceImg() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;"; openFileDialog.Multiselect = true; if ((bool)openFileDialog.ShowDialog()) { if (string.IsNullOrEmpty(openFileDialog.FileName) == false) { PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage); TextEditEvent.ReplaceImagePath = openFileDialog.FileName; TextEditEvent.UpdatePDFEditByEventArgs(); } } } private void CropMode() { IsCrop = true; if (TextEditEvent != null) { TextEditEvent.ClipImage = true; TextEditEvent.UpdatePDFEditByEventArgs(); } } private void Clockwise() { TextEditEvent.Rotate = TextEditEvent.Rotate + 90; TextEditEvent.UpdatePDFEditByEventArgs(); GetImagePreView(); } private void AntiClockwise() { TextEditEvent.Rotate = TextEditEvent.Rotate - 90; TextEditEvent?.UpdatePDFEditByEventArgs(); GetImagePreView(); } #endregion #region 布局处理 private void ImgAlignChecked(object obj) { if (obj != null) { switch ((string)obj) { case "AlignLeft": PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft); break; case "AlignHorizonCenter": PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter); break; case "AlignRight": PDFViewer.SetPDFEditAligment(AlignModes.AlignRight); break; case "DistributeHorizontal": PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal); break; case "AlignTop": PDFViewer.SetPDFEditAligment(AlignModes.AlignTop); break; case "AlignVerticalCenter": PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter); break; case "AlignBottom": PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom); break; case "DistributeVertical": PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical); break; } } } private void ReLoadLayoutAlign(int count) { if (count >= 2) IsLayoutAlign = true; else IsLayoutAlign = false; if (count >= 3) IsLayoutAvgAlign = true; else IsLayoutAvgAlign = false; } #endregion protected List TextEditEventList; public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); navigationContext.Parameters.TryGetValue>(ParameterNames.AnnotEvent, out TextEditEventList); if (PDFViewer != null) { PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler; PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler; if (TextEditEventList != null && TextEditEventList.Count > 0) { TextEditEvent = TextEditEventList[0]; if(TextEditEventList.Count >1) { IsMultiSelectImage = true; } else { GetImagePreView(); } if (TextEditEventList.Count == 2) { IsLayoutAlign = true; IsLayoutAvgAlign = false; } else if (TextEditEventList.Count > 2) { IsLayoutAlign = true; IsLayoutAvgAlign = true; } else { IsLayoutAlign = false; IsLayoutAvgAlign = false; } GetPDFEdit(); } } } private void GetPDFEdit() { var tranUI = (TextEditEvent.Transparent / 255.0)*100; var temp = Math.Round((double)tranUI, 0); Transpent = temp; } //点击空白处时 private ContextMenu EmptyStateMenu(object sender) { var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu; if (popMenu != null && popMenu.Items.Count == 3) { //粘贴 SetPopMenuItem(popMenu.Items[0] as MenuItem, sender, ApplicationCommands.Paste); //添加文本 SetPopMenuItem(popMenu.Items[1] as MenuItem, sender, AddTextCommand); //添加图像 SetPopMenuItem(popMenu.Items[2] as MenuItem, sender, AddImgCommand); } return popMenu; } //选中图像时 private ContextMenu SelectImgPDFEdit(object sender) { var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu; if (popMenu != null && popMenu.Items.Count == 7) { //复制 SetPopMenuItem(popMenu.Items[0] as MenuItem, sender, ApplicationCommands.Copy); //剪切 SetPopMenuItem(popMenu.Items[1] as MenuItem, sender, ApplicationCommands.Cut); //粘贴 SetPopMenuItem(popMenu.Items[2] as MenuItem, sender, ApplicationCommands.Paste); //删除 SetPopMenuItem(popMenu.Items[3] as MenuItem, sender, ApplicationCommands.Delete); //裁剪 SetPopMenuItem(popMenu.Items[4] as MenuItem, sender, CropModeCommand); //替换 SetPopMenuItem(popMenu.Items[5] as MenuItem, sender, ReplaceImgCommand); //导出 SetPopMenuItem(popMenu.Items[6] as MenuItem, sender, ExportImgCommand); } return popMenu; } private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e) { if (e == null) return; switch (e.CommandType) { case CommandType.Context: if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None) { e.PopupMenu = EmptyStateMenu(sender); } else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage) { e.PopupMenu = SelectImgPDFEdit(sender); } break; default: e.DoCommand(); break; } if (e.PopupMenu != null) { e.Handle = true; } } private void GetImagePreView() { try { var list = PDFViewer.GetSelectedImages(); if (list != null && list.Count > 0) { System.Drawing.Bitmap bitmap = null; foreach (var item in list) { if (item.Value.Count > 0) { bitmap = item.Value[0]; break; } } if (bitmap != null) { IntPtr ip = bitmap.GetHbitmap(); System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); CurrentImg = bitmapSource; } } } catch { } } #region 全局 public event EventHandler ClearCheckedAglin; #endregion public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { IsMultiSelectImage = false; TextEditEvent = null; ClearCheckedAglin?.Invoke(null, null); PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler; } } }