RegularViewerControl.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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;
  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.SplitPDFViewToolCreated -= PdfViewControl_SplitPDFViewToolCreated;
  93. PdfViewControl.SplitPDFViewToolCreated += PdfViewControl_SplitPDFViewToolCreated;
  94. PdfViewControl.PDFToolManager?.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.Viewer);
  95. //PdfViewControl.PDFView?.SetShowLink(true);
  96. PDFGrid.Child = PdfViewControl;
  97. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  98. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  99. //PdfViewControl.PDFView.SetFormFieldHighlight(true);
  100. }
  101. private void PdfViewControl_SplitPDFViewToolCreated(object sender, EventArgs e)
  102. {
  103. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  104. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  105. }
  106. public void InitWithPDFViewer(PDFViewControl pdfViewer)
  107. {
  108. PdfViewControl = pdfViewer;
  109. PDFGrid.Child = PdfViewControl;
  110. FloatPageTool.InitWithPDFViewer(pdfViewer);
  111. InitialControl();
  112. DataContext = this;
  113. if(PdfViewControl!=null && PdfViewControl.PDFViewTool.GetCPDFViewer()!=null)
  114. {
  115. //PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  116. //PdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  117. //PdfViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  118. //PdfViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  119. }
  120. }
  121. //private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  122. //{
  123. // if ((e is WidgetSignArgs args))
  124. // {
  125. // var signatureWidget = args.Sign;
  126. // if(signatureWidget != null)
  127. // {
  128. // CPDFSignature sig = signatureWidget.GetSignature(PdfViewControl.PDFView.Document);
  129. // if (signatureWidget.IsSigned() && sig!=null && sig?.SignerList.Count > 0)
  130. // {
  131. // return;
  132. // }
  133. // }
  134. // if (args.WidgetType == C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS)
  135. // {
  136. // panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  137. // CPDFSignatureUI signatureProperty = new CPDFSignatureUI();
  138. // signatureProperty.SetFormProperty(args, PdfViewControl.PDFView);
  139. // PropertyContainer.Child = signatureProperty;
  140. // }
  141. // }
  142. //}
  143. public void CancelWidgetClickHandler()
  144. {
  145. //if (PdfViewControl != null && PdfViewControl.PDFView != null)
  146. //{
  147. // PdfViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  148. //}
  149. }
  150. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  151. {
  152. this.BotaContainer.Child = botaControl;
  153. }
  154. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  155. {
  156. this.displaySettingsControl = displaySettingsControl;
  157. }
  158. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  159. {
  160. this.signatureStatusBarControl = signatureStatusBarControl;
  161. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  162. if (signatureStatusBarControl.Status != SignatureStatus.None)
  163. {
  164. SignatureStatusBorder.Visibility = Visibility.Visible;
  165. }
  166. else
  167. {
  168. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  169. }
  170. }
  171. #endregion
  172. public void ClearViewerControl()
  173. {
  174. PDFGrid.Child = null;
  175. BotaContainer.Child = null;
  176. PropertyContainer.Child = null;
  177. SignatureStatusBorder.Child = null;
  178. displaySettingsControl = null;
  179. }
  180. #region PropertyChanged
  181. /// <summary>
  182. /// Undo Redo Event Noitfy
  183. /// </summary>
  184. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  185. {
  186. OnPropertyChanged(e.PropertyName);
  187. if (e.PropertyName == "CanSave")
  188. {
  189. OnCanSaveChanged?.Invoke(this, CanSave);
  190. }
  191. }
  192. protected void OnPropertyChanged([CallerMemberName] string name = null)
  193. {
  194. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  195. }
  196. #endregion
  197. #region Context Menu
  198. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  199. {
  200. //CommandHelper.ExtraImage_Click(PdfViewControl.PDFView.GetSelectedImages());
  201. }
  202. private void CopyImage_Click(object sender, RoutedEventArgs e)
  203. {
  204. //CommandHelper.CopyImage_Click(PdfViewControl.PDFView.GetSelectedImages());
  205. }
  206. //private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  207. //{
  208. // if (e != null && e.CommandType == CommandType.Context)
  209. // {
  210. // if (e.PressOnSelectedText)
  211. // {
  212. // e.Handle = true;
  213. // e.PopupMenu = new ContextMenu();
  214. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  215. // }
  216. // else if (e.CommandTarget == TargetType.ImageSelection)
  217. // {
  218. // if (PdfViewControl != null && PdfViewControl.PDFView != null && PdfViewControl.PDFView.GetSelectImageCount() > 0)
  219. // {
  220. // e.Handle = true;
  221. // e.PopupMenu = new ContextMenu();
  222. // MenuItem imageCopyMenu = new MenuItem();
  223. // imageCopyMenu = new MenuItem();
  224. // imageCopyMenu.Header = "Copy Images";
  225. // WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  226. // imageCopyMenu.CommandParameter = e;
  227. // e.PopupMenu.Items.Add(imageCopyMenu);
  228. // MenuItem imageExtraMenu = new MenuItem();
  229. // imageExtraMenu = new MenuItem();
  230. // imageExtraMenu.Header = "Extract Images";
  231. // WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  232. // imageExtraMenu.CommandParameter = e;
  233. // e.PopupMenu.Items.Add(imageExtraMenu);
  234. // }
  235. // }
  236. // else
  237. // {
  238. // e.Handle = true;
  239. // e.PopupMenu = new ContextMenu();
  240. // //if (PdfViewControl.CheckHasForm())
  241. // MenuItem fitWidthMenu = new MenuItem();
  242. // fitWidthMenu.Header = "Automatically Resize";
  243. // fitWidthMenu.Click += (o, p) =>
  244. // {
  245. // if (PdfViewControl != null)
  246. // {
  247. // PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  248. // }
  249. // };
  250. // e.PopupMenu.Items.Add(fitWidthMenu);
  251. // MenuItem fitSizeMenu = new MenuItem();
  252. // fitSizeMenu.Header = "Actual Size";
  253. // fitSizeMenu.Click += (o, p) =>
  254. // {
  255. // if (PdfViewControl != null)
  256. // {
  257. // PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  258. // }
  259. // };
  260. // e.PopupMenu.Items.Add(fitSizeMenu);
  261. // MenuItem zoomInMenu = new MenuItem();
  262. // zoomInMenu.Header = "Zoom In";
  263. // zoomInMenu.Click += (o, p) =>
  264. // {
  265. // if (PdfViewControl != null)
  266. // {
  267. // double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor + 0.01, true);
  268. // PdfViewControl.PDFView?.Zoom(newZoom);
  269. // }
  270. // };
  271. // e.PopupMenu.Items.Add(zoomInMenu);
  272. // MenuItem zoomOutMenu = new MenuItem();
  273. // zoomOutMenu.Header = "Zoom Out";
  274. // zoomOutMenu.Click += (o, p) =>
  275. // {
  276. // if (PdfViewControl != null)
  277. // {
  278. // double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor - 0.01, false);
  279. // PdfViewControl.PDFView?.Zoom(newZoom);
  280. // }
  281. // };
  282. // e.PopupMenu.Items.Add(zoomOutMenu);
  283. // e.PopupMenu.Items.Add(new Separator());
  284. // MenuItem singleView = new MenuItem();
  285. // singleView.Header = "Single Page";
  286. // singleView.Click += (o, p) =>
  287. // {
  288. // if (PdfViewControl != null)
  289. // {
  290. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  291. // }
  292. // };
  293. // e.PopupMenu.Items.Add(singleView);
  294. // MenuItem singleContinuousView = new MenuItem();
  295. // singleContinuousView.Header = "Single Page Continuous";
  296. // singleContinuousView.Click += (o, p) =>
  297. // {
  298. // if (PdfViewControl != null)
  299. // {
  300. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  301. // }
  302. // };
  303. // e.PopupMenu.Items.Add(singleContinuousView);
  304. // MenuItem doubleView = new MenuItem();
  305. // doubleView.Header = "Two Pages";
  306. // doubleView.Click += (o, p) =>
  307. // {
  308. // if (PdfViewControl != null)
  309. // {
  310. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  311. // }
  312. // };
  313. // e.PopupMenu.Items.Add(doubleView);
  314. // MenuItem doubleContinuousView = new MenuItem();
  315. // doubleContinuousView.Header = "Two Pages Continuous";
  316. // doubleContinuousView.Click += (o, p) =>
  317. // {
  318. // if (PdfViewControl != null)
  319. // {
  320. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  321. // }
  322. // };
  323. // e.PopupMenu.Items.Add(doubleContinuousView);
  324. // {
  325. // MenuItem resetForms = new MenuItem();
  326. // resetForms.Header = "Reset Forms";
  327. // resetForms.Click += (o, p) =>
  328. // {
  329. // if (PdfViewControl != null)
  330. // {
  331. // PdfViewControl.PDFView?.ResetForm(null);
  332. // }
  333. // };
  334. // e.PopupMenu.Items.Add(new Separator());
  335. // e.PopupMenu.Items.Add(resetForms);
  336. // }
  337. // }
  338. // }
  339. // if (e != null && e.CommandType == CommandType.Copy)
  340. // {
  341. // e.DoCommand();
  342. // }
  343. //}
  344. #endregion
  345. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  346. {
  347. //PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  348. //PdfViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  349. }
  350. }
  351. }