FormsToolContentViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. namespace PDF_Office.ViewModels.Form
  16. {
  17. public class FormsToolContentViewModel : BindableBase, INavigationAware
  18. {
  19. #region 属性
  20. /// <summary>
  21. /// 显示PreView按钮
  22. /// </summary>
  23. public Visibility ShowPreView
  24. {
  25. get { return showPreView; }
  26. set
  27. {
  28. SetProperty(ref showPreView, value);
  29. }
  30. }
  31. //平均对齐布局
  32. private bool _isLayoutAvgAlign = false;
  33. public bool IsLayoutAvgAlign { get { return _isLayoutAvgAlign; } set { SetProperty(ref _isLayoutAvgAlign, value); } }
  34. //对齐布局
  35. private bool _isLayoutAlign = false;
  36. public bool IsLayoutAlign { get { return _isLayoutAlign; } set { SetProperty(ref _isLayoutAlign, value); } }
  37. private bool _isMoreCheckedBtn = false;
  38. public bool IsMoreCheckedBtn { get { return _isMoreCheckedBtn; } set { SetProperty(ref _isMoreCheckedBtn, value); } }
  39. private bool _isAlignmentCheckedBtn = false;
  40. public bool IsAlignmentCheckedBtn { get { return _isAlignmentCheckedBtn; } set { SetProperty(ref _isAlignmentCheckedBtn, value); } }
  41. #endregion
  42. #region 变量
  43. private CPDFViewer PDFViewer;
  44. public ViewContentViewModel viewContentViewModel;
  45. private IRegionManager regions;
  46. private string CurrentToolBtnType;
  47. /// <summary>
  48. /// 按钮和属性面板键值对
  49. /// </summary>
  50. private Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
  51. private Visibility showPreView;
  52. #endregion
  53. #region Command变量
  54. public DelegateCommand<string> CheckedCommand { get; set; }
  55. public DelegateCommand CancleCheckedCommand { get; set; }
  56. public DelegateCommand<string> ClickCommand { get; set; }
  57. public DelegateCommand<object> AlignmentCheckedCommand { get; set; }
  58. public DelegateCommand AlignmentClosedMenuCommand { get; set; }
  59. public DelegateCommand ClosedMenuCommand { get; set; }
  60. public event EventHandler<string> UncheckedToolsBtnEvent;
  61. #endregion
  62. #region 初始化
  63. public FormsToolContentViewModel(IRegionManager regionManager)
  64. {
  65. regions = regionManager;
  66. CheckedCommand = new DelegateCommand<string>(CheckedEvent);
  67. CancleCheckedCommand = new DelegateCommand(CancleChecked);
  68. ClickCommand = new DelegateCommand<string>(ClickEvent);
  69. AlignmentCheckedCommand = new DelegateCommand<object>(AlignmentChecked);
  70. ClosedMenuCommand = new DelegateCommand(ClosedMenu);
  71. AlignmentClosedMenuCommand = new DelegateCommand(AlignmentClosedMenu);
  72. InitBtnToProperty();
  73. }
  74. private void InitBtnToProperty()
  75. {
  76. btnToProperty.Add("EmptyForm", "EmptyFormProperty");
  77. btnToProperty.Add("Text", "TextFieldProperty");
  78. btnToProperty.Add("CheckBox", "CheckBoxProperty");
  79. btnToProperty.Add("RadioButton", "RadioButtonProperty");
  80. btnToProperty.Add("ListBox", "ListBoxProperty");
  81. btnToProperty.Add("Combox", "ComboxProperty");
  82. btnToProperty.Add("Button", "ButtonProperty");
  83. btnToProperty.Add("Sign", "SignProperty");
  84. }
  85. #endregion
  86. #region 属性面板
  87. /// <summary>
  88. /// Form
  89. /// </summary>
  90. /// <param name="type"></param>
  91. private void CheckedEvent(string type)
  92. {
  93. AddToPropertyPanel(type);
  94. }
  95. private void ClickEvent(string obj)
  96. {
  97. if (string.IsNullOrEmpty(obj) == false)
  98. {
  99. if (CurrentToolBtnType == obj)
  100. {
  101. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  102. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  103. UncheckedToolsBtnEvent?.Invoke(null, CurrentToolBtnType);
  104. CurrentToolBtnType = "";
  105. }
  106. else
  107. {
  108. CurrentToolBtnType = obj;
  109. }
  110. }
  111. }
  112. private void AlignmentChecked(object obj)
  113. {
  114. if(obj != null)
  115. {
  116. var tag = (string)obj;
  117. switch(tag)
  118. {
  119. case "Left":
  120. PDFViewer.SetFormAligment(AlignModes.AlignLeft);
  121. break;
  122. case "Vertical":
  123. PDFViewer.SetFormAligment(AlignModes.AlignVerticalCenter);
  124. break;
  125. case "Right":
  126. PDFViewer.SetFormAligment(AlignModes.AlignRight);
  127. break;
  128. case "Top":
  129. PDFViewer.SetFormAligment(AlignModes.AlignTop);
  130. break;
  131. case "Horizontal":
  132. PDFViewer.SetFormAligment(AlignModes.AlignHorizonCenter);
  133. break;
  134. case "Lower":
  135. PDFViewer.SetFormAligment(AlignModes.AlignBottom);
  136. break;
  137. case "Distribute Vertical":
  138. PDFViewer.SetFormAligment(AlignModes.DistributeVertical);
  139. break;
  140. case "Distribute Horizontal":
  141. PDFViewer.SetFormAligment(AlignModes.DistributeHorizontal);
  142. break;
  143. }
  144. }
  145. }
  146. private void ClosedMenu()
  147. {
  148. IsMoreCheckedBtn = false;
  149. }
  150. private void AlignmentClosedMenu()
  151. {
  152. IsAlignmentCheckedBtn = false;
  153. }
  154. private void AddToPropertyPanel(string type, WidgetArgs widget = null, UpdateAttributeHelper e = null)
  155. {
  156. if (btnToProperty.ContainsKey(type))
  157. {
  158. NavigationParameters parameters = new NavigationParameters();
  159. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  160. parameters.Add("WidgetArgs", widget);
  161. parameters.Add(ParameterNames.AnnotEvent, e);
  162. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  163. {
  164. //if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName))
  165. regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters);
  166. }));
  167. ShowPropertyPanel(true);
  168. }
  169. }
  170. private void ShowPropertyPanel(bool show = true)
  171. {
  172. viewContentViewModel.IsPropertyOpen = show;
  173. }
  174. #endregion
  175. #region 工具按钮
  176. private void CancleChecked()
  177. {
  178. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  179. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  180. }
  181. #endregion
  182. #region 表单内部触发的事件(比如选中表单行为等)
  183. private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
  184. {
  185. //点击表单,签名表单,触发事件
  186. if (e != null)
  187. {
  188. }
  189. }
  190. // 选中时
  191. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  192. {
  193. int count = 0;
  194. if (e != null)
  195. {
  196. count = e.AnnotItemsList.Count;
  197. var annotHandlerEventArgs = e.AnnotItemsList[0];
  198. if (annotHandlerEventArgs as WidgetArgs != null)
  199. {
  200. var widgetArgs = annotHandlerEventArgs as WidgetArgs;
  201. //新建的表单之后;
  202. if (CurrentWidgetArgs != null &&
  203. CurrentWidgetArgs.AnnotIndex == widgetArgs.AnnotIndex &&
  204. CurrentWidgetArgs.PageIndex == widgetArgs.PageIndex)
  205. {
  206. GetSelectWidget(widgetArgs, e);
  207. UpdateLayoutAlign(count);
  208. return;
  209. }
  210. CurrentWidgetArgs = widgetArgs;
  211. GetSelectWidget(widgetArgs, e);
  212. UncheckedToolsBtnEvent?.Invoke(null, CurrentToolBtnType);
  213. CurrentToolBtnType = "";
  214. }
  215. }
  216. UpdateLayoutAlign(count);
  217. }
  218. private void UpdateLayoutAlign(int count)
  219. {
  220. if (count == 0 || count == 1)
  221. {
  222. IsLayoutAlign = false;
  223. IsLayoutAvgAlign = false;
  224. }
  225. else if (count == 2)
  226. {
  227. IsLayoutAlign = true;
  228. IsLayoutAvgAlign = false;
  229. }
  230. else
  231. {
  232. IsLayoutAlign = true;
  233. IsLayoutAvgAlign = true;
  234. }
  235. }
  236. public WidgetArgs CurrentWidgetArgs;
  237. //新增
  238. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  239. {
  240. if(e != null && e.Count > 0)
  241. {
  242. var widgeArgs = e[e.Count - 1].EditAnnotArgs as WidgetArgs;
  243. if (widgeArgs != null)
  244. {
  245. CurrentWidgetArgs = widgeArgs;
  246. }
  247. AnnotEditEvent editEvent = e[e.Count - 1];
  248. //新增表单后,选中
  249. if (editEvent.EditAction == ActionType.Add)
  250. {
  251. PDFViewer.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex);
  252. }
  253. }
  254. }
  255. //获取View选中的表单
  256. private void GetSelectWidget(WidgetArgs widgetArgs, AnnotAttribEvent e)
  257. {
  258. if (widgetArgs == null) return;
  259. switch (widgetArgs.WidgeType)
  260. {
  261. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_TEXTFIELD:
  262. AddToPropertyPanel("Text", widgetArgs, new UpdateAttributeHelper(e));
  263. break;
  264. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_CHECKBOX:
  265. AddToPropertyPanel("CheckBox", widgetArgs, new UpdateAttributeHelper(e));
  266. break;
  267. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
  268. AddToPropertyPanel("RadioButton", widgetArgs, new UpdateAttributeHelper(e));
  269. break;
  270. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
  271. AddToPropertyPanel("ListBox", widgetArgs, new UpdateAttributeHelper(e));
  272. break;
  273. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
  274. AddToPropertyPanel("Combox", widgetArgs, new UpdateAttributeHelper(e));
  275. break;
  276. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  277. AddToPropertyPanel("Button", widgetArgs, new UpdateAttributeHelper(e));
  278. break;
  279. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
  280. AddToPropertyPanel("Sign", widgetArgs, new UpdateAttributeHelper(e));
  281. break;
  282. }
  283. }
  284. #endregion
  285. #region Navegation
  286. public bool IsNavigationTarget(NavigationContext navigationContext)
  287. {
  288. return true;
  289. }
  290. public void OnNavigatedFrom(NavigationContext navigationContext)
  291. {
  292. UncheckedToolsBtnEvent?.Invoke(null, CurrentToolBtnType);
  293. CurrentToolBtnType = "";
  294. }
  295. public void OnNavigatedTo(NavigationContext navigationContext)
  296. {
  297. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel,out viewContentViewModel);
  298. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  299. BindingFormHandler();
  300. }
  301. private void BindingFormHandler()
  302. {
  303. if (PDFViewer != null)
  304. {
  305. PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
  306. PDFViewer.WidgetClickHander += PDFViewer_WidgetClickHander;
  307. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  308. PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  309. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  310. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  311. }
  312. }
  313. #endregion
  314. }
  315. }