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; 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(); #endregion #region 属性 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); } } #endregion #region Command // 添加文本、图像 public DelegateCommand AddContentCommand { get; set; } public DelegateCommand AddTextCommand { get; set; } public DelegateCommand AddImgCommand { 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); } private void InitBtnToProperty() { btnToProperty.Add("Text", "TextEditProperty"); btnToProperty.Add("Image", "ImageEditProperty"); btnToProperty.Add("TextAndImage", "ImageTextEditProperty"); btnToProperty.Add("PropertyPanelContent", "PropertyPanelContent"); } #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 EnterEditMode(bool isCheckMode,string modeType) { if (isCheckMode) { if (modeType == "Text") { IsImgEdit = false; IsTextEdit = true; PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText); } else { IsImgEdit = true; IsTextEdit = false; PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage); } } else { IsImgEdit = false; IsTextEdit = false; PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None); PDFViewer.SetMouseMode(MouseModes.PDFEdit); } } #region Command功能 private void AddText() { EnterEditMode(true, "Text"); } private void AddImg() { EnterEditMode(true, "Image"); } #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); System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => { regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters); })); ShowPropertyPanel(true); } } private void ShowPropertyPanel(bool show = true) { viewContentViewModel.IsPropertyOpen = show; } private void PDFViewer_PDFEditActiveHandler(object sender, List e) { if (e != null && e.Count > 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); } } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); navigationContext.Parameters.TryGetValue(ParameterNames.ViewContentViewModel, out viewContentViewModel); if (PDFViewer != null) { PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler; PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler; PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler; PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler; } } 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); } break; default: e.DoCommand(); break; } if (e.PopupMenu != null) { e.Handle = true; } } //点击空白处时 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; } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { IsImgEdit = false; IsTextEdit = false; PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler; PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler; ShowPropertyPanel(false); } } }