RegularViewerControl.xaml.cs 16 KB

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