using ComPDFKitViewer; using ComPDFKitViewer.PdfViewer; using PDF_Office.Model; using PDF_Office.Model.PropertyPanel.AnnotPanel; 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; using System.Windows.Media; namespace PDF_Office.ViewModels.PropertyPanel { public class TextEditPropertyViewModel : BindableBase, INavigationAware { #region 属性 private Brush selectColor = new SolidColorBrush(Colors.Black); public Brush SelectColor { get { return selectColor; } set { SetProperty(ref selectColor, value); if (TextEditEvent != null) { bool isok = TextEditEvent.FontColor.A != (SelectColor as SolidColorBrush).Color.A || TextEditEvent.FontColor.B != (SelectColor as SolidColorBrush).Color.B || TextEditEvent.FontColor.G != (SelectColor as SolidColorBrush).Color.G || TextEditEvent.FontColor.R != (SelectColor as SolidColorBrush).Color.R; if (isok) { TextEditEvent.FontColor = (SelectColor as SolidColorBrush).Color; TextEditEvent.UpdatePDFEditByEventArgs(); } } } } private FontFamily fontFamily = new FontFamily("Courier"); public FontFamily TextFontFamily { get { return fontFamily; } set { SetProperty(ref fontFamily, value); if (TextEditEvent != null) { TextEditEvent.FontFamily = fontFamily; TextEditEvent.UpdatePDFEditByEventArgs(); } } } private FontWeight fontWeights = FontWeights.Normal; public FontWeight TextFontWeights { get { return fontWeights; } set { SetProperty(ref fontWeights, value); if (TextEditEvent != null) { TextEditEvent.FontWeight = fontWeights; TextEditEvent.UpdatePDFEditByEventArgs(); } } } private FontStyle fontStyle = FontStyles.Normal; public FontStyle TextFontStyle { get { return fontStyle; } set { SetProperty(ref fontStyle, value); if (TextEditEvent != null) { TextEditEvent.FontStyle = fontStyle; TextEditEvent.UpdatePDFEditByEventArgs(); } } } private int fontSize = 24; public int TextFontSize { get { return fontSize; } set { SetProperty(ref fontSize, value); if (TextEditEvent != null) { TextEditEvent.FontSize = fontSize; TextEditEvent.UpdatePDFEditByEventArgs(); } } } private List fontStyleList = new List(); public List FontStyleList { get { return fontStyleList; } set { SetProperty(ref fontStyleList, value); } } #endregion #region Command public DelegateCommand SelectedColorCommand { get; set; } public DelegateCommand SelectedFontStyleCommand { get; set; } public DelegateCommand FontFamilyChangedCommand { get; set; } public DelegateCommand FontStyleChangedCommand { get; set; } public DelegateCommand FontSizeChangedCommand { get; set; } #endregion private ComPDFKitViewer.PDFEditEvent TextEditEvent; public TextEditPropertyViewModel() { InitVariable(); InitCommand(); } private void InitVariable() { InitFontStyles(); } private void InitFontStyles() { FontStyleList = LoadFontStyle.Load(); } private void InitCommand() { SelectedColorCommand = new DelegateCommand(SelectedColor); SelectedFontStyleCommand = new DelegateCommand(SelectedFontStyle); FontFamilyChangedCommand = new DelegateCommand(FontFamilyChanged); FontStyleChangedCommand = new DelegateCommand(FontStyleChanged); FontSizeChangedCommand = new DelegateCommand(FontSizeChanged); } private void SelectedColor(object obj) { if (obj != null) { var colorValue = (Color)obj; if (colorValue != null) { SelectColor = new SolidColorBrush(colorValue); } } } private void SelectedFontStyle(object obj) { if (obj != null && (FontStyleItem)obj != null) { var item = (FontStyleItem)obj; } } private void FontFamilyChanged(object obj) { if (obj != null) { if ((int)obj > -1) { if ((int)obj == 0) { TextFontFamily = new FontFamily("Courier"); } if ((int)obj == 1) { TextFontFamily = new FontFamily("Helvetica"); } if ((int)obj == 2) { TextFontFamily = new FontFamily("Times"); } } } } private void FontStyleChanged(object obj) { if (obj != null) { var item = (ComboBoxItem)obj; var content = (string)item.Content; if (content != null) { if (content == "Regular") { TextFontWeights = FontWeights.Normal; TextFontStyle = FontStyles.Normal; } if (content == "Bold") { TextFontWeights = FontWeights.Bold; TextFontStyle = FontStyles.Normal; } if (content == "Italic") { TextFontWeights = FontWeights.Normal; TextFontStyle = FontStyles.Italic; } if (content == "Bold Italic") { TextFontWeights = FontWeights.Bold; TextFontStyle = FontStyles.Italic; } } } } private void FontSizeChanged(object obj) { if (obj != null) { var item = (ComboBoxItem)obj; var content = (string)item.Content; if (content != null) { var intData = int.Parse(content); TextFontSize = intData; } } } public void OnNavigatedTo(NavigationContext navigationContext) { } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } private CPDFViewer PDFViewer; public void OnNavigatedFrom(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); 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) { ContextMenu popMenu = new ContextMenu(); var menu = new MenuItem(); menu.Header = "Cut"; popMenu.Items.Add(menu); menu = new MenuItem(); menu.Header = "Copy"; popMenu.Items.Add(menu); menu = new MenuItem(); menu.Header = "Paste"; popMenu.Items.Add(menu); menu = new MenuItem(); menu.Header = "Delete"; popMenu.Items.Add(menu); menu = new MenuItem(); menu.Header = "SelectAll"; popMenu.Items.Add(menu); switch (e.CommandType) { case CommandType.Context: if (popMenu.Items.Count == 5) { MenuItem menuItem = popMenu.Items[0] as MenuItem; menuItem.CommandTarget = (UIElement)sender; menuItem.Command = ApplicationCommands.Cut; menuItem = popMenu.Items[1] as MenuItem; menuItem.CommandTarget = (UIElement)sender; menuItem.Command = ApplicationCommands.Copy; menuItem = popMenu.Items[2] as MenuItem; menuItem.CommandTarget = (UIElement)sender; menuItem.Command = ApplicationCommands.Paste; menuItem = popMenu.Items[3] as MenuItem; menuItem.CommandTarget = (UIElement)sender; menuItem.Command = ApplicationCommands.Delete; menuItem = popMenu.Items[4] as MenuItem; menuItem.CommandTarget = (UIElement)sender; menuItem.Command = ApplicationCommands.SelectAll; e.PopupMenu = popMenu; if (e.PopupMenu != null) { e.Handle = true; } } break; default: e.DoCommand(); break; } } private void PDFViewer_PDFEditActiveHandler(object sender, List e) { if(e != null && e.Count > 0) { TextEditEvent = e[0]; } else { // TextEditEvent = null; } } } }