RegularViewerControl.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.Helper;
  14. using Compdfkit_Tools.PDFControl;
  15. using ComPDFKitViewer;
  16. using ComPDFKitViewer.AnnotEvent;
  17. using ComPDFKitViewer.PdfViewer;
  18. namespace Compdfkit_Tools.PDFView
  19. {
  20. public partial class RegularViewerControl : UserControl, INotifyPropertyChanged
  21. {
  22. public PDFViewControl PdfViewControl = new PDFViewControl();
  23. public CPDFAnnotationControl PDFAnnotationControl = new CPDFAnnotationControl();
  24. private SignatureStatusBarControl signatureStatusBarControl;
  25. private CPDFDisplaySettingsControl displaySettingsControl = null;
  26. private PanelState panelState = PanelState.GetInstance();
  27. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  28. public event PropertyChangedEventHandler PropertyChanged;
  29. public event EventHandler<bool> OnCanSaveChanged;
  30. private bool CanSave
  31. {
  32. get
  33. {
  34. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  35. {
  36. return PdfViewControl.PDFView.UndoManager.CanSave;
  37. }
  38. return false;
  39. }
  40. }
  41. public RegularViewerControl()
  42. {
  43. InitializeComponent();
  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.PDFView?.SetMouseMode(MouseModes.Viewer);
  93. //PDFViewControl.PDFView?.Load();
  94. PdfViewControl.PDFView?.SetShowLink(true);
  95. PDFGrid.Child = PdfViewControl;
  96. PdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  97. PdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  98. PdfViewControl.PDFView.SetFormFieldHighlight(true);
  99. }
  100. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  101. {
  102. PdfViewControl.PDFView = pdfViewer;
  103. PDFGrid.Child = PdfViewControl;
  104. FloatPageTool.InitWithPDFViewer(pdfViewer);
  105. InitialControl();
  106. DataContext = this;
  107. if(PdfViewControl!=null && PdfViewControl.PDFView!=null)
  108. {
  109. PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  110. PdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  111. }
  112. }
  113. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  114. {
  115. this.BotaContainer.Child = botaControl;
  116. }
  117. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  118. {
  119. this.displaySettingsControl = displaySettingsControl;
  120. }
  121. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  122. {
  123. this.signatureStatusBarControl = signatureStatusBarControl;
  124. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  125. if (signatureStatusBarControl.Status != SignatureStatus.None)
  126. {
  127. SignatureStatusBorder.Visibility = Visibility.Visible;
  128. }
  129. else
  130. {
  131. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  132. }
  133. }
  134. #endregion
  135. public void ClearViewerControl()
  136. {
  137. PDFGrid.Child = null;
  138. BotaContainer.Child = null;
  139. PropertyContainer.Child= null;
  140. SignatureStatusBorder.Child = null;
  141. displaySettingsControl = null;
  142. }
  143. #region PropertyChanged
  144. /// <summary>
  145. /// Undo Redo Event Noitfy
  146. /// </summary>
  147. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  148. {
  149. OnPropertyChanged(e.PropertyName);
  150. if (e.PropertyName == "CanSave")
  151. {
  152. OnCanSaveChanged?.Invoke(this, CanSave);
  153. }
  154. }
  155. protected void OnPropertyChanged([CallerMemberName] string name = null)
  156. {
  157. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  158. }
  159. #endregion
  160. #region Context Menu
  161. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  162. {
  163. CommandHelper.ExtraImage_Click(PdfViewControl.PDFView.GetSelectedImages());
  164. }
  165. private void CopyImage_Click(object sender, RoutedEventArgs e)
  166. {
  167. CommandHelper.CopyImage_Click(PdfViewControl.PDFView.GetSelectedImages());
  168. }
  169. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  170. {
  171. if (e != null && e.CommandType == CommandType.Context)
  172. {
  173. if (e.PressOnSelectedText)
  174. {
  175. e.Handle = true;
  176. e.PopupMenu = new ContextMenu();
  177. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  178. }
  179. else if (e.CommandTarget == TargetType.ImageSelection)
  180. {
  181. if (PdfViewControl != null && PdfViewControl.PDFView != null && PdfViewControl.PDFView.GetSelectImageCount() > 0)
  182. {
  183. e.Handle = true;
  184. e.PopupMenu = new ContextMenu();
  185. MenuItem imageCopyMenu = new MenuItem();
  186. imageCopyMenu = new MenuItem();
  187. imageCopyMenu.Header = "Copy Images";
  188. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  189. imageCopyMenu.CommandParameter = e;
  190. e.PopupMenu.Items.Add(imageCopyMenu);
  191. MenuItem imageExtraMenu = new MenuItem();
  192. imageExtraMenu = new MenuItem();
  193. imageExtraMenu.Header = "Extract Images";
  194. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  195. imageExtraMenu.CommandParameter = e;
  196. e.PopupMenu.Items.Add(imageExtraMenu);
  197. }
  198. }
  199. else
  200. {
  201. e.Handle = true;
  202. e.PopupMenu = new ContextMenu();
  203. //if (PdfViewControl.CheckHasForm())
  204. MenuItem fitWidthMenu = new MenuItem();
  205. fitWidthMenu.Header = "Automatically Resize";
  206. fitWidthMenu.Click += (o, p) =>
  207. {
  208. if (PdfViewControl != null)
  209. {
  210. PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  211. }
  212. };
  213. e.PopupMenu.Items.Add(fitWidthMenu);
  214. MenuItem fitSizeMenu = new MenuItem();
  215. fitSizeMenu.Header = "Actual Size";
  216. fitSizeMenu.Click += (o, p) =>
  217. {
  218. if (PdfViewControl != null)
  219. {
  220. PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  221. }
  222. };
  223. e.PopupMenu.Items.Add(fitSizeMenu);
  224. MenuItem zoomInMenu = new MenuItem();
  225. zoomInMenu.Header = "Zoom In";
  226. zoomInMenu.Click += (o, p) =>
  227. {
  228. if (PdfViewControl != null)
  229. {
  230. double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor + 0.01, true);
  231. PdfViewControl.PDFView?.Zoom(newZoom);
  232. }
  233. };
  234. e.PopupMenu.Items.Add(zoomInMenu);
  235. MenuItem zoomOutMenu = new MenuItem();
  236. zoomOutMenu.Header = "Zoom Out";
  237. zoomOutMenu.Click += (o, p) =>
  238. {
  239. if (PdfViewControl != null)
  240. {
  241. double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor - 0.01, false);
  242. PdfViewControl.PDFView?.Zoom(newZoom);
  243. }
  244. };
  245. e.PopupMenu.Items.Add(zoomOutMenu);
  246. e.PopupMenu.Items.Add(new Separator());
  247. MenuItem singleView = new MenuItem();
  248. singleView.Header = "Single Page";
  249. singleView.Click += (o, p) =>
  250. {
  251. if (PdfViewControl != null)
  252. {
  253. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  254. }
  255. };
  256. e.PopupMenu.Items.Add(singleView);
  257. MenuItem singleContinuousView = new MenuItem();
  258. singleContinuousView.Header = "Single Page Continuous";
  259. singleContinuousView.Click += (o, p) =>
  260. {
  261. if (PdfViewControl != null)
  262. {
  263. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  264. }
  265. };
  266. e.PopupMenu.Items.Add(singleContinuousView);
  267. MenuItem doubleView = new MenuItem();
  268. doubleView.Header = "Two Pages";
  269. doubleView.Click += (o, p) =>
  270. {
  271. if (PdfViewControl != null)
  272. {
  273. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  274. }
  275. };
  276. e.PopupMenu.Items.Add(doubleView);
  277. MenuItem doubleContinuousView = new MenuItem();
  278. doubleContinuousView.Header = "Two Pages Continuous";
  279. doubleContinuousView.Click += (o, p) =>
  280. {
  281. if (PdfViewControl != null)
  282. {
  283. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  284. }
  285. };
  286. e.PopupMenu.Items.Add(doubleContinuousView);
  287. {
  288. MenuItem resetForms = new MenuItem();
  289. resetForms.Header = "Reset Forms";
  290. resetForms.Click += (o, p) =>
  291. {
  292. if (PdfViewControl != null)
  293. {
  294. PdfViewControl.PDFView?.ResetForm(null);
  295. }
  296. };
  297. e.PopupMenu.Items.Add(new Separator());
  298. e.PopupMenu.Items.Add(resetForms);
  299. }
  300. }
  301. }
  302. if (e != null && e.CommandType == CommandType.Copy)
  303. {
  304. e.DoCommand();
  305. }
  306. }
  307. #endregion
  308. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  309. {
  310. PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  311. PdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  312. }
  313. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  314. {
  315. PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  316. }
  317. }
  318. }