RegularViewerControl.xaml.cs 17 KB

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