123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584 |
- using ComPDFKitViewer;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Master.Helper;
- using PDF_Master.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_Master.ViewModels.Form
- {
- public class FormsToolContentViewModel : BindableBase, INavigationAware
- {
- #region 属性
- /// <summary>
- /// 显示PreView按钮
- /// </summary>
- 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;
-
- /// <summary>
- /// 按钮和属性面板键值对
- /// </summary>
- private Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
- private Visibility showPreView;
- #endregion
- #region Command变量
- public DelegateCommand<string> CheckedCommand { get; set; }
- public DelegateCommand CancleCheckedCommand { get; set; }
- public DelegateCommand<string> ClickCommand { get; set; }
- public DelegateCommand<object> AlignmentCheckedCommand { get; set; }
- public DelegateCommand AlignmentClosedMenuCommand { get; set; }
- public DelegateCommand ClosedMenuCommand { get; set; }
- public event EventHandler<string> UncheckedToolsBtnEvent;
- #region 右键菜单
- public DelegateCommand<object> PropertyMenuCommand { get; set; }
- public DelegateCommand<object> CrossPageMenuCommand { get; set; }
- public DelegateCommand<object> JumpPosMenuCommand { get; set; }
- public DelegateCommand<object> HideNameMenuCommand { get; set; }
- public DelegateCommand<object> SearchFormMenuCommand { get; set; }
- public DelegateCommand<object> PrintMenuCommand { get; set; }
- public DelegateCommand<object> DefaultValueMenuCommand { get; set; }
-
- #endregion
- #endregion
- #region 初始化
- public FormsToolContentViewModel(IRegionManager regionManager)
- {
- regions = regionManager;
- CheckedCommand = new DelegateCommand<string>(CheckedEvent);
- CancleCheckedCommand = new DelegateCommand(CancleChecked);
- ClickCommand = new DelegateCommand<string>(ClickEvent);
- AlignmentCheckedCommand = new DelegateCommand<object>(AlignmentChecked);
- ClosedMenuCommand = new DelegateCommand(ClosedMenu);
- AlignmentClosedMenuCommand = new DelegateCommand(AlignmentClosedMenu);
- InitBtnToProperty();
- //右键菜单
- PropertyMenuCommand = new DelegateCommand<object>(PropertyMenu);
- CrossPageMenuCommand = new DelegateCommand<object>(CrossPageMenu);
- JumpPosMenuCommand = new DelegateCommand<object>(JumpPosMenu);
- HideNameMenuCommand = new DelegateCommand<object>(HideNameMenu);
- SearchFormMenuCommand = new DelegateCommand<object>(SearchFormMenu);
- PrintMenuCommand = new DelegateCommand<object>(PrintMenu);
- DefaultValueMenuCommand = new DelegateCommand<object>(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 属性面板
- /// <summary>
- /// Form
- /// </summary>
- /// <param name="type"></param>
- 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<AnnotEditEvent> 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<ViewContentViewModel>(ParameterNames.ViewContentViewModel,out viewContentViewModel);
- navigationContext.Parameters.TryGetValue<CPDFViewer>(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
- }
- }
|