FormControl.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Input;
  9. using ComPDFKitViewer;
  10. using ComPDFKit.Controls.Helper;
  11. using ComPDFKit.PDFAnnotation;
  12. using ComPDFKitViewer.Widget;
  13. using ComPDFKit.PDFAnnotation.Form;
  14. using ComPDFKit.Tool.Help;
  15. using ComPDFKit.Tool;
  16. namespace ComPDFKit.Controls.PDFControl
  17. {
  18. public partial class FormControl : UserControl, INotifyPropertyChanged
  19. {
  20. #region Property
  21. public PDFViewControl PdfViewControl;
  22. public FromPropertyControl FromPropertyControl = new FromPropertyControl();
  23. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  24. public event PropertyChangedEventHandler PropertyChanged;
  25. private CPDFDisplaySettingsControl displaySettingsControl;
  26. private PanelState panelState = PanelState.GetInstance();
  27. public bool CanUndo
  28. {
  29. get
  30. {
  31. if (PdfViewControl != null && PdfViewControl.PDFViewTool.GetCPDFViewer() != null)
  32. {
  33. return PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo;
  34. }
  35. return false;
  36. }
  37. }
  38. public bool CanRedo
  39. {
  40. get
  41. {
  42. if (PdfViewControl != null && PdfViewControl.PDFViewTool.GetCPDFViewer() != null)
  43. {
  44. return PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo;
  45. }
  46. return false;
  47. }
  48. }
  49. public bool CanSave
  50. {
  51. get
  52. {
  53. if (PdfViewControl != null && PdfViewControl.PDFViewTool.GetCPDFViewer() != null)
  54. {
  55. if (PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo ||
  56. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo)
  57. {
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. }
  64. public event EventHandler<bool> OnCanSaveChanged;
  65. public event EventHandler OnAnnotEditHandler;
  66. #endregion
  67. public FormControl()
  68. {
  69. InitializeComponent();
  70. DataContext = this;
  71. }
  72. #region Load Unload custom control
  73. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  74. {
  75. PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler;
  76. PdfViewControl.MouseRightButtonDownHandler += PdfViewControl_MouseRightButtonDownHandler;
  77. InitialPDFViewControl(PdfViewControl);
  78. }
  79. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  80. {
  81. PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler;
  82. }
  83. #endregion
  84. #region Expand and collapse Panel
  85. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  86. {
  87. PropertyContainer.Width = 260;
  88. PropertyContainer.Child = propertytPanel;
  89. PropertyContainer.Visibility = visible;
  90. }
  91. public void ExpandLeftPanel(bool isExpand)
  92. {
  93. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  94. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  95. if (isExpand)
  96. {
  97. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  98. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  99. }
  100. else
  101. {
  102. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  103. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  104. }
  105. }
  106. #endregion
  107. #region Private Command Method
  108. private void PDFToolManager_MouseLeftButtonUpHandler(object sender, MouseEventObject e)
  109. {
  110. if (e.IsCreate&& e.annotType == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  111. {
  112. CPDFAnnotation baseWidget = PdfViewControl.PDFToolManager.GetCPDFAnnotation();
  113. if (baseWidget != null)
  114. {
  115. CPDFWidget widget =(baseWidget as CPDFWidget);
  116. switch (widget.WidgetType)
  117. {
  118. case C_WIDGET_TYPE.WIDGET_NONE:
  119. break;
  120. case C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  121. widget.SetFieldName("Button" + GetTime());
  122. break;
  123. case C_WIDGET_TYPE.WIDGET_CHECKBOX:
  124. widget.SetFieldName("Checkbox" + GetTime());
  125. break;
  126. case C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
  127. widget.SetFieldName("Radio button");
  128. break;
  129. case C_WIDGET_TYPE.WIDGET_TEXTFIELD:
  130. widget.SetFieldName("Text" + GetTime());
  131. break;
  132. case C_WIDGET_TYPE.WIDGET_COMBOBOX:
  133. widget.SetFieldName("Combobox" + GetTime());
  134. break;
  135. case C_WIDGET_TYPE.WIDGET_LISTBOX:
  136. widget.SetFieldName("List" + GetTime());
  137. break;
  138. case C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
  139. widget.SetFieldName("Signature" + GetTime());
  140. break;
  141. case C_WIDGET_TYPE.WIDGET_UNKNOWN:
  142. break;
  143. default:
  144. break;
  145. }
  146. PdfViewControl.UpdateAnnotFrame();
  147. }
  148. }
  149. }
  150. private void PDFToolManager_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
  151. {
  152. if (e.annotType== C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  153. {
  154. BaseWidget baseWidget = PdfViewControl.GetCacheHitTestWidget();
  155. if (baseWidget!=null)
  156. {
  157. switch ((baseWidget.GetAnnotData().Annot as CPDFWidget).WidgetType)
  158. {
  159. case C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  160. case C_WIDGET_TYPE.WIDGET_COMBOBOX:
  161. case C_WIDGET_TYPE.WIDGET_LISTBOX:
  162. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  163. break;
  164. default:
  165. break;
  166. }
  167. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  168. PdfViewControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  169. baseWidget.GetAnnotData().PageIndex,
  170. baseWidget.GetAnnotData().Annot);
  171. FromPropertyControl.SetPropertyForType(
  172. annotParam,
  173. baseWidget.GetAnnotData().Annot,
  174. PdfViewControl.PDFViewTool.GetCPDFViewer().GetDocument());
  175. }
  176. }
  177. }
  178. private string GetTime()
  179. {
  180. DateTime dateTime = DateTime.Now;
  181. return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
  182. }
  183. private void UndoButton_Click(object sender, RoutedEventArgs e)
  184. {
  185. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  186. {
  187. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Undo();
  188. PdfViewControl.PDFToolManager.ClearSelect();
  189. PdfViewControl.PDFViewTool.GetCPDFViewer().UpdateAnnotFrame();
  190. }
  191. }
  192. private void RedoButton_Click(object sender, RoutedEventArgs e)
  193. {
  194. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  195. {
  196. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Redo();
  197. PdfViewControl.PDFToolManager.ClearSelect();
  198. PdfViewControl.PDFViewTool.GetCPDFViewer().UpdateAnnotFrame();
  199. }
  200. }
  201. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  202. {
  203. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null && CanUndo)
  204. {
  205. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Undo();
  206. }
  207. }
  208. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  209. {
  210. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null && CanRedo)
  211. {
  212. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Redo();
  213. }
  214. }
  215. protected void OnPropertyChanged([CallerMemberName] string name = null)
  216. {
  217. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  218. }
  219. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  220. {
  221. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  222. {
  223. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  224. }
  225. else if (e.PropertyName == nameof(PanelState.RightPanel))
  226. {
  227. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  228. {
  229. ExpandRightPropertyPanel(FromPropertyControl, Visibility.Visible);
  230. }
  231. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  232. {
  233. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  234. }
  235. else
  236. {
  237. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  238. }
  239. }
  240. }
  241. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  242. {
  243. OnPropertyChanged(e.PropertyName);
  244. if (e.PropertyName == "CanSave")
  245. {
  246. OnCanSaveChanged?.Invoke(this, CanSave);
  247. }
  248. }
  249. #endregion
  250. #region Public Method
  251. public void InitWithPDFViewer(PDFViewControl pDFViewControl)
  252. {
  253. PdfViewControl = pDFViewControl;
  254. PdfViewControl.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  255. PdfViewControl.MouseLeftButtonUpHandler -= PDFToolManager_MouseLeftButtonUpHandler;
  256. PdfViewControl.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
  257. PdfViewControl.MouseLeftButtonUpHandler += PDFToolManager_MouseLeftButtonUpHandler;
  258. PDFFormTool.InitWithPDFViewer(pDFViewControl, FromPropertyControl);
  259. FloatPageTool.InitWithPDFViewer(pDFViewControl);
  260. PDFGrid.Child = PdfViewControl;
  261. panelState.PropertyChanged -= PanelState_PropertyChanged;
  262. panelState.PropertyChanged += PanelState_PropertyChanged;
  263. }
  264. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  265. {
  266. this.BotaContainer.Child = botaControl;
  267. }
  268. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  269. {
  270. this.displaySettingsControl = displaySettingsControl;
  271. }
  272. public void ClearAllToolState()
  273. {
  274. this.PDFFormTool.ClearAllToolState();
  275. }
  276. public void SetToolBarContainerVisibility(Visibility visibility)
  277. {
  278. this.ToolBarContainer.Visibility = visibility;
  279. }
  280. public void InitialPDFViewControl(PDFViewControl newPDFViewer)
  281. {
  282. PDFFormTool.InitWithPDFViewer(newPDFViewer, FromPropertyControl);
  283. FromPropertyControl.SetPDFViewer(newPDFViewer);
  284. PDFFormTool.ClearAllToolState();
  285. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  286. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  287. newPDFViewer.CustomSignHandle = true;
  288. }
  289. private void PdfViewControl_MouseRightButtonDownHandler(object sender, MouseEventObject e)
  290. {
  291. ContextMenu ContextMenu = PdfViewControl.GetRightMenu();
  292. if (ContextMenu == null)
  293. {
  294. ContextMenu = new ContextMenu();
  295. }
  296. switch (e.hitTestType)
  297. {
  298. case MouseHitTestType.Annot:
  299. case MouseHitTestType.SelectRect:
  300. CreateFormContextMenu(sender, ref ContextMenu, e.annotType);
  301. break;
  302. default:
  303. ContextMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  304. PdfViewControl.CreateViewerMenu(sender, ref ContextMenu);
  305. break;
  306. }
  307. PdfViewControl.SetRightMenu(ContextMenu);
  308. }
  309. private void CreateFormContextMenu(object sender, ref ContextMenu menu, C_ANNOTATION_TYPE annotType)
  310. {
  311. switch (annotType)
  312. {
  313. case C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET:
  314. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  315. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  316. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  317. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  318. break;
  319. default:
  320. break;
  321. }
  322. }
  323. public void UnloadEvent()
  324. {
  325. PdfViewControl.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  326. PdfViewControl.MouseLeftButtonUpHandler -= PDFToolManager_MouseLeftButtonUpHandler;
  327. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  328. panelState.PropertyChanged -= PanelState_PropertyChanged;
  329. }
  330. public void ClearViewerControl()
  331. {
  332. PDFGrid.Child = null;
  333. BotaContainer.Child = null;
  334. PropertyContainer.Child = null;
  335. displaySettingsControl = null;
  336. PdfViewControl.SetCreateWidgetType(C_WIDGET_TYPE.WIDGET_NONE);
  337. }
  338. #endregion
  339. }
  340. }