RegularViewerControl.xaml.cs 15 KB

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