using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media; using System.Windows; using PDF_Master.Model.PropertyPanel.AnnotPanel; using System.Windows.Controls; using Prism.Regions; using ComPDFKitViewer.PdfViewer; using PDF_Master.Model; using Microsoft.Win32; using PDF_Master.CustomControl; using ComPDFKitViewer; using System.Windows.Input; using PDF_Master.Helper; using ComPDFKitViewer.AnnotEvent; namespace PDF_Master.ViewModels.Tools { public class TextEditToolContentViewModel: BindableBase, INavigationAware { #region 属性与变量 public ViewContentViewModel viewContentViewModel; private CPDFViewer PDFViewer; private IRegionManager regions; private Dictionary btnToProperty = new Dictionary(); private bool _isTextEdit = false; public bool IsTextEdit { get { return _isTextEdit; } set { SetProperty(ref _isTextEdit, value); } } private bool _isImgEdit = false; public bool IsImgEdit { get { return _isImgEdit; } set { SetProperty(ref _isImgEdit, value); } } private bool _flg=false; public bool flg { get { return _flg; } set { SetProperty(ref _flg, value); } } #endregion #region Command // 添加文本、图像 public DelegateCommand AddContentCommand { get; set; } public DelegateCommand AddTextCommand { get; set; } public DelegateCommand AddImgCommand { get; set; } public DelegateCommand EditTextModeCommand { get; set; } #endregion #region 初始化 public TextEditToolContentViewModel(IRegionManager regionManager) { regions = regionManager; InitCommand(); InitBtnToProperty(); } private void InitCommand() { AddContentCommand = new DelegateCommand(AddContent); AddTextCommand = new DelegateCommand(AddText); AddImgCommand = new DelegateCommand(AddImg); EditTextModeCommand = new DelegateCommand(EditTextMode); } private void InitBtnToProperty() { btnToProperty.Add("Text", "TextEditProperty"); btnToProperty.Add("Image", "ImageEditProperty"); btnToProperty.Add("TextAndImage", "ImageTextEditProperty"); btnToProperty.Add("PropertyPanelContent", "PropertyPanelContent"); } #endregion #region 右键菜单 //点击空白处时 private ContextMenu EmptyStateMenu(object sender) { var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu; CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender); //粘贴 customMenu.SetMenuBinding(0, ApplicationCommands.Paste); //添加文本 customMenu.SetMenuBinding(1, AddTextCommand); //添加图像 customMenu.SetMenuBinding(2, AddImgCommand); return popMenu; } //选中文字的框 private ContextMenu SelectTextBorder(object sender) { var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu; CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender); //复制 customMenu.SetMenuBinding(0, ApplicationCommands.Copy); //剪切 customMenu.SetMenuBinding(1, ApplicationCommands.Cut); //粘贴 customMenu.SetMenuBinding(2, ApplicationCommands.Paste); //删除 customMenu.SetMenuBinding(3, ApplicationCommands.Delete); customMenu.SetVisibilityProperty(6, false); return popMenu; } //选中文字内容 private ContextMenu SelectTextContent(object sender) { var popMenu = App.Current.FindResource("SelectContentMenu") as ContextMenu; CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender); //复制 customMenu.SetMenuBinding(0, ApplicationCommands.Copy); //剪切 customMenu.SetMenuBinding(1, ApplicationCommands.Cut); //粘贴 customMenu.SetMenuBinding(2, ApplicationCommands.Paste); //粘贴并匹配样式 customMenu.SetVisibilityProperty(3, false); //删除 customMenu.SetMenuBinding(4, ApplicationCommands.Delete); //全部选定 customMenu.SetMenuBinding(5, ApplicationCommands.SelectAll); return popMenu; } //编辑中右键 private ContextMenu EditTextContent(object sender) { var popMenu = App.Current.FindResource("NoneSelectContentMenu") as ContextMenu; CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender); ////复制 //customMenu.SetMenuBinding(0, ApplicationCommands.Copy); ////剪切 //customMenu.SetMenuBinding(1, ApplicationCommands.Cut); //全部选定 customMenu.SetMenuBinding(0, ApplicationCommands.SelectAll); //粘贴 customMenu.SetMenuBinding(1, ApplicationCommands.Paste); //粘贴并匹配样式 customMenu.SetVisibilityProperty(2, false); ////删除 //customMenu.SetMenuBinding(4, ApplicationCommands.Delete); return popMenu; } #endregion #region 图片右键菜单 //选中图像时 private ContextMenu SelectImgPDFEdit(object sender) { var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu; CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender); //复制 customMenu.SetMenuBinding(0, ApplicationCommands.Copy); //剪切 customMenu.SetMenuBinding(1, ApplicationCommands.Cut); //粘贴 customMenu.SetMenuBinding(2, ApplicationCommands.Paste); //删除 customMenu.SetMenuBinding(3, ApplicationCommands.Delete); //裁剪 customMenu.SetMenuBinding(4, new DelegateCommand(CropMode)); //替换 customMenu.SetMenuBinding(5, new DelegateCommand(ReplaceImg)); //导出 customMenu.SetMenuBinding(6, new DelegateCommand(ExportImg)); return popMenu; } //选中裁剪图像时 private ContextMenu CropImgPDFEdit(object sender) { var popMenu = App.Current.FindResource("CropImgMenu") as ContextMenu; CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender); //确认裁剪 customMenu.SetMenuBinding(0, new DelegateCommand(CropImg)); //取消裁剪 customMenu.SetMenuBinding(1, new DelegateCommand(CancelCropImg)); //还原裁剪 customMenu.SetMenuBinding(2, new DelegateCommand(RestoreCropImg)); return popMenu; } //多选图片右键 private ContextMenu SelectMoreImage(object sender) { var popMenu = App.Current.FindResource("SelectMoreImageMenu") as ContextMenu; CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender); //复制 customMenu.SetMenuBinding(0, ApplicationCommands.Copy); //剪切 customMenu.SetMenuBinding(1, ApplicationCommands.Cut); //粘贴 customMenu.SetMenuBinding(2, ApplicationCommands.Paste); //删除 customMenu.SetMenuBinding(3, ApplicationCommands.Delete); //导出 customMenu.SetMenuBinding(4, new DelegateCommand(ExportImg)); return popMenu; } #endregion #region 快捷键 private void ShortCut_KeyDown(object sender, KeyEventArgs e) { try { if (e.Key == Key.Escape) { if (PDFViewer != null) { //缩小esc的操作范围 if (PDFViewer.ToolManager != null && PDFViewer.GetPDFEditCreateType() == ComPDFKit.PDFPage.CPDFEditType.EditText) { PDFViewer.RemovePDFEditEmptyText(); //只有在有画框的时候才进行 if (PDFViewer.MouseMode == MouseModes.PDFEdit&& PDFViewer.ToolManager.HasTool == true) { PDFViewer.RemoveTool(false); } else if (IsTextEdit == true || IsImgEdit == true) { IsImgEdit = false; IsTextEdit = false; PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None); //文本和图像都框选 PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage); PDFViewer.SetMouseMode(MouseModes.PDFEdit); PDFViewer.ReloadDocument(); ShowPropertyPanel(false); } } else if (PDFViewer.ToolManager != null && PDFViewer.GetPDFEditCreateType() == ComPDFKit.PDFPage.CPDFEditType.EditImage) { PDFViewer.RemovePDFEditEmptyText(); if (PDFViewer.MouseMode == MouseModes.PDFEdit&&PDFViewer.ToolManager.HasTool == true) { PDFViewer.RemoveTool(false); } else if (IsTextEdit == true || IsImgEdit == true) { IsImgEdit = false; IsTextEdit = false; PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None); //文本和图像都框选 PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage); PDFViewer.SetMouseMode(MouseModes.PDFEdit); PDFViewer.ReloadDocument(); ShowPropertyPanel(false); } } } } } catch { } } #endregion //模式选择进入 public void AddContent(object obj) { if (PDFViewer == null || obj == null || obj as CustomIconToggleBtn == null) return; var btn = obj as CustomIconToggleBtn; EnterEditMode((bool)btn.IsChecked, btn.Tag.ToString()); } //编辑按钮逻辑,暂时保留 private void EditTextMode() { } //对应模式的逻辑 private void EnterEditMode(bool isCheckMode,string modeType) { if (isCheckMode) { if (modeType == "Text") { IsImgEdit = false; IsTextEdit = true; //只框选文本 PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText); PDFViewer.SetMouseMode(MouseModes.PDFEdit); PDFViewer.ReloadDocument(); PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText); List pDFEditEvents = new List(); AddToPropertyPanel("Text", null); ShowPropertyPanel(true); } else { IsImgEdit = true; IsTextEdit = false; //只框选图像 PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditImage); PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage); PDFViewer.SetMouseMode(MouseModes.PDFEdit); PDFViewer.ReloadDocument(); } } else { IsImgEdit = false; IsTextEdit = false; PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None); //文本和图像都框选 PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage); PDFViewer.SetMouseMode(MouseModes.PDFEdit); PDFViewer.ReloadDocument(); ShowPropertyPanel(false); } } #region Command功能 private void AddText() { EnterEditMode(true, "Text"); } private void AddImg() { EnterEditMode(true, "Image"); } //进入裁剪模式 private void CropMode() { if (TextEditEvent != null) { TextEditEvent.ClipImage = true; TextEditEvent.UpdatePDFEditByEventArgs(); flg = true; } } //取消裁剪 private void CancelCropImg() { if (TextEditEvent != null) { TextEditEvent.ClipImage = false; TextEditEvent.CancelClip(); flg = false; TextEditEvent.UpdatePDFEditByEventArgs(); } } //还原裁剪 private void RestoreCropImg() { if (TextEditEvent != null) { TextEditEvent.RestoreClip(); } } //完成裁剪 private void CropImg() { if (TextEditEvent != null) { TextEditEvent.SaveClip(); TextEditEvent.ClipImage = false; flg = false; TextEditEvent.UpdatePDFEditByEventArgs(); GetImagePreView(); } } //导出图片 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++; } } var strFilePath = folder.SelectedPath + "\\0.png"; CommonHelper.ShowFileBrowser(strFilePath); } } //替换图片 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) { TextEditEvent.ReplaceImagePath = openFileDialog.FileName; TextEditEvent.UpdatePDFEditByEventArgs(); ReplaceimgTask(); } } } //此处只有异步 ReplaceimgTask()才有作用 async Task ReplaceimgTask() { await Task.Delay(100); GetImagePreView(); } //选中的图像 private System.Windows.Media.Imaging.BitmapSource _currentImg; public System.Windows.Media.Imaging.BitmapSource CurrentImg { get { return _currentImg; } set { SetProperty(ref _currentImg, value); } } 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 { } } #endregion //传参数给属性面板 private void AddToPropertyPanel(string type, List e) { if (btnToProperty.ContainsKey(type)) { NavigationParameters parameters = new NavigationParameters(); parameters.Add(ParameterNames.PDFViewer, PDFViewer); parameters.Add(ParameterNames.AnnotEvent, e); parameters.Add(ParameterNames.TextEditToolContentViewModel, this); System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => { regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters); })); if(e!=null) { ShowPropertyPanel(true); } } } //控制属性面板是否展示 private void ShowPropertyPanel(bool show = true) { viewContentViewModel.IsPropertyOpen = show; } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); navigationContext.Parameters.TryGetValue(ParameterNames.ViewContentViewModel, out viewContentViewModel); if (PDFViewer != null) { if(viewContentViewModel.TabSelectedIndex==2) { IsImgEdit = false; IsTextEdit = false; } //左键激活 PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler; PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler; //右键 PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler; PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler; //图片添加 PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler; PDFViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler; //粘贴后选中 PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler; PDFViewer.PDFEditHandler += PDFViewer_PDFEditHandler; KeyEventsHelper.KeyDown -= ShortCut_KeyDown; KeyEventsHelper.KeyDown += ShortCut_KeyDown; } } private void PDFViewer_PDFEditHandler(object sender, List e) { PDFViewer.SelectPDFEdit(e,true); } protected PDFEditEvent TextEditEvent; //左键激活逻辑 private void PDFViewer_PDFEditActiveHandler(object sender, List e) { if (e != null && e.Count > 0) { TextEditEvent=e[0]; bool isText = false; bool isImg = false; foreach (var item in e) { if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage) { isImg = true; } if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText) { isText = true; } } if (isText == true && isImg == false) { AddToPropertyPanel("Text", e); } if (isText == false && isImg == true) { AddToPropertyPanel("Image", e); } if (isText == true && isImg == true) { AddToPropertyPanel("TextAndImage", e); } } else { AddToPropertyPanel("PropertyPanelContent", null); } } //右键点击逻辑 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.EditText) { //文字编辑 if (e.PressOnBorder == true) { e.PopupMenu = SelectTextBorder(sender); } else if(e.PressOnSelectedText == true) { e.PopupMenu = SelectTextContent(sender); } else if(e.SelectText=="") { e.PopupMenu= EditTextContent(sender); } else { e.PopupMenu = SelectTextContent(sender); } } else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage) { if (e.IsClipState == true) { e.PopupMenu = CropImgPDFEdit(sender); } else { e.PopupMenu = SelectImgPDFEdit(sender); } } break; default: e.DoCommand(); break; } if (e.PopupMenu != null) { e.Handle = true; } flg = e.IsClipState; } //图片添加逻辑 private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e) { if (e.NotifyType == CustomNotifyType.PDFEditImage) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;"; if (openFileDialog.ShowDialog() == true) { PDFViewer.AddPDFEditImage(openFileDialog.FileName,500,500); } } } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { IsImgEdit = false; IsTextEdit = false; PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler; PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler; PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler; PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler; KeyEventsHelper.KeyDown -= ShortCut_KeyDown; ShowPropertyPanel(false); } } }