RegularViewerControl.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. using System;
  2. using System.ComponentModel;
  3. using System.Runtime.CompilerServices;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. using Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI;
  8. using Compdfkit_Tools.Helper;
  9. using Compdfkit_Tools.PDFControl;
  10. using ComPDFKit.DigitalSign;
  11. using ComPDFKit.PDFAnnotation.Form;
  12. using ComPDFKitViewer;
  13. namespace Compdfkit_Tools.PDFView
  14. {
  15. public partial class RegularViewerControl : UserControl, INotifyPropertyChanged
  16. {
  17. public PDFViewControl PdfViewControl = new PDFViewControl();
  18. public CPDFAnnotationControl PDFAnnotationControl = new CPDFAnnotationControl();
  19. private SignatureStatusBarControl signatureStatusBarControl;
  20. private CPDFDisplaySettingsControl displaySettingsControl = null;
  21. private PanelState panelState = PanelState.GetInstance();
  22. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  23. public event PropertyChangedEventHandler PropertyChanged;
  24. public event EventHandler<bool> OnCanSaveChanged;
  25. private bool CanSave
  26. {
  27. get
  28. {
  29. if (PdfViewControl != null && PdfViewControl.PDFViewTool.GetCPDFViewer() != null)
  30. {
  31. if (PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo ||
  32. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo)
  33. {
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. }
  40. public RegularViewerControl()
  41. {
  42. InitializeComponent();
  43. panelState.PropertyChanged -= PanelState_PropertyChanged;
  44. panelState.PropertyChanged += PanelState_PropertyChanged;
  45. }
  46. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  47. {
  48. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  49. {
  50. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  51. }
  52. else if (e.PropertyName == nameof(PanelState.RightPanel))
  53. {
  54. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  55. {
  56. ExpandRightPropertyPanel(PDFAnnotationControl, Visibility.Visible);
  57. }
  58. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  59. {
  60. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  61. }
  62. else
  63. {
  64. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  65. }
  66. }
  67. }
  68. public void ExpandLeftPanel(bool isExpand)
  69. {
  70. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  71. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  72. if (isExpand)
  73. {
  74. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  75. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  76. }
  77. else
  78. {
  79. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  80. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  81. }
  82. }
  83. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  84. {
  85. PropertyContainer.Width = 260;
  86. PropertyContainer.Child = propertytPanel;
  87. PropertyContainer.Visibility = visible;
  88. }
  89. #region Init PDFViewer
  90. private void InitialControl()
  91. {
  92. PdfViewControl.PDFToolManager?.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.Pan);
  93. //PdfViewControl.PDFView?.SetShowLink(true);
  94. PDFGrid.Child = PdfViewControl;
  95. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  96. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  97. //PdfViewControl.PDFView.SetFormFieldHighlight(true);
  98. }
  99. public void InitWithPDFViewer(PDFViewControl pdfViewer)
  100. {
  101. PdfViewControl = pdfViewer;
  102. PDFGrid.Child = PdfViewControl;
  103. FloatPageTool.InitWithPDFViewer(pdfViewer);
  104. InitialControl();
  105. DataContext = this;
  106. if(PdfViewControl!=null && PdfViewControl.PDFViewTool.GetCPDFViewer()!=null)
  107. {
  108. //PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  109. //PdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  110. //PdfViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  111. //PdfViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  112. }
  113. }
  114. //private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  115. //{
  116. // if ((e is WidgetSignArgs args))
  117. // {
  118. // var signatureWidget = args.Sign;
  119. // if(signatureWidget != null)
  120. // {
  121. // CPDFSignature sig = signatureWidget.GetSignature(PdfViewControl.PDFView.Document);
  122. // if (signatureWidget.IsSigned() && sig!=null && sig?.SignerList.Count > 0)
  123. // {
  124. // return;
  125. // }
  126. // }
  127. // if (args.WidgetType == C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS)
  128. // {
  129. // panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  130. // CPDFSignatureUI signatureProperty = new CPDFSignatureUI();
  131. // signatureProperty.SetFormProperty(args, PdfViewControl.PDFView);
  132. // PropertyContainer.Child = signatureProperty;
  133. // }
  134. // }
  135. //}
  136. public void CancelWidgetClickHandler()
  137. {
  138. //if (PdfViewControl != null && PdfViewControl.PDFView != null)
  139. //{
  140. // PdfViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  141. //}
  142. }
  143. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  144. {
  145. this.BotaContainer.Child = botaControl;
  146. }
  147. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  148. {
  149. this.displaySettingsControl = displaySettingsControl;
  150. }
  151. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  152. {
  153. this.signatureStatusBarControl = signatureStatusBarControl;
  154. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  155. if (signatureStatusBarControl.Status != SignatureStatus.None)
  156. {
  157. SignatureStatusBorder.Visibility = Visibility.Visible;
  158. }
  159. else
  160. {
  161. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  162. }
  163. }
  164. #endregion
  165. public void ClearViewerControl()
  166. {
  167. PDFGrid.Child = null;
  168. BotaContainer.Child = null;
  169. PropertyContainer.Child = null;
  170. SignatureStatusBorder.Child = null;
  171. displaySettingsControl = null;
  172. }
  173. #region PropertyChanged
  174. /// <summary>
  175. /// Undo Redo Event Noitfy
  176. /// </summary>
  177. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  178. {
  179. OnPropertyChanged(e.PropertyName);
  180. if (e.PropertyName == "CanSave")
  181. {
  182. OnCanSaveChanged?.Invoke(this, CanSave);
  183. }
  184. }
  185. protected void OnPropertyChanged([CallerMemberName] string name = null)
  186. {
  187. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  188. }
  189. #endregion
  190. #region Context Menu
  191. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  192. {
  193. //CommandHelper.ExtraImage_Click(PdfViewControl.PDFView.GetSelectedImages());
  194. }
  195. private void CopyImage_Click(object sender, RoutedEventArgs e)
  196. {
  197. //CommandHelper.CopyImage_Click(PdfViewControl.PDFView.GetSelectedImages());
  198. }
  199. //private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  200. //{
  201. // if (e != null && e.CommandType == CommandType.Context)
  202. // {
  203. // if (e.PressOnSelectedText)
  204. // {
  205. // e.Handle = true;
  206. // e.PopupMenu = new ContextMenu();
  207. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  208. // }
  209. // else if (e.CommandTarget == TargetType.ImageSelection)
  210. // {
  211. // if (PdfViewControl != null && PdfViewControl.PDFView != null && PdfViewControl.PDFView.GetSelectImageCount() > 0)
  212. // {
  213. // e.Handle = true;
  214. // e.PopupMenu = new ContextMenu();
  215. // MenuItem imageCopyMenu = new MenuItem();
  216. // imageCopyMenu = new MenuItem();
  217. // imageCopyMenu.Header = "Copy Images";
  218. // WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  219. // imageCopyMenu.CommandParameter = e;
  220. // e.PopupMenu.Items.Add(imageCopyMenu);
  221. // MenuItem imageExtraMenu = new MenuItem();
  222. // imageExtraMenu = new MenuItem();
  223. // imageExtraMenu.Header = "Extract Images";
  224. // WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  225. // imageExtraMenu.CommandParameter = e;
  226. // e.PopupMenu.Items.Add(imageExtraMenu);
  227. // }
  228. // }
  229. // else
  230. // {
  231. // e.Handle = true;
  232. // e.PopupMenu = new ContextMenu();
  233. // //if (PdfViewControl.CheckHasForm())
  234. // MenuItem fitWidthMenu = new MenuItem();
  235. // fitWidthMenu.Header = "Automatically Resize";
  236. // fitWidthMenu.Click += (o, p) =>
  237. // {
  238. // if (PdfViewControl != null)
  239. // {
  240. // PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  241. // }
  242. // };
  243. // e.PopupMenu.Items.Add(fitWidthMenu);
  244. // MenuItem fitSizeMenu = new MenuItem();
  245. // fitSizeMenu.Header = "Actual Size";
  246. // fitSizeMenu.Click += (o, p) =>
  247. // {
  248. // if (PdfViewControl != null)
  249. // {
  250. // PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  251. // }
  252. // };
  253. // e.PopupMenu.Items.Add(fitSizeMenu);
  254. // MenuItem zoomInMenu = new MenuItem();
  255. // zoomInMenu.Header = "Zoom In";
  256. // zoomInMenu.Click += (o, p) =>
  257. // {
  258. // if (PdfViewControl != null)
  259. // {
  260. // double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor + 0.01, true);
  261. // PdfViewControl.PDFView?.Zoom(newZoom);
  262. // }
  263. // };
  264. // e.PopupMenu.Items.Add(zoomInMenu);
  265. // MenuItem zoomOutMenu = new MenuItem();
  266. // zoomOutMenu.Header = "Zoom Out";
  267. // zoomOutMenu.Click += (o, p) =>
  268. // {
  269. // if (PdfViewControl != null)
  270. // {
  271. // double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor - 0.01, false);
  272. // PdfViewControl.PDFView?.Zoom(newZoom);
  273. // }
  274. // };
  275. // e.PopupMenu.Items.Add(zoomOutMenu);
  276. // e.PopupMenu.Items.Add(new Separator());
  277. // MenuItem singleView = new MenuItem();
  278. // singleView.Header = "Single Page";
  279. // singleView.Click += (o, p) =>
  280. // {
  281. // if (PdfViewControl != null)
  282. // {
  283. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  284. // }
  285. // };
  286. // e.PopupMenu.Items.Add(singleView);
  287. // MenuItem singleContinuousView = new MenuItem();
  288. // singleContinuousView.Header = "Single Page Continuous";
  289. // singleContinuousView.Click += (o, p) =>
  290. // {
  291. // if (PdfViewControl != null)
  292. // {
  293. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  294. // }
  295. // };
  296. // e.PopupMenu.Items.Add(singleContinuousView);
  297. // MenuItem doubleView = new MenuItem();
  298. // doubleView.Header = "Two Pages";
  299. // doubleView.Click += (o, p) =>
  300. // {
  301. // if (PdfViewControl != null)
  302. // {
  303. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  304. // }
  305. // };
  306. // e.PopupMenu.Items.Add(doubleView);
  307. // MenuItem doubleContinuousView = new MenuItem();
  308. // doubleContinuousView.Header = "Two Pages Continuous";
  309. // doubleContinuousView.Click += (o, p) =>
  310. // {
  311. // if (PdfViewControl != null)
  312. // {
  313. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  314. // }
  315. // };
  316. // e.PopupMenu.Items.Add(doubleContinuousView);
  317. // {
  318. // MenuItem resetForms = new MenuItem();
  319. // resetForms.Header = "Reset Forms";
  320. // resetForms.Click += (o, p) =>
  321. // {
  322. // if (PdfViewControl != null)
  323. // {
  324. // PdfViewControl.PDFView?.ResetForm(null);
  325. // }
  326. // };
  327. // e.PopupMenu.Items.Add(new Separator());
  328. // e.PopupMenu.Items.Add(resetForms);
  329. // }
  330. // }
  331. // }
  332. // if (e != null && e.CommandType == CommandType.Copy)
  333. // {
  334. // e.DoCommand();
  335. // }
  336. //}
  337. #endregion
  338. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  339. {
  340. //PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  341. //PdfViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  342. }
  343. }
  344. }