RegularViewerControl.xaml.cs 15 KB

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