using ComPDFKitViewer; using ComPDFKitViewer.PdfViewer; using Microsoft.Win32; using PDF_Office.CustomControl.CompositeControl; 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.PDFEdit { public class TextEditPropertyViewModel : PDFEditVM, INavigationAware { #region private ComPDFKit.PDFPage.Edit.TextAlignType _textAlign; public ComPDFKit.PDFPage.Edit.TextAlignType TextAlign { get { return _textAlign; } set { _textAlign = value; } } #endregion #region Command #region 文本Command public DelegateCommand AddTextCommand { get; set; } public DelegateCommand AddImgCommand { get; set; } public DelegateCommand SelectedColorCommand { get; set; } public DelegateCommand SelectedFontStyleCommand { get; set; } public DelegateCommand FontSizeChangedCommand { get; set; } public DelegateCommand TextAlignCheckedCommand { get; set; } //进入属性面板编辑 public DelegateCommand EditTextModeCommand { get; set; } public DelegateCommand CustomFontStyleCommand { get; set; } public DelegateCommand ReDefineFontStyleCommand { get; set; } public DelegateCommand RestoreDefaultStyleCommand { get; set; } public DelegateCommand LayoutAlignCheckedCommand { get; set; } #endregion #endregion #region 初始化 public TextEditPropertyViewModel() { InitVariable(); InitCommand(); } private void InitVariable() { InitBaseVariable(); } private void InitCommand() { AddTextCommand = new DelegateCommand(AddText); SelectedColorCommand = new DelegateCommand(SelectedColor); SelectedFontStyleCommand = new DelegateCommand(SelectedFontStyle); FontSizeChangedCommand = new DelegateCommand(FontSizeChanged); TextAlignCheckedCommand = new DelegateCommand(TextAlignChecked); EditTextModeCommand = new DelegateCommand(EditTextMode); CustomFontStyleCommand = new DelegateCommand(CustomFontStyle_Click); ReDefineFontStyleCommand = new DelegateCommand(ReDefineFontStyle); RestoreDefaultStyleCommand = new DelegateCommand(RestoreDefaultStyle); LayoutAlignCheckedCommand = new DelegateCommand(LayoutAlignChecked); } #endregion #region 文本处理 private void AddText() { PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText); } private void EditTextMode() { } private void CustomFontStyle_Click() { ContextMenu menu = SelectAnnotContextMenu(); if(menu != null) { menu.IsOpen = true; } } private void ReDefineFontStyle() { } private void RestoreDefaultStyle() { } private void LayoutAlignChecked(object obj) { if(obj != null) { 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 TextAlignChecked(object obj) { if ((string)obj != null && TextEditEvent != null) { switch ((string)obj) { case "AlignLeft": TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignLeft; break; case "AlignCenter": TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignMiddle; break; case "AlignRight": TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignRight; break; case "Align": TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignNone; break; } TextAlign = TextEditEvent.TextAlign; TextEditEvent.UpdatePDFEditByEventArgs(); } } 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 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; } } } #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 #region 右键菜单 //点击空白处时 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 SelectTextBorder(object sender) { var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu; if (popMenu != null && popMenu.Items.Count == 5) { //编辑 SetPopMenuItem(popMenu.Items[0] as MenuItem, sender, EditTextModeCommand); //复制 SetPopMenuItem(popMenu.Items[1] as MenuItem, sender, ApplicationCommands.Copy); //剪切 SetPopMenuItem(popMenu.Items[2] as MenuItem, sender, ApplicationCommands.Cut); //粘贴 SetPopMenuItem(popMenu.Items[3] as MenuItem, sender, ApplicationCommands.Paste); //删除 SetPopMenuItem(popMenu.Items[4] as MenuItem, sender, ApplicationCommands.Delete); } return popMenu; } //选中文字内容 private ContextMenu SelectTextContent(object sender) { var popMenu = App.Current.FindResource("SelectContentMenu") as ContextMenu; if (popMenu != null && popMenu.Items.Count == 6) { //复制 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.Paste); //删除 SetPopMenuItem(popMenu.Items[4] as MenuItem, sender, ApplicationCommands.Delete); ////全部选定 SetPopMenuItem(popMenu.Items[5] as MenuItem, sender, ApplicationCommands.SelectAll); } return popMenu; } #endregion #region 编辑PDF内容触发的事件 /// /// 右键菜单的事件 /// 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); } if (e.PressOnSelectedText == true) { e.PopupMenu = SelectTextContent(sender); } } break; default: e.DoCommand(); break; } if (e.PopupMenu != null) { e.Handle = true; } } private ContextMenu SelectAnnotContextMenu() { var popMenu = App.Current.FindResource("CustomFontStyleFlyoutMenu") as ContextMenu; if (popMenu != null && popMenu.Items.Count == 2) { //剪切 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; } return popMenu; } #region 全局 public event EventHandler ClearCheckedAglin; #endregion #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) { if (TextEditEventList != null && TextEditEventList.Count > 0) { TextEditEvent = TextEditEventList[0]; GetPDFEdit(); if (TextEditEventList.Count == 2) { IsLayoutAlign = true; IsLayoutAvgAlign = false; } else if(TextEditEventList.Count >2) { IsLayoutAlign = true; IsLayoutAvgAlign = true; } else { IsLayoutAlign = false; IsLayoutAvgAlign = false; } } PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler; PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler; ChangedValue -= FontMode_ChangedValue; ChangedValue += FontMode_ChangedValue; IsCanSave = true; } } private void FontMode_ChangedValue(object sender, FontSetModeType e) { if(sender != null) { switch(e) { case FontSetModeType.PresetFontStyes: if(FontStyleList != null && sender is string == true) { var item = FontStyleList.FirstOrDefault(temp => temp.mTag == (string)sender); if (item != null && TextEditEvent != null) { TextEditEvent.FontFamily = item.mFontFamily; TextEditEvent.FontSize = item.mFontSize; TextEditEvent.FontWeight = item.mFontWeight; TextEditEvent.FontStyle = item.mFontStyle; TextEditEvent.UpdatePDFEditByEventArgs(); } } break; case FontSetModeType.FontFamilys: if(sender is string == true) { TextEditEvent.FontFamily = new FontFamily((string)sender); TextEditEvent.UpdatePDFEditByEventArgs(); } break; case FontSetModeType.FontSizes: if (sender is double == true && (double)sender > 0 && TextEditEvent.FontSize > 0) { TextEditEvent.FontSize = (double)sender; TextEditEvent.UpdatePDFEditByEventArgs(); } break; case FontSetModeType.FontWeight_Style: UpdateFontWeight_Style(); TextEditEvent.FontWeight = FontWeightItem; TextEditEvent.FontStyle = FontStyleItem; TextEditEvent.UpdatePDFEditByEventArgs(); break; case FontSetModeType.FontColor: if(sender is Color == true) { var setColor = (Color)sender; var eventColor = TextEditEvent.FontColor; bool isok = eventColor.A != setColor.A || eventColor.B != setColor.B || eventColor.G != setColor.G || eventColor.R != setColor.R; if(isok) { TextEditEvent.FontColor = eventColor; TextEditEvent.UpdatePDFEditByEventArgs(); } } break; case FontSetModeType.TextAlignment: break; } } } private void GetPDFEdit() { if (TextEditEvent != null) { SelectColor = new SolidColorBrush(TextEditEvent.FontColor); FontSizeData = new ComboDataItem(TextEditEvent.FontSize); if(TextEditEvent.FontFamily != null) FontFamilyData = new ComboDataItem(TextEditEvent.FontFamily.ToString(), TextEditEvent.FontFamily.ToString()); SelectedFontWeights_Style(TextEditEvent.FontStyle, TextEditEvent.FontWeight); TextAlign = TextEditEvent.TextAlign; SelectColor = new SolidColorBrush(TextEditEvent.FontColor); } } public bool IsNavigationTarget(NavigationContext navigationContext){ return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { IsCanSave = false; TextEditEvent = null; ClearCheckedAglin?.Invoke(null, null); PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler; ChangedValue -= FontMode_ChangedValue; } } }