FormsToolContentViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Office.Helper;
  5. using PDF_Office.Model;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Input;
  17. namespace PDF_Office.ViewModels.Form
  18. {
  19. public class FormsToolContentViewModel : BindableBase, INavigationAware
  20. {
  21. #region 属性
  22. /// <summary>
  23. /// 显示PreView按钮
  24. /// </summary>
  25. public Visibility ShowPreView
  26. {
  27. get { return showPreView; }
  28. set
  29. {
  30. SetProperty(ref showPreView, value);
  31. }
  32. }
  33. //平均对齐布局
  34. private bool _isLayoutAvgAlign = false;
  35. public bool IsLayoutAvgAlign { get { return _isLayoutAvgAlign; } set { SetProperty(ref _isLayoutAvgAlign, value); } }
  36. //对齐布局
  37. private bool _isLayoutAlign = false;
  38. public bool IsLayoutAlign { get { return _isLayoutAlign; } set { SetProperty(ref _isLayoutAlign, value); } }
  39. private bool _isMoreCheckedBtn = false;
  40. public bool IsMoreCheckedBtn { get { return _isMoreCheckedBtn; } set { SetProperty(ref _isMoreCheckedBtn, value); } }
  41. private bool _isAlignmentCheckedBtn = false;
  42. public bool IsAlignmentCheckedBtn { get { return _isAlignmentCheckedBtn; } set { SetProperty(ref _isAlignmentCheckedBtn, value); } }
  43. #endregion
  44. #region 变量
  45. private CPDFViewer PDFViewer;
  46. public ViewContentViewModel viewContentViewModel;
  47. private IRegionManager regions;
  48. private string CurrentToolBtnType;
  49. /// <summary>
  50. /// 按钮和属性面板键值对
  51. /// </summary>
  52. private Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
  53. private Visibility showPreView;
  54. #endregion
  55. #region Command变量
  56. public DelegateCommand<string> CheckedCommand { get; set; }
  57. public DelegateCommand CancleCheckedCommand { get; set; }
  58. public DelegateCommand<string> ClickCommand { get; set; }
  59. public DelegateCommand<object> AlignmentCheckedCommand { get; set; }
  60. public DelegateCommand AlignmentClosedMenuCommand { get; set; }
  61. public DelegateCommand ClosedMenuCommand { get; set; }
  62. public event EventHandler<string> UncheckedToolsBtnEvent;
  63. #region 右键菜单
  64. public DelegateCommand<object> PropertyMenuCommand { get; set; }
  65. public DelegateCommand<object> CrossPageMenuCommand { get; set; }
  66. public DelegateCommand<object> JumpPosMenuCommand { get; set; }
  67. public DelegateCommand<object> HideNameMenuCommand { get; set; }
  68. public DelegateCommand<object> SearchFormMenuCommand { get; set; }
  69. public DelegateCommand<object> PrintMenuCommand { get; set; }
  70. public DelegateCommand<object> DefaultValueMenuCommand { get; set; }
  71. #endregion
  72. #endregion
  73. #region 初始化
  74. public FormsToolContentViewModel(IRegionManager regionManager)
  75. {
  76. regions = regionManager;
  77. CheckedCommand = new DelegateCommand<string>(CheckedEvent);
  78. CancleCheckedCommand = new DelegateCommand(CancleChecked);
  79. ClickCommand = new DelegateCommand<string>(ClickEvent);
  80. AlignmentCheckedCommand = new DelegateCommand<object>(AlignmentChecked);
  81. ClosedMenuCommand = new DelegateCommand(ClosedMenu);
  82. AlignmentClosedMenuCommand = new DelegateCommand(AlignmentClosedMenu);
  83. InitBtnToProperty();
  84. //右键菜单
  85. PropertyMenuCommand = new DelegateCommand<object>(PropertyMenu);
  86. CrossPageMenuCommand = new DelegateCommand<object>(CrossPageMenu);
  87. JumpPosMenuCommand = new DelegateCommand<object>(JumpPosMenu);
  88. HideNameMenuCommand = new DelegateCommand<object>(HideNameMenu);
  89. SearchFormMenuCommand = new DelegateCommand<object>(SearchFormMenu);
  90. PrintMenuCommand = new DelegateCommand<object>(PrintMenu);
  91. DefaultValueMenuCommand = new DelegateCommand<object>(DefaultValueMenu);
  92. }
  93. #region Command实现
  94. #region Command右键菜单
  95. private void PropertyMenu(object obj)
  96. {
  97. }
  98. private void CrossPageMenu(object obj)
  99. {
  100. }
  101. private void JumpPosMenu(object obj)
  102. {
  103. }
  104. private void HideNameMenu(object obj)
  105. {
  106. }
  107. private void SearchFormMenu(object obj)
  108. {
  109. }
  110. private void PrintMenu(object obj)
  111. {
  112. }
  113. private void DefaultValueMenu(object obj)
  114. {
  115. }
  116. #endregion
  117. #endregion
  118. private void InitBtnToProperty()
  119. {
  120. btnToProperty.Add("EmptyForm", "EmptyFormProperty");
  121. btnToProperty.Add("Text", "TextFieldProperty");
  122. btnToProperty.Add("CheckBox", "CheckBoxProperty");
  123. btnToProperty.Add("RadioButton", "RadioButtonProperty");
  124. btnToProperty.Add("ListBox", "ListBoxProperty");
  125. btnToProperty.Add("Combox", "ComboxProperty");
  126. btnToProperty.Add("Button", "ButtonProperty");
  127. btnToProperty.Add("Sign", "SignProperty");
  128. }
  129. #endregion
  130. #region 属性面板
  131. /// <summary>
  132. /// Form
  133. /// </summary>
  134. /// <param name="type"></param>
  135. private void CheckedEvent(string type)
  136. {
  137. AddToPropertyPanel(type);
  138. }
  139. private void ClickEvent(string obj)
  140. {
  141. if (string.IsNullOrEmpty(obj) == false)
  142. {
  143. if (CurrentToolBtnType == obj)
  144. {
  145. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  146. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  147. UncheckedToolsBtnEvent?.Invoke(null, CurrentToolBtnType);
  148. CurrentToolBtnType = "";
  149. }
  150. else
  151. {
  152. CurrentToolBtnType = obj;
  153. }
  154. }
  155. }
  156. private void AlignmentChecked(object obj)
  157. {
  158. if(obj != null)
  159. {
  160. var tag = (string)obj;
  161. switch(tag)
  162. {
  163. case "Left":
  164. PDFViewer.SetFormAligment(AlignModes.AlignLeft);
  165. break;
  166. case "Vertical":
  167. PDFViewer.SetFormAligment(AlignModes.AlignVerticalCenter);
  168. break;
  169. case "Right":
  170. PDFViewer.SetFormAligment(AlignModes.AlignRight);
  171. break;
  172. case "Top":
  173. PDFViewer.SetFormAligment(AlignModes.AlignTop);
  174. break;
  175. case "Horizontal":
  176. PDFViewer.SetFormAligment(AlignModes.AlignHorizonCenter);
  177. break;
  178. case "Lower":
  179. PDFViewer.SetFormAligment(AlignModes.AlignBottom);
  180. break;
  181. case "Distribute Vertical":
  182. PDFViewer.SetFormAligment(AlignModes.DistributeVertical);
  183. break;
  184. case "Distribute Horizontal":
  185. PDFViewer.SetFormAligment(AlignModes.DistributeHorizontal);
  186. break;
  187. }
  188. }
  189. }
  190. private void ClosedMenu()
  191. {
  192. IsMoreCheckedBtn = false;
  193. }
  194. private void AlignmentClosedMenu()
  195. {
  196. IsAlignmentCheckedBtn = false;
  197. }
  198. private void AddToPropertyPanel(string type, WidgetArgs widget = null, UpdateAttributeHelper e = null)
  199. {
  200. if (btnToProperty.ContainsKey(type))
  201. {
  202. NavigationParameters parameters = new NavigationParameters();
  203. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  204. parameters.Add("WidgetArgs", widget);
  205. parameters.Add(ParameterNames.AnnotEvent, e);
  206. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  207. {
  208. //if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName))
  209. regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters);
  210. }));
  211. ShowPropertyPanel(true);
  212. }
  213. }
  214. private void ShowPropertyPanel(bool show = true)
  215. {
  216. viewContentViewModel.IsPropertyOpen = show;
  217. }
  218. #endregion
  219. #region 工具按钮
  220. private void CancleChecked()
  221. {
  222. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  223. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  224. }
  225. #endregion
  226. #region 表单内部触发的事件(比如选中表单行为等)
  227. private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
  228. {
  229. //点击表单,签名表单,触发事件
  230. if (e != null)
  231. {
  232. }
  233. }
  234. // 选中时
  235. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  236. {
  237. int count = 0;
  238. if (e != null)
  239. {
  240. count = e.AnnotItemsList.Count;
  241. var annotHandlerEventArgs = e.AnnotItemsList[0];
  242. if (annotHandlerEventArgs as WidgetArgs != null)
  243. {
  244. var widgetArgs = annotHandlerEventArgs as WidgetArgs;
  245. //新建的表单之后;
  246. if (CurrentWidgetArgs != null &&
  247. CurrentWidgetArgs.AnnotIndex == widgetArgs.AnnotIndex &&
  248. CurrentWidgetArgs.PageIndex == widgetArgs.PageIndex)
  249. {
  250. GetSelectWidget(widgetArgs, e);
  251. UpdateLayoutAlign(count);
  252. return;
  253. }
  254. CurrentWidgetArgs = widgetArgs;
  255. GetSelectWidget(widgetArgs, e);
  256. UncheckedToolsBtnEvent?.Invoke(null, CurrentToolBtnType);
  257. CurrentToolBtnType = "";
  258. }
  259. }
  260. UpdateLayoutAlign(count);
  261. }
  262. private void UpdateLayoutAlign(int count)
  263. {
  264. if (count == 0 || count == 1)
  265. {
  266. IsLayoutAlign = false;
  267. IsLayoutAvgAlign = false;
  268. }
  269. else if (count == 2)
  270. {
  271. IsLayoutAlign = true;
  272. IsLayoutAvgAlign = false;
  273. }
  274. else
  275. {
  276. IsLayoutAlign = true;
  277. IsLayoutAvgAlign = true;
  278. }
  279. }
  280. public WidgetArgs CurrentWidgetArgs;
  281. //新增
  282. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  283. {
  284. if(e != null && e.Count > 0)
  285. {
  286. var widgeArgs = e[e.Count - 1].EditAnnotArgs as WidgetArgs;
  287. if (widgeArgs != null)
  288. {
  289. CurrentWidgetArgs = widgeArgs;
  290. }
  291. AnnotEditEvent editEvent = e[e.Count - 1];
  292. //新增表单后,选中
  293. if (editEvent.EditAction == ActionType.Add)
  294. {
  295. PDFViewer.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex);
  296. }
  297. }
  298. }
  299. //获取View选中的表单
  300. private void GetSelectWidget(WidgetArgs widgetArgs, AnnotAttribEvent e)
  301. {
  302. if (widgetArgs == null) return;
  303. switch (widgetArgs.WidgeType)
  304. {
  305. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_TEXTFIELD:
  306. AddToPropertyPanel("Text", widgetArgs, new UpdateAttributeHelper(e));
  307. break;
  308. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_CHECKBOX:
  309. AddToPropertyPanel("CheckBox", widgetArgs, new UpdateAttributeHelper(e));
  310. break;
  311. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
  312. AddToPropertyPanel("RadioButton", widgetArgs, new UpdateAttributeHelper(e));
  313. break;
  314. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
  315. AddToPropertyPanel("ListBox", widgetArgs, new UpdateAttributeHelper(e));
  316. break;
  317. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
  318. AddToPropertyPanel("Combox", widgetArgs, new UpdateAttributeHelper(e));
  319. break;
  320. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  321. AddToPropertyPanel("Button", widgetArgs, new UpdateAttributeHelper(e));
  322. break;
  323. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
  324. AddToPropertyPanel("Sign", widgetArgs, new UpdateAttributeHelper(e));
  325. break;
  326. }
  327. }
  328. #endregion
  329. #region Navegation
  330. public bool IsNavigationTarget(NavigationContext navigationContext)
  331. {
  332. return true;
  333. }
  334. public void OnNavigatedFrom(NavigationContext navigationContext)
  335. {
  336. UncheckedToolsBtnEvent?.Invoke(null, CurrentToolBtnType);
  337. CurrentToolBtnType = "";
  338. if (PDFViewer != null)
  339. {
  340. PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
  341. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  342. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  343. PDFViewer.AnnotCommandHandler -= PDFViewer_WidgetCommandHandler;
  344. PDFViewer.AnnotCommandHandler -= PDFViewer_WidgetCommandHandler;
  345. }
  346. }
  347. public void OnNavigatedTo(NavigationContext navigationContext)
  348. {
  349. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel,out viewContentViewModel);
  350. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  351. BindingFormHandler();
  352. }
  353. private void BindingFormHandler()
  354. {
  355. if (PDFViewer != null)
  356. {
  357. PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
  358. PDFViewer.WidgetClickHander += PDFViewer_WidgetClickHander;
  359. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  360. PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  361. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  362. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  363. PDFViewer.AnnotCommandHandler -= PDFViewer_WidgetCommandHandler;
  364. PDFViewer.AnnotCommandHandler += PDFViewer_WidgetCommandHandler;
  365. }
  366. }
  367. private void PDFViewer_WidgetCommandHandler(object sender, AnnotCommandArgs e)
  368. {
  369. if (e == null)
  370. return;
  371. switch (e.CommandType)
  372. {
  373. case CommandType.Context:
  374. if(e.CommandTarget == TargetType.WidgetView)
  375. {
  376. WidgetViewCommandArgs widgetCommand = e as WidgetViewCommandArgs;
  377. if(widgetCommand != null && widgetCommand.AnnotEvent != null )
  378. {
  379. if (widgetCommand.AnnotEvent.AnnotItemsList.Count == 1)
  380. {
  381. e.PopupMenu = SelectedFormMenu(sender);
  382. }
  383. else if(widgetCommand.AnnotEvent.AnnotItemsList.Count > 1)
  384. {
  385. e.PopupMenu = MultiSelectedFormMenu(sender);
  386. }
  387. }
  388. }
  389. else
  390. {
  391. e.PopupMenu = BlankReaMenu(sender);
  392. }
  393. break;
  394. default:
  395. e.DoCommand();
  396. break;
  397. }
  398. if (e.PopupMenu != null)
  399. {
  400. e.Handle = true;
  401. }
  402. }
  403. private void InitMenu(CustomPopMenu customMenu)
  404. {
  405. int index= 0;
  406. //属性
  407. customMenu.SetMenuBinding(index, PropertyMenuCommand);
  408. customMenu.SetTagProperty(index, "Property");
  409. index++;
  410. //复制
  411. customMenu.SetMenuBinding(index, ApplicationCommands.Copy);
  412. customMenu.SetTagProperty(index, "Copy");
  413. index++;
  414. //剪切
  415. customMenu.SetMenuBinding(index, ApplicationCommands.Cut);
  416. customMenu.SetTagProperty(index, "Cut");
  417. index++;
  418. //粘贴
  419. customMenu.SetMenuBinding(index, ApplicationCommands.Paste);
  420. customMenu.SetTagProperty(index, "Paste");
  421. index++;
  422. //删除
  423. customMenu.SetMenuBinding(index, ApplicationCommands.Delete);
  424. customMenu.SetTagProperty(index, "Delete");
  425. index++;
  426. //创建多个副本
  427. customMenu.SetMenuBinding(index, ApplicationCommands.Delete);
  428. customMenu.SetTagProperty(index, "CreateCopy");
  429. index++;
  430. //跨页复制
  431. customMenu.SetMenuBinding(index, CrossPageMenuCommand);
  432. customMenu.SetTagProperty(index, "CrossPage");
  433. index++;
  434. //显示跳位编号
  435. customMenu.SetMenuBinding(index, JumpPosMenuCommand);
  436. customMenu.SetTagProperty(index, "JumpPos");
  437. index++;
  438. //隐藏名称
  439. customMenu.SetMenuBinding(index, HideNameMenuCommand);
  440. customMenu.SetTagProperty(index, "HideName");
  441. index++;
  442. //查找
  443. customMenu.SetMenuBinding(index, SearchFormMenuCommand);
  444. customMenu.SetTagProperty(index, "SearchForm");
  445. index++;
  446. //打印
  447. customMenu.SetMenuBinding(index, PrintMenuCommand);
  448. customMenu.SetTagProperty(index, "print");
  449. index++;
  450. //设置当前属性为默认值
  451. customMenu.SetMenuBinding(index, DefaultValueMenuCommand);
  452. customMenu.SetTagProperty(index, "DefaultValue");
  453. index++;
  454. customMenu.AllMenuVisibility(true);
  455. }
  456. //点击空白处
  457. private ContextMenu BlankReaMenu(object sender)
  458. {
  459. var popMenu = App.Current.FindResource("FormContentMenu") as ContextMenu;
  460. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  461. InitMenu(customMenu);
  462. customMenu.SetVisibilityProperty("Property", false);
  463. customMenu.SetVisibilityProperty("Copy", false);
  464. customMenu.SetVisibilityProperty("Cut", false);
  465. customMenu.SetVisibilityProperty("Delete", false);
  466. customMenu.SetVisibilityProperty("CreateCopy", false);
  467. customMenu.SetVisibilityProperty("CrossPage", false);
  468. customMenu.SetVisibilityProperty("DefaultValue", false);
  469. return popMenu;
  470. }
  471. //选中一个
  472. private ContextMenu SelectedFormMenu(object sender)
  473. {
  474. var popMenu = App.Current.FindResource("FormContentMenu") as ContextMenu;
  475. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  476. InitMenu(customMenu);
  477. customMenu.SetVisibilityProperty("SearchForm", false);
  478. customMenu.SetVisibilityProperty("print", false);
  479. return popMenu;
  480. }
  481. //多选
  482. private ContextMenu MultiSelectedFormMenu(object sender)
  483. {
  484. var popMenu = App.Current.FindResource("FormContentMenu") as ContextMenu;
  485. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  486. InitMenu(customMenu);
  487. customMenu.SetVisibilityProperty("CreateCopy", false);
  488. customMenu.SetVisibilityProperty("DefaultValue", false);
  489. customMenu.SetVisibilityProperty("SearchForm", false);
  490. customMenu.SetVisibilityProperty("print", false);
  491. return popMenu;
  492. }
  493. #endregion
  494. }
  495. }