RegularViewerControl.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. {
  214. MenuItem resetForms = new MenuItem();
  215. resetForms.Header = "Reset Forms";
  216. resetForms.Click += (o, p) =>
  217. {
  218. if (PdfViewControl != null)
  219. {
  220. PdfViewControl.PDFView?.ResetForm(null);
  221. }
  222. };
  223. e.PopupMenu.Items.Add(resetForms);
  224. }
  225. MenuItem fitWidthMenu = new MenuItem();
  226. fitWidthMenu.Header = "Automatically Resize";
  227. fitWidthMenu.Click += (o, p) =>
  228. {
  229. if (PdfViewControl != null)
  230. {
  231. PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  232. }
  233. };
  234. e.PopupMenu.Items.Add(fitWidthMenu);
  235. MenuItem fitSizeMenu = new MenuItem();
  236. fitSizeMenu.Header = "Actual Size";
  237. fitSizeMenu.Click += (o, p) =>
  238. {
  239. if (PdfViewControl != null)
  240. {
  241. PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  242. }
  243. };
  244. e.PopupMenu.Items.Add(fitSizeMenu);
  245. MenuItem zoomInMenu = new MenuItem();
  246. zoomInMenu.Header = "Zoom In";
  247. zoomInMenu.Click += (o, p) =>
  248. {
  249. if (PdfViewControl != null)
  250. {
  251. double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor + 0.01, true);
  252. PdfViewControl.PDFView?.Zoom(newZoom);
  253. }
  254. };
  255. e.PopupMenu.Items.Add(zoomInMenu);
  256. MenuItem zoomOutMenu = new MenuItem();
  257. zoomOutMenu.Header = "Zoom Out";
  258. zoomOutMenu.Click += (o, p) =>
  259. {
  260. if (PdfViewControl != null)
  261. {
  262. double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor - 0.01, false);
  263. PdfViewControl.PDFView?.Zoom(newZoom);
  264. }
  265. };
  266. e.PopupMenu.Items.Add(zoomOutMenu);
  267. e.PopupMenu.Items.Add(new Separator());
  268. MenuItem singleView = new MenuItem();
  269. singleView.Header = "Single Page";
  270. singleView.Click += (o, p) =>
  271. {
  272. if (PdfViewControl != null)
  273. {
  274. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  275. }
  276. };
  277. e.PopupMenu.Items.Add(singleView);
  278. MenuItem singleContinuousView = new MenuItem();
  279. singleContinuousView.Header = "Single Page Continuous";
  280. singleContinuousView.Click += (o, p) =>
  281. {
  282. if (PdfViewControl != null)
  283. {
  284. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  285. }
  286. };
  287. e.PopupMenu.Items.Add(singleContinuousView);
  288. MenuItem doubleView = new MenuItem();
  289. doubleView.Header = "Two Pages";
  290. doubleView.Click += (o, p) =>
  291. {
  292. if (PdfViewControl != null)
  293. {
  294. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  295. }
  296. };
  297. e.PopupMenu.Items.Add(doubleView);
  298. MenuItem doubleContinuousView = new MenuItem();
  299. doubleContinuousView.Header = "Two Pages Continuous";
  300. doubleContinuousView.Click += (o, p) =>
  301. {
  302. if (PdfViewControl != null)
  303. {
  304. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  305. }
  306. };
  307. e.PopupMenu.Items.Add(doubleContinuousView);
  308. }
  309. }
  310. if (e != null && e.CommandType == CommandType.Copy)
  311. {
  312. e.DoCommand();
  313. }
  314. }
  315. #endregion
  316. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  317. {
  318. PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  319. PdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  320. }
  321. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  322. {
  323. PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  324. }
  325. }
  326. }