FormsToolContentViewModel.cs 12 KB

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