RegularViewerControl.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 _isActive = false;
  31. public bool IsActive
  32. {
  33. get => _isActive;
  34. set
  35. {
  36. _isActive = value;
  37. OnPropertyChanged();
  38. }
  39. }
  40. private bool CanSave
  41. {
  42. get
  43. {
  44. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  45. {
  46. return PdfViewControl.PDFView.UndoManager.CanSave;
  47. }
  48. return false;
  49. }
  50. }
  51. public RegularViewerControl()
  52. {
  53. InitializeComponent();
  54. panelState.PropertyChanged += PanelState_PropertyChanged;
  55. }
  56. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  57. {
  58. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  59. {
  60. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  61. }
  62. else if (e.PropertyName == nameof(PanelState.RightPanel))
  63. {
  64. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  65. {
  66. ExpandRightPropertyPanel(PDFAnnotationControl, Visibility.Visible);
  67. }
  68. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  69. {
  70. ExpandRightPropertyPanel((IsActive)?displaySettingsControl:null, Visibility.Visible);
  71. }
  72. else
  73. {
  74. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  75. }
  76. }
  77. }
  78. public void ExpandLeftPanel(bool isExpand)
  79. {
  80. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  81. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  82. if (isExpand)
  83. {
  84. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  85. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  86. }
  87. else
  88. {
  89. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  90. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  91. }
  92. }
  93. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  94. {
  95. PropertyContainer.Width = 260;
  96. PropertyContainer.Child = propertytPanel;
  97. PropertyContainer.Visibility = visible;
  98. }
  99. #region Init PDFViewer
  100. private void InitialControl()
  101. {
  102. PdfViewControl.PDFView?.SetMouseMode(MouseModes.Viewer);
  103. //PDFViewControl.PDFView?.Load();
  104. PdfViewControl.PDFView?.SetShowLink(true);
  105. PDFGrid.Child = PdfViewControl;
  106. PdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  107. PdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  108. PdfViewControl.PDFView.SetFormFieldHighlight(true);
  109. }
  110. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  111. {
  112. PdfViewControl.PDFView = pdfViewer;
  113. PDFGrid.Child = PdfViewControl;
  114. FloatPageTool.InitWithPDFViewer(pdfViewer);
  115. InitialControl();
  116. DataContext = this;
  117. if(PdfViewControl!=null && PdfViewControl.PDFView!=null)
  118. {
  119. PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  120. PdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  121. }
  122. }
  123. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  124. {
  125. this.BotaContainer.Child = botaControl;
  126. }
  127. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  128. {
  129. this.displaySettingsControl = displaySettingsControl;
  130. }
  131. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  132. {
  133. this.signatureStatusBarControl = signatureStatusBarControl;
  134. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  135. if (signatureStatusBarControl.Status != SignatureStatus.None)
  136. {
  137. SignatureStatusBorder.Visibility = Visibility.Visible;
  138. }
  139. else
  140. {
  141. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  142. }
  143. }
  144. #endregion
  145. public void ClearViewerControl()
  146. {
  147. PDFGrid.Child = null;
  148. BotaContainer.Child = null;
  149. PropertyContainer.Child= null;
  150. SignatureStatusBorder.Child = null;
  151. }
  152. #region PropertyChanged
  153. /// <summary>
  154. /// Undo Redo Event Noitfy
  155. /// </summary>
  156. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  157. {
  158. OnPropertyChanged(e.PropertyName);
  159. if (e.PropertyName == "CanSave")
  160. {
  161. OnCanSaveChanged?.Invoke(this, CanSave);
  162. }
  163. }
  164. protected void OnPropertyChanged([CallerMemberName] string name = null)
  165. {
  166. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  167. }
  168. #endregion
  169. #region Context Menu
  170. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  171. {
  172. CommandHelper.ExtraImage_Click(PdfViewControl.PDFView.GetSelectedImages());
  173. }
  174. private void CopyImage_Click(object sender, RoutedEventArgs e)
  175. {
  176. CommandHelper.CopyImage_Click(PdfViewControl.PDFView.GetSelectedImages());
  177. }
  178. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  179. {
  180. if (e != null && e.CommandType == CommandType.Context)
  181. {
  182. if (e.PressOnSelectedText)
  183. {
  184. e.Handle = true;
  185. e.PopupMenu = new ContextMenu();
  186. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  187. }
  188. else if (e.CommandTarget == TargetType.ImageSelection)
  189. {
  190. if (PdfViewControl != null && PdfViewControl.PDFView != null && PdfViewControl.PDFView.GetSelectImageCount() > 0)
  191. {
  192. e.Handle = true;
  193. e.PopupMenu = new ContextMenu();
  194. MenuItem imageCopyMenu = new MenuItem();
  195. imageCopyMenu = new MenuItem();
  196. imageCopyMenu.Header = "Copy Images";
  197. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  198. imageCopyMenu.CommandParameter = e;
  199. e.PopupMenu.Items.Add(imageCopyMenu);
  200. MenuItem imageExtraMenu = new MenuItem();
  201. imageExtraMenu = new MenuItem();
  202. imageExtraMenu.Header = "Extract Images";
  203. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  204. imageExtraMenu.CommandParameter = e;
  205. e.PopupMenu.Items.Add(imageExtraMenu);
  206. }
  207. }
  208. else
  209. {
  210. e.Handle = true;
  211. e.PopupMenu = new ContextMenu();
  212. //if (PdfViewControl.CheckHasForm())
  213. MenuItem fitWidthMenu = new MenuItem();
  214. fitWidthMenu.Header = "Automatically Resize";
  215. fitWidthMenu.Click += (o, p) =>
  216. {
  217. if (PdfViewControl != null)
  218. {
  219. PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  220. }
  221. };
  222. e.PopupMenu.Items.Add(fitWidthMenu);
  223. MenuItem fitSizeMenu = new MenuItem();
  224. fitSizeMenu.Header = "Actual Size";
  225. fitSizeMenu.Click += (o, p) =>
  226. {
  227. if (PdfViewControl != null)
  228. {
  229. PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  230. }
  231. };
  232. e.PopupMenu.Items.Add(fitSizeMenu);
  233. MenuItem zoomInMenu = new MenuItem();
  234. zoomInMenu.Header = "Zoom In";
  235. zoomInMenu.Click += (o, p) =>
  236. {
  237. if (PdfViewControl != null)
  238. {
  239. double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor + 0.01, true);
  240. PdfViewControl.PDFView?.Zoom(newZoom);
  241. }
  242. };
  243. e.PopupMenu.Items.Add(zoomInMenu);
  244. MenuItem zoomOutMenu = new MenuItem();
  245. zoomOutMenu.Header = "Zoom Out";
  246. zoomOutMenu.Click += (o, p) =>
  247. {
  248. if (PdfViewControl != null)
  249. {
  250. double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor - 0.01, false);
  251. PdfViewControl.PDFView?.Zoom(newZoom);
  252. }
  253. };
  254. e.PopupMenu.Items.Add(zoomOutMenu);
  255. e.PopupMenu.Items.Add(new Separator());
  256. MenuItem singleView = new MenuItem();
  257. singleView.Header = "Single Page";
  258. singleView.Click += (o, p) =>
  259. {
  260. if (PdfViewControl != null)
  261. {
  262. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  263. }
  264. };
  265. e.PopupMenu.Items.Add(singleView);
  266. MenuItem singleContinuousView = new MenuItem();
  267. singleContinuousView.Header = "Single Page Continuous";
  268. singleContinuousView.Click += (o, p) =>
  269. {
  270. if (PdfViewControl != null)
  271. {
  272. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  273. }
  274. };
  275. e.PopupMenu.Items.Add(singleContinuousView);
  276. MenuItem doubleView = new MenuItem();
  277. doubleView.Header = "Two Pages";
  278. doubleView.Click += (o, p) =>
  279. {
  280. if (PdfViewControl != null)
  281. {
  282. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  283. }
  284. };
  285. e.PopupMenu.Items.Add(doubleView);
  286. MenuItem doubleContinuousView = new MenuItem();
  287. doubleContinuousView.Header = "Two Pages Continuous";
  288. doubleContinuousView.Click += (o, p) =>
  289. {
  290. if (PdfViewControl != null)
  291. {
  292. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  293. }
  294. };
  295. e.PopupMenu.Items.Add(doubleContinuousView);
  296. {
  297. MenuItem resetForms = new MenuItem();
  298. resetForms.Header = "Reset Forms";
  299. resetForms.Click += (o, p) =>
  300. {
  301. if (PdfViewControl != null)
  302. {
  303. PdfViewControl.PDFView?.ResetForm(null);
  304. }
  305. };
  306. e.PopupMenu.Items.Add(new Separator());
  307. e.PopupMenu.Items.Add(resetForms);
  308. }
  309. }
  310. }
  311. if (e != null && e.CommandType == CommandType.Copy)
  312. {
  313. e.DoCommand();
  314. }
  315. }
  316. #endregion
  317. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  318. {
  319. PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  320. PdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  321. }
  322. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  323. {
  324. PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  325. }
  326. }
  327. }