using ComPDFKitViewer; using ComPDFKitViewer.AnnotEvent; using ComPDFKitViewer.PdfViewer; using PDF_Office.Helper; 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.Form { public class FormsToolContentViewModel : BindableBase, INavigationAware { #region 属性 /// /// 显示PreView按钮 /// public Visibility ShowPreView { get { return showPreView; } set { SetProperty(ref showPreView, value); } } //平均对齐布局 private bool _isLayoutAvgAlign = false; public bool IsLayoutAvgAlign { get { return _isLayoutAvgAlign; } set { SetProperty(ref _isLayoutAvgAlign, value); } } //对齐布局 private bool _isLayoutAlign = false; public bool IsLayoutAlign { get { return _isLayoutAlign; } set { SetProperty(ref _isLayoutAlign, value); } } private bool _isMoreCheckedBtn = false; public bool IsMoreCheckedBtn { get { return _isMoreCheckedBtn; } set { SetProperty(ref _isMoreCheckedBtn, value); } } private bool _isAlignmentCheckedBtn = false; public bool IsAlignmentCheckedBtn { get { return _isAlignmentCheckedBtn; } set { SetProperty(ref _isAlignmentCheckedBtn, value); } } #endregion #region 变量 private CPDFViewer PDFViewer; public ViewContentViewModel viewContentViewModel; private IRegionManager regions; private string CurrentToolBtnType; /// /// 按钮和属性面板键值对 /// private Dictionary btnToProperty = new Dictionary(); private Visibility showPreView; #endregion #region Command变量 public DelegateCommand CheckedCommand { get; set; } public DelegateCommand CancleCheckedCommand { get; set; } public DelegateCommand ClickCommand { get; set; } public DelegateCommand AlignmentCheckedCommand { get; set; } public DelegateCommand AlignmentClosedMenuCommand { get; set; } public DelegateCommand ClosedMenuCommand { get; set; } public event EventHandler UncheckedToolsBtnEvent; #region 右键菜单 public DelegateCommand PropertyMenuCommand { get; set; } public DelegateCommand CrossPageMenuCommand { get; set; } public DelegateCommand JumpPosMenuCommand { get; set; } public DelegateCommand HideNameMenuCommand { get; set; } public DelegateCommand SearchFormMenuCommand { get; set; } public DelegateCommand PrintMenuCommand { get; set; } public DelegateCommand DefaultValueMenuCommand { get; set; } #endregion #endregion #region 初始化 public FormsToolContentViewModel(IRegionManager regionManager) { regions = regionManager; CheckedCommand = new DelegateCommand(CheckedEvent); CancleCheckedCommand = new DelegateCommand(CancleChecked); ClickCommand = new DelegateCommand(ClickEvent); AlignmentCheckedCommand = new DelegateCommand(AlignmentChecked); ClosedMenuCommand = new DelegateCommand(ClosedMenu); AlignmentClosedMenuCommand = new DelegateCommand(AlignmentClosedMenu); InitBtnToProperty(); //右键菜单 PropertyMenuCommand = new DelegateCommand(PropertyMenu); CrossPageMenuCommand = new DelegateCommand(CrossPageMenu); JumpPosMenuCommand = new DelegateCommand(JumpPosMenu); HideNameMenuCommand = new DelegateCommand(HideNameMenu); SearchFormMenuCommand = new DelegateCommand(SearchFormMenu); PrintMenuCommand = new DelegateCommand(PrintMenu); DefaultValueMenuCommand = new DelegateCommand(DefaultValueMenu); } #region Command实现 #region Command右键菜单 private void PropertyMenu(object obj) { } private void CrossPageMenu(object obj) { } private void JumpPosMenu(object obj) { } private void HideNameMenu(object obj) { } private void SearchFormMenu(object obj) { } private void PrintMenu(object obj) { } private void DefaultValueMenu(object obj) { } #endregion #endregion private void InitBtnToProperty() { btnToProperty.Add("EmptyForm", "EmptyFormProperty"); btnToProperty.Add("Text", "TextFieldProperty"); btnToProperty.Add("CheckBox", "CheckBoxProperty"); btnToProperty.Add("RadioButton", "RadioButtonProperty"); btnToProperty.Add("ListBox", "ListBoxProperty"); btnToProperty.Add("Combox", "ComboxProperty"); btnToProperty.Add("Button", "ButtonProperty"); btnToProperty.Add("Sign", "SignProperty"); } #endregion #region 属性面板 /// /// Form /// /// private void CheckedEvent(string type) { AddToPropertyPanel(type); } private void ClickEvent(string obj) { if (string.IsNullOrEmpty(obj) == false) { if (CurrentToolBtnType == obj) { PDFViewer.SetMouseMode(MouseModes.FormEditTool); PDFViewer.SetToolParam(new AnnotHandlerEventArgs()); UncheckedToolsBtnEvent?.Invoke(null, CurrentToolBtnType); CurrentToolBtnType = ""; } else { CurrentToolBtnType = obj; } } } private void AlignmentChecked(object obj) { if(obj != null) { var tag = (string)obj; switch(tag) { case "Left": PDFViewer.SetFormAligment(AlignModes.AlignLeft); break; case "Vertical": PDFViewer.SetFormAligment(AlignModes.AlignVerticalCenter); break; case "Right": PDFViewer.SetFormAligment(AlignModes.AlignRight); break; case "Top": PDFViewer.SetFormAligment(AlignModes.AlignTop); break; case "Horizontal": PDFViewer.SetFormAligment(AlignModes.AlignHorizonCenter); break; case "Lower": PDFViewer.SetFormAligment(AlignModes.AlignBottom); break; case "Distribute Vertical": PDFViewer.SetFormAligment(AlignModes.DistributeVertical); break; case "Distribute Horizontal": PDFViewer.SetFormAligment(AlignModes.DistributeHorizontal); break; } } } private void ClosedMenu() { IsMoreCheckedBtn = false; } private void AlignmentClosedMenu() { IsAlignmentCheckedBtn = false; } private void AddToPropertyPanel(string type, WidgetArgs widget = null, UpdateAttributeHelper e = null) { if (btnToProperty.ContainsKey(type)) { NavigationParameters parameters = new NavigationParameters(); parameters.Add(ParameterNames.PDFViewer, PDFViewer); parameters.Add("WidgetArgs", widget); parameters.Add(ParameterNames.AnnotEvent, e); System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => { //if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName)) regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters); })); ShowPropertyPanel(true); } } private void ShowPropertyPanel(bool show = true) { viewContentViewModel.IsPropertyOpen = show; } #endregion #region 工具按钮 private void CancleChecked() { PDFViewer.SetMouseMode(MouseModes.FormEditTool); PDFViewer.SetToolParam(new AnnotHandlerEventArgs()); } #endregion #region 表单内部触发的事件(比如选中表单行为等) private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e) { //点击表单,签名表单,触发事件 if (e != null) { } } // 选中时 private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e) { int count = 0; if (e != null) { count = e.AnnotItemsList.Count; var annotHandlerEventArgs = e.AnnotItemsList[0]; if (annotHandlerEventArgs as WidgetArgs != null) { var widgetArgs = annotHandlerEventArgs as WidgetArgs; //新建的表单之后; if (CurrentWidgetArgs != null && CurrentWidgetArgs.AnnotIndex == widgetArgs.AnnotIndex && CurrentWidgetArgs.PageIndex == widgetArgs.PageIndex) { GetSelectWidget(widgetArgs, e); UpdateLayoutAlign(count); return; } CurrentWidgetArgs = widgetArgs; GetSelectWidget(widgetArgs, e); UncheckedToolsBtnEvent?.Invoke(null, CurrentToolBtnType); CurrentToolBtnType = ""; } } UpdateLayoutAlign(count); } private void UpdateLayoutAlign(int count) { if (count == 0 || count == 1) { IsLayoutAlign = false; IsLayoutAvgAlign = false; } else if (count == 2) { IsLayoutAlign = true; IsLayoutAvgAlign = false; } else { IsLayoutAlign = true; IsLayoutAvgAlign = true; } } public WidgetArgs CurrentWidgetArgs; //新增 private void PDFViewer_AnnotEditHandler(object sender, List e) { if(e != null && e.Count > 0) { var widgeArgs = e[e.Count - 1].EditAnnotArgs as WidgetArgs; if (widgeArgs != null) { CurrentWidgetArgs = widgeArgs; } AnnotEditEvent editEvent = e[e.Count - 1]; //新增表单后,选中 if (editEvent.EditAction == ActionType.Add) { PDFViewer.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex); } } } //获取View选中的表单 private void GetSelectWidget(WidgetArgs widgetArgs, AnnotAttribEvent e) { if (widgetArgs == null) return; switch (widgetArgs.WidgeType) { case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_TEXTFIELD: AddToPropertyPanel("Text", widgetArgs, new UpdateAttributeHelper(e)); break; case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_CHECKBOX: AddToPropertyPanel("CheckBox", widgetArgs, new UpdateAttributeHelper(e)); break; case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_RADIOBUTTON: AddToPropertyPanel("RadioButton", widgetArgs, new UpdateAttributeHelper(e)); break; case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX: AddToPropertyPanel("ListBox", widgetArgs, new UpdateAttributeHelper(e)); break; case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX: AddToPropertyPanel("Combox", widgetArgs, new UpdateAttributeHelper(e)); break; case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON: AddToPropertyPanel("Button", widgetArgs, new UpdateAttributeHelper(e)); break; case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS: AddToPropertyPanel("Sign", widgetArgs, new UpdateAttributeHelper(e)); break; } } #endregion #region Navegation public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { UncheckedToolsBtnEvent?.Invoke(null, CurrentToolBtnType); CurrentToolBtnType = ""; if (PDFViewer != null) { PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander; PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler; PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler; PDFViewer.AnnotCommandHandler -= PDFViewer_WidgetCommandHandler; PDFViewer.AnnotCommandHandler -= PDFViewer_WidgetCommandHandler; } } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.ViewContentViewModel,out viewContentViewModel); navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); BindingFormHandler(); } private void BindingFormHandler() { if (PDFViewer != null) { PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander; PDFViewer.WidgetClickHander += PDFViewer_WidgetClickHander; PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler; PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler; PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler; PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler; PDFViewer.AnnotCommandHandler -= PDFViewer_WidgetCommandHandler; PDFViewer.AnnotCommandHandler += PDFViewer_WidgetCommandHandler; } } private void PDFViewer_WidgetCommandHandler(object sender, AnnotCommandArgs e) { if (e == null) return; switch (e.CommandType) { case CommandType.Context: if(e.CommandTarget == TargetType.WidgetView) { WidgetViewCommandArgs widgetCommand = e as WidgetViewCommandArgs; if(widgetCommand != null && widgetCommand.AnnotEvent != null ) { if (widgetCommand.AnnotEvent.AnnotItemsList.Count == 1) { e.PopupMenu = SelectedFormMenu(sender); } else if(widgetCommand.AnnotEvent.AnnotItemsList.Count > 1) { e.PopupMenu = MultiSelectedFormMenu(sender); } } } else { e.PopupMenu = BlankReaMenu(sender); } break; default: e.DoCommand(); break; } if (e.PopupMenu != null) { e.Handle = true; } } private void InitMenu(CustomPopMenu customMenu) { int index= 0; //属性 customMenu.SetMenuBinding(index, PropertyMenuCommand); customMenu.SetTagProperty(index, "Property"); index++; //复制 customMenu.SetMenuBinding(index, ApplicationCommands.Copy); customMenu.SetTagProperty(index, "Copy"); index++; //剪切 customMenu.SetMenuBinding(index, ApplicationCommands.Cut); customMenu.SetTagProperty(index, "Cut"); index++; //粘贴 customMenu.SetMenuBinding(index, ApplicationCommands.Paste); customMenu.SetTagProperty(index, "Paste"); index++; //删除 customMenu.SetMenuBinding(index, ApplicationCommands.Delete); customMenu.SetTagProperty(index, "Delete"); index++; //创建多个副本 customMenu.SetMenuBinding(index, ApplicationCommands.Delete); customMenu.SetTagProperty(index, "CreateCopy"); index++; //跨页复制 customMenu.SetMenuBinding(index, CrossPageMenuCommand); customMenu.SetTagProperty(index, "CrossPage"); index++; //显示跳位编号 customMenu.SetMenuBinding(index, JumpPosMenuCommand); customMenu.SetTagProperty(index, "JumpPos"); index++; //隐藏名称 customMenu.SetMenuBinding(index, HideNameMenuCommand); customMenu.SetTagProperty(index, "HideName"); index++; //查找 customMenu.SetMenuBinding(index, SearchFormMenuCommand); customMenu.SetTagProperty(index, "SearchForm"); index++; //打印 customMenu.SetMenuBinding(index, PrintMenuCommand); customMenu.SetTagProperty(index, "print"); index++; //设置当前属性为默认值 customMenu.SetMenuBinding(index, DefaultValueMenuCommand); customMenu.SetTagProperty(index, "DefaultValue"); index++; customMenu.AllMenuVisibility(true); } //点击空白处 private ContextMenu BlankReaMenu(object sender) { var popMenu = App.Current.FindResource("FormContentMenu") as ContextMenu; CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender); InitMenu(customMenu); customMenu.SetVisibilityProperty("Property", false); customMenu.SetVisibilityProperty("Copy", false); customMenu.SetVisibilityProperty("Cut", false); customMenu.SetVisibilityProperty("Delete", false); customMenu.SetVisibilityProperty("CreateCopy", false); customMenu.SetVisibilityProperty("CrossPage", false); customMenu.SetVisibilityProperty("DefaultValue", false); return popMenu; } //选中一个 private ContextMenu SelectedFormMenu(object sender) { var popMenu = App.Current.FindResource("FormContentMenu") as ContextMenu; CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender); InitMenu(customMenu); customMenu.SetVisibilityProperty("SearchForm", false); customMenu.SetVisibilityProperty("print", false); return popMenu; } //多选 private ContextMenu MultiSelectedFormMenu(object sender) { var popMenu = App.Current.FindResource("FormContentMenu") as ContextMenu; CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender); InitMenu(customMenu); customMenu.SetVisibilityProperty("CreateCopy", false); customMenu.SetVisibilityProperty("DefaultValue", false); customMenu.SetVisibilityProperty("SearchForm", false); customMenu.SetVisibilityProperty("print", false); return popMenu; } #endregion } }