RegularViewerControl.xaml.cs 16 KB

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